Học mạng máy tính – Kiến thức Quản trị hệ thống

Lệnh xem dung lượng trên máy chủ Linux (Command line)

Lệnh xem dung lượng trên máy chủ Linux
Bài này thuộc phần 18 của 18 phần trong series Học CentOS 7 căn bản
3/5 - (2 bình chọn)

Server hoặc VPS của bạn đang gặp vấn đề Full disk? Bạn cần tìm xem file hay thư mục nào đang chiếm nhiều dung lượng nhất trên ổ cứng? Hôm nay HMMT sẽ chia sẽ cách xem dung lượng trên máy chủ Linux thông qua Command line và thông thường, chúng ta sẽ cần tìm ở một số thư mục chính có khả năng chiếm nhiều dung lượng như /tmp, /home hay /var.

1. Sử dụng bằng lệnh df

[root@hmmt ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
 /dev/cciss/c0d0p2     78361192  23185840  51130588  32% /
 /dev/cciss/c0d0p5     24797380  22273432   1243972  95% /home
 /dev/cciss/c0d0p3     29753588  25503792   2713984  91% /data
 /dev/cciss/c0d0p1       295561     21531    258770   8% /boot
[root@hmmt ~]# df -a
Filesystem           1K-blocks      Used Available Use% Mounted on
 /dev/cciss/c0d0p2     78361192  23186116  51130312  32% /
 proc                         0         0         0   -  /proc
 sysfs                        0         0         0   -  /sys
 devpts                       0         0         0   -  /dev/pts
 /dev/cciss/c0d0p5     24797380  22273432   1243972  95% /home
 /dev/cciss/c0d0p3     29753588  25503792   2713984  91% /data
 /dev/cciss/c0d0p1       295561     21531    258770   8% /boot
 tmpfs                   257476         0    257476   0% /dev/shm
 none                         0         0         0   -  /proc/sys/fs/binfmt_misc
 sunrpc                       0         0         0   -  /var/lib/nfs/rpc_pipefs
[root@hmmt ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
 /dev/cciss/c0d0p2      75G   23G   49G  32% /
 /dev/cciss/c0d0p5      24G   22G  1.2G  95% /home
 /dev/cciss/c0d0p3      29G   25G  2.6G  91% /data
 /dev/cciss/c0d0p1     289M   22M  253M   8% /boot
 tmpfs                 252M     0  252M   0% /dev/shmof /home
[root@hmmt ~]# df -hT /home
Filesystem      Type    Size  Used Avail Use% Mounted on
 /dev/cciss/c0d0p5   ext3     24G   22G  1.2G  95% /home
 View all Disk Partitions in Linux
[root@hmmt ~]# fdisk –l
Disk /dev/sda: 637.8 GB, 637802643456 bytes
 255 heads, 63 sectors/track, 77541 cylinders
 Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
 /dev/sda1   *           1          13      104391   83  Linux
 /dev/sda2              14        2624    20972857+  83  Linux
 /dev/sda3            2625        4582    15727635   83  Linux
 /dev/sda4            4583       77541   586043167+   5  Extended

2. Sử dụng lệnh du

Ví dụ: chúng ta sử dụng command sau để bắt đầu tìm 10 file/thư mục chiếm nhiều dung lượng nhất

[root@hmmt ~]#du -a /var | sort -n -r | head -n 10
1008372 /var
 313236 /var/www
 253964 /var/log
 192544 /var/lib
 152628 /var/spool
 152508 /var/spool/squid
 136524 /var/spool/squid/00
 95736 /var/log/mrtg.log
 74688 /var/log/squid
 62544 /var/cache

Nếu bạn muốn một kết quả tốt hơn, dễ hiểu hơn thì có thể thử nhiều phương án sau:

$ cd /path/to/some/where
$ du -hsx * | sort -rh | head -10

Trong đó:

# du /home/wiki
 40 /home/wiki/downloads
 4 /home/wiki/.mozilla/plugins
 4 /home/wiki/.mozilla/extensions
 12 /home/wiki/.mozilla
 12 /home/wiki/.ssh
 689112 /home/wiki/Ubuntu-12.10
 689360 /home/wiki
# du -h /home/wiki
 40K /home/wiki/downloads
 4.0K /home/wiki/.mozilla/plugins
 4.0K /home/wiki/.mozilla/extensions
 12K /home/wiki/.mozilla
 12K /home/wiki/.ssh
 673M /home/wiki/Ubuntu-12.10
 du -sh /home/wiki
 674M /home/wiki
 du -a /home/wiki
 4 /home/wiki/.bash_logout
 12 /home/wiki/downloads/uploadprogress-1.0.3.1.tgz
 24 /home/wiki/downloads/Phpfiles-org.tar.bz2
 40 /home/wiki/downloads
 12 /home/wiki/uploadprogress-1.0.3.1.tgz
 4 /home/wiki/.mozilla/plugins
 4 /home/wiki/.mozilla/extensions
 12 /home/wiki/.mozilla
# du -ah /home/wiki
 4.0K /home/wiki/.bash_logout
 12K /home/wiki/downloads/uploadprogress-1.0.3.1.tgz
 24K /home/wiki/downloads/Phpfiles-org.tar.bz2
 40K /home/wiki/downloads
 12K /home/wiki/uploadprogress-1.0.3.1.tgz
 4.0K /home/wiki/.mozilla/plugins
 du -mh /home/wiki
 40K /home/wiki/downloads
 4.0K /home/wiki/.mozilla/plugins
 4.0K /home/wiki/.mozilla/extensions
 12K /home/wiki/.mozilla
 12K /home/wiki/.ssh
 673M /home/wiki/Ubu
 du -ch /home/wiki
 40K /home/wiki/downloads
 4.0K /home/wiki/.mozilla/plugins
 4.0K /home/wiki/.mozilla/extensions
 12K /home/wiki/.mozilla
 12K /home/wiki/.ssh
 673M /home/wiki/Ubuntu-12.10
 674M /home/wiki
 674M total
# du -ah --exclude="*.txt" /home/wiki  (bỏ file .txt )
 4.0K /home/wiki/.bash_logout
 12K /home/wiki/downloads/uploadprogress-1.0.3.1.tgz
 24K /home/wiki/downloads/Phpfiles-org.tar.bz2
 40K /home/wiki/downloads
 12K /home/wiki/uploadprogress-1.0.3.1.tgz
 4.0K /home/wiki/.bash_history
 4.0K /home/wiki/.bash_profile
 4.0K /home/wiki/.mozilla/plugins
# du -ha --time /home/wiki
 4.0K 2012-10-12 22:32 /home/wiki/.bash_logout
 12K 2013-01-19 18:48 /home/wiki/downloads/uploadprogress-1.0.3.1.tgz
 24K 2013-01-19 18:48 /home/wiki/downloads/Phpfiles-org.tar.bz2
 40K 2013-01-19 18:48 /home/wiki/downloads
 12K 2013-01-19 18:32 /home/wiki/uploadprogress-1.0.3.1.tgz
 4.0K 2012-10-13 00:11 /home/wiki/.bash_history
 4.0K 2012-10-12 22:32 /home/wiki/.bash_profile

Hoặc tìm kiếm chi tiết các file dung lượng lớn như :

du -hs * $1 |
 sed  's/^([0-9.]*)([KMG]*)/2 12/
 s/^K/1/;s/^M/2/;s/^G/3/' |
 sort -g -k1 -k2n |
 cut -c3- |
 tail -$2

3. Sử dụng script

Một script tìm kiếm những file với dung lượng >MB bằng find với syntax:

find {/path/to/directory/} -type f -size +{size-in-kb}k -exec ls -lh {} ; | awk "{ print $9 ": " $5 }"

Ví dụ : chúng ta thực hiện tìm những file có dung lượng lớn hơn 50MB tại thư mục hiện hành:

 find . -type f -size +50000k -exec ls -lh {} ; | awk "{ print $9 ": " $5 }"

Search trong /var/log:

 find /var/log -type f -size +100000k -exec ls -lh {} ; | awk "{ print $9 ": " $5 }"

Chúc các bạn thành công mọi thắc mắc, cần sự trợ giúp. Hãy để lại bình luận bên dưới.

Xem tiếp các bài trong Series Học CentOS 7 căn bản

Exit mobile version