Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Linux

15 คำสั่ง Linux LS ที่คุณควรรู้

คุณสามารถใช้คำสั่ง Linux ls เพื่อแสดงเนื้อหาใน directory เป็นหนึ่งในคำสั่งเทอร์มินัลพื้นฐานที่สุดใน Linux การเข้าใจมันอย่างลึกซึ้งจึงจำเป็นสำหรับการนำทางในเทอร์มินัล ด้านล่างเป็นตัวอย่างการใช้งาน ls utility ที่มีประโยชน์ Bookmark ไว้เป็น reference สำหรับอนาคต

1. แสดงเนื้อหา Directory

โดย default คำสั่ง ls แสดงรายการไฟล์และ directories ใน current directory สามารถระบุ directories ด้วย relative หรือ absolute path ได้

ls
ls Documents
ls ../

2. แสดงข้อมูลเพิ่มเติม

สามารถแสดงข้อมูลเพิ่มเติมเกี่ยวกับเนื้อหาโฟลเดอร์ด้วย long listing format เพิ่ม option -l เข้าไปใน ls ปกติเพื่อ enable long format output

ls -l ~/Documents

Output ประกอบด้วย Linux file permissions, link count, owner และ group information, file size, last modification time, เป็นต้น

3. แสดงไฟล์ซ่อน (Hidden Files)

Hidden files ใน Linux เริ่มต้นด้วย dot "." symbol คำสั่ง default ไม่แสดงไฟล์ซ่อนเหล่านี้ แต่สามารถดูได้ง่ายด้วย option -a หรือ --all

ls -a
ls --all

4. จำแนกเนื้อหา Directory

Option -F ของ ls ให้จำแนกเนื้อหา directory ตาม type ได้ โดย append ตัวอักษรจากเซต */=>@|

ls -F

Directories แทนด้วย /, executables ด้วย *, symbolic links ด้วย @, เป็นต้น

5. แสดงขนาดไฟล์ (Filesizes)

หากต้องการดู filesizes อย่างเดียว ใช้ option -s หรือ --size ข้อควรทราบคือขนาดแสดงเป็น blocks เหมือนคำสั่ง Linux du

ls -s

6. แสดงขนาดไฟล์อ่านง่าย (Human Readable Filesizes)

ทั้ง ls มาตรฐานและ ls -s แสดง filesize เป็น blocks เพิ่ม option -h เพื่อแสดงใน human-readable format

ls -sh
ls -lh

7. เรียงลำดับ Output ตาม Modification Time

ใช้ option -t ของ ls เรียงเนื้อหา directory ตาม latest modification time เพิ่ม flag -l เพื่อดูข้อมูลเพิ่มเติม

ls -t
ls -lt

8. เรียงลำดับ Output ตาม Size

ใช้ option -S ของ ls สำหรับเรียง output ตามขนาด

ls -S

9. แสดงไฟล์ด้วย Patterns

ใช้ bash wildcards กับ ls commands เพื่อแสดงไฟล์ตาม pattern ตัวอย่างคำสั่งนี้แสดงเฉพาะ mp3 files

ls *.mp3

10. ซ่อนไฟล์ด้วย Patterns

เรายังสามารถซ่อนไฟล์หรือ directories ตาม predefined pattern ได้ ตัวอย่างต่อไปแสดงไฟล์ทั้งหมดยกเว้น mp3 tracks

ls -I .mp3 Music/
ls --hide=.mp3 Music/
ls --ignore=*.mp3 Music/

11. แสดง UID และ GID

Linux systems ใช้ UID (User Identifier) และ GID (Group Identifier) ระบุ users และ groups สามารถแสดงข้อมูลนี้สำหรับไฟล์ทั้งหมดด้วย options -n หรือ --numeric-uid-gid

ls -n
ls --numeric-uid-gid

12. แสดงเนื้อหา Subdirectory

โดย default ls ไม่แสดงเนื้อหาที่เก็บอยู่ใน subdirectories แต่ใช้ option -R หรือ --recursive เพื่อ override ได้ ตัวอย่างด้านล่างแสดงสิ่งนี้

ls -R
ls --recursive

13. แสดง Directories เท่านั้น

ใช้ option -d ตามด้วย bash pattern เพื่อดู sub-directories ทั้งหมดในโฟลเดอร์

ls -d */
ls --directory */

14. แสดง Help Page

Help page ของ ls มี summarized information ของ options ทั้งหมด ใช้เมื่อต้องหา option เฉพาะ

ls --help

15. แสดง Manual Page

สามารถดู man page เพื่อหา detailed instructions ของ ls options ทั้งหมดและวิธีใช้

man ls

สรุป

Utility ls เป็นหนึ่งใน navigation tools ที่ simple ที่สุดแต่ versatile ที่สุดใน Linux ไม่สามารถ master Linux command line ได้จริงๆ หากไม่คุ้นเคยกับ ls commands โชคดีที่มันเรียนรู้ง่ายเมื่อเข้าใจ basic examples อย่างที่แสดงในคู่มือนี้ ดูวิธีอื่นๆ ในการ list directory content ใน Linux terminal