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

วิธีตรวจสอบพื้นที่ดิสก์ว่างใน Linux (df, du, find)

คู่มือนี้แนะนำคำสั่งตรวจสอบพื้นที่ดิสก์ใน Linux: df (Disk Free), du (Disk Usage), find (หาไฟล์ใหญ่)

1. df - Disk Free (พื้นที่ Partition)

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda5        57G  2.4G   52G   5% /
/dev/sda1        99M   12M   83M  12% /boot
/dev/sda2        15G  1.3G   13G  10% /home
  • -h = Human readable (K, M, G)
  • -T = แสดง Filesystem Type
  • -i = แสดง Inode Usage (สำคัญสำหรับไฟล์จำนวนมาก)

2. du - Disk Usage (ขนาดไฟล์/โฟลเดอร์)

# du -sh /path/to/dir
104K    /path/to/dir

# รายละเอียดไฟล์/โฟลเดอร์ย่อย + Total
du -shc /path/to/dir/*
4.0K    /path/to/dir/file1
64K     /path/to/dir/subdir
100K    total
  • -s = Summarize (แสดงแค่ Total)
  • -h = Human readable
  • -c = Grand Total
  • --max-depth=1 = แสดงระดับ 1 (เหมือน *)

3. find - หาไฟล์ขนาดใหญ่

# หาไฟล์ > 100MB ใน /home
find /home -type f -size +100M -exec ls -lh {} \;

# หาไฟล์ > 1GB ทั้งระบบ (ยกเว้น /proc /sys)
find / -type f -size +1G -not -path "/proc/*" -not -path "/sys/*" -exec ls -lh {} \;

# หาไฟล์ใหญ่ 20 อันดับแรก
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | sort -k5 -hr | head -20

เครื่องมือ GUI / ทันสมัยกว่า

Best Practices

  • ตรวจสอบ df -h และ df -i เป็นประจำ (Cron + Alert)
  • ตั้งค่า Logrotate สำหรับ /var/log
  • ลบ Package cache: apt clean, dnf clean all, pacman -Sc
  • ลบ Kernel เก่า: apt autoremove, dnf remove --oldinstallonly
  • ใช้ ncdu / หาโฟลเดอร์ที่กินพื้นที่มากที่สุด

การ Monitor Disk Space เป็นหน้าที่พื้นฐานของ SysAdmin ป้องกัน Out of Space ก่อนเกิดขึ้น

Toolคำอธิบาย
ncduNCurses Disk Usage - TUI เรียกดูโฟลเดอร์ ใช้คีย์บอร์ด นำทางง่าย (apt install ncdu)
baobabDisk Usage Analyzer (GNOME GUI)
dufModern df replacement - สวย รองรับ JSON
dustModern du replacement - รูปแบบ Tree สวย
btrfs/ext4 quotaQuota ต่อ User/Project