ค้นหาเว็บไซต์

วิธีค้นหาการใช้งานดิสก์ของไฟล์และไดเร็กทอรีใน Linux


Linux 'du' (การใช้งานดิสก์) คือคำสั่ง Unix/Linux มาตรฐานที่ใช้เพื่อตรวจสอบข้อมูลการใช้งานดิสก์ของไฟล์และไดเรกทอรีบนเครื่อง

คำสั่ง du มีตัวเลือกพารามิเตอร์มากมายที่สามารถใช้เพื่อรับผลลัพธ์ได้หลายรูปแบบ คำสั่ง du ยังแสดงไฟล์และขนาดไดเร็กทอรีในลักษณะเรียกซ้ำ

บทความนี้จะอธิบายคำสั่ง 'du' ที่มีประโยชน์พร้อมตัวอย่าง ซึ่งสามารถช่วยคุณกำหนดขนาดของไฟล์และไดเร็กทอรีใน Linux ข้อมูลที่ให้ไว้ในบทความนี้มีที่มาจากหน้าคู่มือของคำสั่ง 'du'

วิธีค้นหาขนาดของไดเรกทอรีใน Linux

1. หากต้องการรับข้อมูลสรุปการใช้งานดิสก์สำหรับแผนผังไดเร็กทอรี '/home/tecmint' และไดเร็กทอรีย่อย ให้ใช้คำสั่งต่อไปนี้

du /home/tecmint

4       /home/tecmint/bin
4       /home/tecmint/.ffbinaries-cache
672     /home/tecmint/.openjfx/cache/17.0.3-ea
676     /home/tecmint/.openjfx/cache
680     /home/tecmint/.openjfx
36      /home/tecmint/.ssh
92      /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml
96      /home/tecmint/.config/xfce4/xfconf
12      /home/tecmint/.config/xfce4/desktop
...
56758808	/home/tecmint

ผลลัพธ์ของคำสั่งข้างต้นจะแสดงจำนวนบล็อกดิสก์ในไดเร็กทอรี /home/tecmint พร้อมด้วยไดเร็กทอรีย่อย

วิธีค้นหาขนาดไดเร็กทอรีในรูปแบบที่มนุษย์สามารถอ่านได้

2. ด้วยการใช้ตัวเลือก '-h' พร้อมด้วยคำสั่ง 'du' คุณจะได้รับผลลัพธ์ในรูปแบบ 'Human Readable รูปแบบ' ซึ่งช่วยให้คุณสามารถดูขนาดเป็นหน่วยต่างๆ เช่น ไบต์ กิโลไบต์ เมกะไบต์ กิกะไบต์ และอื่นๆ

du -h /home/tecmint

4.0K    /home/tecmint/bin
4.0K    /home/tecmint/.ffbinaries-cache
672K    /home/tecmint/.openjfx/cache/17.0.3-ea
676K    /home/tecmint/.openjfx/cache
680K    /home/tecmint/.openjfx
36K     /home/tecmint/.ssh
92K     /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml
96K     /home/tecmint/.config/xfce4/xfconf
12K     /home/tecmint/.config/xfce4/desktop
...
55G	/home/tecmint

วิธีค้นหาขนาดรวมของไดเร็กทอรีใน Linux

3. หากต้องการดูสรุปขนาดการใช้งานดิสก์ทั้งหมดสำหรับไดเร็กทอรี ให้ใช้ตัวเลือก '-s' ดังต่อไปนี้

du -sh /home/tecmint

55G	/home/tecmint

4. การใช้แฟล็ก '-a' ด้วยคำสั่ง 'du' จะแสดงการใช้งานดิสก์ของไฟล์และไดเร็กทอรีทั้งหมด

du -a /home/tecmint

4       /home/tecmint/fix-deb-src-error-ubuntu
4       /home/tecmint/tecmint.py
0       /home/tecmint/bin/FoxitReader
4       /home/tecmint/bin
4       /home/tecmint/.ffbinaries-cache
52      /home/tecmint/Eclipse-with-PyDev.jpg
300     /home/tecmint/Screenshot from 2023-07-27 14-02-45.png
16      /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so
28      /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so
20      /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so
64      /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so
512     /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so
...
56759528	/home/tecmint

5. การใช้แฟล็ก '-a' พร้อมด้วย '-h' จะแสดงการใช้งานดิสก์ของไฟล์และโฟลเดอร์ทั้งหมดในรูปแบบที่มนุษย์สามารถอ่านได้ รูปแบบ. ผลลัพธ์ที่ได้จะง่ายต่อการเข้าใจ เนื่องจากจะแสดงขนาดไฟล์เป็นหน่วยต่างๆ เช่น กิโลไบต์ เมกะไบต์ และอื่นๆ

du -ah /home/tecmint

4.0K    /home/tecmint/fix-deb-src-error-ubuntu
4.0K    /home/tecmint/tecmint.py
28K     /home/tecmint/download.php?file=%2Foomph%2Fepp%2F2023-06%2FR%2Feclipse-inst-jre-linux64.tar.gz.1
0       /home/tecmint/bin/FoxitReader
4.0K    /home/tecmint/bin
4.0K    /home/tecmint/.ffbinaries-cache
52K     /home/tecmint/Eclipse-with-PyDev.jpg
300K    /home/tecmint/Screenshot from 2023-07-27 14-02-45.png
16K     /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so
28K     /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so
20K     /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so
64K     /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so
512K    /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so
...
55G	/home/tecmint

6. หากต้องการตรวจสอบการใช้งานดิสก์ของแผนผังไดเร็กทอรีและไดเร็กทอรีย่อยในบล็อก กิโลไบต์ ให้ใช้ตัวเลือก '-k' (แสดงขนาดเป็น 1,024 ไบต์)

du -k /home/tecmint

4       /home/tecmint/bin
4       /home/tecmint/.ffbinaries-cache
672     /home/tecmint/.openjfx/cache/17.0.3-ea
676     /home/tecmint/.openjfx/cache
680     /home/tecmint/.openjfx
8       /home/tecmint/.uk.co.screamingfrog.seospider.ui.b/webview
12      /home/tecmint/.uk.co.screamingfrog.seospider.ui.b
36      /home/tecmint/.ssh
92      /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml
96      /home/tecmint/.config/xfce4/xfconf
12      /home/tecmint/.config/xfce4/desktop
4       /home/tecmint/.config/xfce4/xfwm4
8       /home/tecmint/.config/xfce4/panel/launcher-18
8       /home/tecmint/.config/xfce4/panel/launcher-17
8       /home/tecmint/.config/xfce4/panel/launcher-14
8       /home/tecmint/.config/xfce4/panel/launcher-3
8       /home/tecmint/.config/xfce4/panel/launcher-16
8       /home/tecmint/.config/xfce4/panel/launcher-5
...
56769128	/home/tecmint

7. หากต้องการรับข้อมูลสรุปการใช้งานดิสก์สำหรับแผนผังไดเร็กทอรี รวมถึงไดเร็กทอรีย่อยในหน่วย เมกะไบต์ (MB) เท่านั้น ให้ใช้ '-mh' ตัวเลือกดังนี้ ธง '-m' นับจำนวนบล็อกในหน่วย MB และ '-h' หมายถึงรูปแบบที่มนุษย์สามารถอ่านได้

du -mh /home/tecmint

4.0K    /home/tecmint/bin
4.0K    /home/tecmint/.ffbinaries-cache
672K    /home/tecmint/.openjfx/cache/17.0.3-ea
676K    /home/tecmint/.openjfx/cache
680K    /home/tecmint/.openjfx
8.0K    /home/tecmint/.uk.co.screamingfrog.seospider.ui.b/webview
12K     /home/tecmint/.uk.co.screamingfrog.seospider.ui.b
36K     /home/tecmint/.ssh
92K     /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml
96K     /home/tecmint/.config/xfce4/xfconf
12K     /home/tecmint/.config/xfce4/desktop
4.0K    /home/tecmint/.config/xfce4/xfwm4
8.0K    /home/tecmint/.config/xfce4/panel/launcher-18
8.0K    /home/tecmint/.config/xfce4/panel/launcher-17
8.0K    /home/tecmint/.config/xfce4/panel/launcher-14
8.0K    /home/tecmint/.config/xfce4/panel/launcher-3
...
55G	/home/tecmint

8. แฟล็ก '-c' ระบุจำนวนการใช้พื้นที่ดิสก์โดยรวมที่บรรทัดสุดท้าย หากไดเร็กทอรีของคุณใช้พื้นที่ 55GB สองบรรทัดสุดท้ายของเอาต์พุตจะปรากฏดังนี้

du -ch /home/tecmint

4.0K    /home/tecmint/bin
4.0K    /home/tecmint/.ffbinaries-cache
672K    /home/tecmint/.openjfx/cache/17.0.3-ea
676K    /home/tecmint/.openjfx/cache
680K    /home/tecmint/.openjfx
8.0K    /home/tecmint/.uk.co.screamingfrog.seospider.ui.b/webview
12K     /home/tecmint/.uk.co.screamingfrog.seospider.ui.b
36K     /home/tecmint/.ssh
92K     /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml
96K     /home/tecmint/.config/xfce4/xfconf
12K     /home/tecmint/.config/xfce4/desktop
4.0K    /home/tecmint/.config/xfce4/xfwm4
8.0K    /home/tecmint/.config/xfce4/panel/launcher-18
8.0K    /home/tecmint/.config/xfce4/panel/launcher-17
8.0K    /home/tecmint/.config/xfce4/panel/launcher-14
8.0K    /home/tecmint/.config/xfce4/panel/launcher-3
8.0K    /home/tecmint/.config/xfce4/panel/launcher-16
...
55G	/home/tecmint
55G	total

วิธีแยกไฟล์ด้วย du Command

9. คำสั่งต่อไปนี้คำนวณและแสดงการใช้งานดิสก์ของไฟล์และไดเร็กทอรีทั้งหมด ยกเว้นไฟล์ที่ตรงกับรูปแบบที่กำหนด

ในตัวอย่างที่ให้มา คำสั่งไม่รวมไฟล์ '.txt' เมื่อคำนวณขนาดรวมของไดเร็กทอรี วิธีการนี้ช่วยให้คุณสามารถยกเว้นรูปแบบไฟล์ที่ต้องการได้โดยใช้แฟล็ก '--exclude'

เมื่อตรวจสอบผลลัพธ์ คุณจะสังเกตเห็นว่าไม่มีรายการสำหรับไฟล์ '.txt'

du -ah --exclude="*.txt" /home/tecmint

4.0K    /home/tecmint/fix-deb-src-error-ubuntu
4.0K    /home/tecmint/tecmint.py
0       /home/tecmint/bin/FoxitReader
4.0K    /home/tecmint/bin
4.0K    /home/tecmint/.ffbinaries-cache
52K     /home/tecmint/Eclipse-with-PyDev.jpg
300K    /home/tecmint/Screenshot from 2023-07-27 14-02-45.png
16K     /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so
28K     /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so
20K     /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so
64K     /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so
512K    /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so
28K     /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_freetype.so
672K    /home/tecmint/.openjfx/cache/17.0.3-ea
...
55G	/home/tecmint

วิธีค้นหาการใช้งานไดเร็กทอรีตามเวลาแก้ไข

10. หากต้องการแสดงการใช้งานดิสก์ตามเวลาแก้ไข ให้ใช้แฟล็ก '--time' ดังที่แสดงด้านล่าง

du -ha --time /home/tecmint

4.0K    2023-07-06 13:15        /home/tecmint/fix-deb-src-error-ubuntu
4.0K    2023-06-14 12:50        /home/tecmint/tecmint.py
0       2021-12-07 12:26        /home/tecmint/bin/FoxitReader
4.0K    2021-12-07 12:26        /home/tecmint/bin
4.0K    2022-10-10 11:02        /home/tecmint/.ffbinaries-cache
52K     2023-05-12 12:09        /home/tecmint/Eclipse-with-PyDev.jpg
300K    2023-07-27 14:02        /home/tecmint/Screenshot from 2023-07-27 14-02-45.png
16K     2022-10-01 12:41        /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so
28K     2022-10-01 12:41        /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so
20K     2022-10-01 12:41        /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so
64K     2022-10-01 12:41        /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so
512K    2022-10-01 12:41        /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so
28K     2022-10-01 12:41        /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_freetype.so
672K    2022-10-01 12:41        /home/tecmint/.openjfx/cache/17.0.3-ea
676K    2022-10-01 12:41        /home/tecmint/.openjfx/cache
...
55G	2023-08-14 10:22	/home/tecmint

คำสั่ง du มีความหลากหลายและสามารถปรับแต่งตามความต้องการของคุณได้โดยใช้ตัวเลือกและแฟล็กต่างๆ ใช้ man du ในเทอร์มินัลเพื่อดูข้อมูลโดยละเอียดเพิ่มเติมเกี่ยวกับตัวเลือกและการใช้งาน