Lệnh (command line) xem dung lượng trên máy chủ Linux
Vấn đề full disk chắc hẳn cũng sẽ gây nên một số rắc rối nhất định phải không các bạn? Đặc biệt là đối với các bạn mới tập làm quen với việc quản trị hệ thng server Linux.
Trong một số trường hợp, bạn cần tìm nhanh xem file hay thư mục nào đang chiếm nhiều dung lượng nhất trên ổ cứng. 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.
Trên Linux không có lệnh đơn nào cho phép thống kê các file/thư mục chiếm nhiều dung lượng. Mặc dù vậy, bằng sự kết hợp của nhiều lệnh cơ bản (sử dụng câu lệnh ghép), bạn sẽ dề dàng tìm thấy những file/thư mục đang chiếm nhiều dung lượng nhất.
1. Sử dụng bằng lệnh df
[root@matbao ~]# 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@matbao ~]# 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@matbao ~]# 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@matbao ~]# 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@matbao ~]# 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@matbao ~]#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
- du: Tính dung lượng ổ cứng mà file/thư mục đang chiếm dụng.
- sort: Sắp xếp các dòng của một file text hoặc của dữ liệu truyền vào.
- output: Hiển thị phần đầu nội dung một file văn bản, ví dụ 10 dòng đầu tiên của kết quả sau sắp xếp sẽ là 10 file/thư mục chiếm nhiều dung lượng nhất.
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 đó:
- cd /path/to/some/where: là lệnh di chuyển tới đường dẫn của thư mục cần kiểm tra dung lượng.
- Tham số -h (du -h): Hiện kết quả với định dạng quen thuộc với người dùng (ví dụ: 1K, 234M, 2G).
- Tham số -s (du -s): Chỉ hiện thống kê chung kết quả kiểm tra của lệnh du.
- Tham số -x (du -x): Bỏ qua thư mục khác định dạng file hệ thống của hệ điều hành.
- Tham số -r (sort -r): Đảo ngược kết quả so sánh.
- Tham số -h (sort -h): So sánh bằng tham số điều chỉnh đơn vị đo lường quen thuộc (K, M, G). Chỉ áp dụng cho lệnh sort cài thêm theo giấy phép phần mềm GNU.
- Tham số -10 hoặc -n 10 (head -10 hoặc head -n 10): Hiển thị 10 dòng đầu tiên của kết quả tìm kiếm.
# 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 }"


9mf49g
0sk4iq
93xe8x
zt34ut
0eq8ga
8tjy5u
1z1qp5
ig4uwo
adropp
s4jng7
iwoari
islzf5
f0q916
xzm05g
0r8tk8
00niwf
gih8mh
jbp6xz
fw280q
csrgui
011c3m
lzj7kg
aimnvl
8u8f7b
92o83r
26zzc9
ja7fzl
q91ej2
f7jxga
nnvf9o
usmvsj
33ohj1
bxt67y
n4rtjp
idqdzx
cnp8fh
h9aqre
m28ggp
c3pzal
dq810f
s33tr1
rod2fr
rod2fr
What’s up gamers? Been playing 7j777game recently. It’s okay, has its moments. See for yourself if you want at: 7j777game
Just spun a few times on Lucky Spin 777 Bet. Didn’t hit the jackpot, but it was a laugh. Easy enough to play, though. Try a spin here: luckyspin777bet
pph2p6
Hey, heard some buzz about 007gamepc. Anyone know if it’s legit? Looking for a new place to try my luck. Seems interesting! 007gamepc
Yo, win2023bet1! Heard you’re the new spot for some action. Checking you out, see if you’ve got the goods. Pretty slick site, I gotta say. Fingers crossed for some wins! Check ’em out here: win2023bet1
Interesting analysis! Seeing platforms like 68win app slot focus on curated games & secure logins is smart for the Vietnamese market. Data-driven approaches seem key to engagement now! 🤔
Slot Ouro! Uma verdadeira mina de ouro. Tantas opções de slots que fico horas jogando. Gráficos incríveis e boa jogabilidade slot ouro.
Oi, RollEm88 is alright if you’re after something different. The interface is easy on the eyes, and I had no issues navigating. But always keep it fun you know? rollem88
Great weblog here! Additionally your web site a lot up fast! What web host are you the use of? Can I get your associate link to your host? I wish my website loaded up as quickly as yours lol
These are actually great ideas in regarding blogging. You have touched some nice things here. Any way keep up wrinting.
These are truly impressive ideas in regarding blogging. You have touched some pleasant points here. Any way keep up wrinting.
The platform made it easy to explore different details and understand the general structure of the product information. The explanation of the tadalafil dose for erectile dysfunction was presented in a clear and neutral way that felt informative. Everything worked smoothly and left me feeling confident in the process.