Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux du 命令詳解:統計檔案與目錄磁碟佔用空間實例

一、命令格式

du [選項][檔案]

二、命令功能

顯示每個檔案與目錄的磁碟使用空間。

三、命令參數

-a 或 -all 顯示目錄中個別檔案的大小;-b 或 -bytes 以 byte 為單位;-c 或 --total 除個別大小外同時顯示總和;-k 或 --kilobytes 以 KB(1024 bytes)輸出;-m 或 --megabytes 以 MB 輸出;-s 或 --summarize 僅顯示總計;-h 或 --human-readable 以 K、M、G 提高可讀性;-x 或 --one-file-xystem 只處理一開始的檔案系統,遇不同檔案系統則略過;-L 顯示符號連結的來源檔大小;-S 或 --separate-dirs 顯示個別目錄大小時不含子目錄;-X<檔案> 在指定檔案中列出要略過的項目;--exclude=<目錄或檔案> 略過指定項目;-D 顯示符號連結來源檔大小;-H 或 --si 同 -h 但以 1000 為換算單位;-l 或 --count-links 重複計算硬連結檔案。

四、實戰範例

範例一:顯示目錄或檔案所佔空間

命令:du

輸出:

[root@localhost test]# du
608 ./test6
308 ./test4
4 ./scf/lib
32 ./scf
8 ./test3
1288 .
[root@localhost test]#

說明:只顯示當前目錄下各子目錄的大小與當前目錄總大小,最下方的 1288 即當前目錄總大小。

範例二:顯示指定檔案所佔空間

命令:du log2012.log

輸出:

[root@localhost test]# du log2012.log 
300 log2012.log
[root@localhost test]#

範例三:查看指定目錄所佔空間

命令:du scf

輸出:

[root@localhost test]# du scf
4 scf/lib
12 scf/service/deploy
16 scf/service
4 scf/doc
4 scf/bin
32 scf
[root@localhost test]#

範例四:顯示多個檔案所佔空間

命令:du log30.tar.gz log31.tar.gz

輸出:

[root@localhost test]# du log30.tar.gz log31.tar.gz 
4 log30.tar.gz
4 log31.tar.gz
[root@localhost test]#

範例五:只顯示總和大小

命令:du -s

輸出:

[root@localhost test]# du -s
1288 .
[root@localhost test]# du -s scf
32 scf
[root@localhost soft]# du -s test
1288 test
[root@localhost soft]#

範例六:以易讀格式顯示

命令:du -h test

輸出:

[root@localhost soft]# du -h test
608K test/test6
308K test/test4
32K test/scf
8.0K test/test3
1.3M test
[root@localhost soft]#

範例七:檔案與目錄都顯示

命令:du -ah test

輸出:

[root@localhost soft]# du -ah test
4.0K test/log31.tar.gz
300K test/test6/log2012.log
608K test/test6
308K test/test4
32K test/scf
300K test/log2012.log
1.3M test
[root@localhost soft]#

範例八:顯示各自大小並統計總和

命令:du -c log30.tar.gz log31.tar.gz

輸出:

[root@localhost test]# du -c log30.tar.gz log31.tar.gz 
4 log30.tar.gz
4 log31.tar.gz
8 總計
[root@localhost test]#

加上 -c 後,du 不僅顯示各自佔用空間,還在最後一行統計總和。

範例九:依空間大小排序

命令:du | sort -nr | more

輸出:

[root@localhost test]# du|sort -nr|more
1288 .
608 ./test6
308 ./test4
32 ./scf
8 ./test3
4 ./scf/bin
[root@localhost test]#

範例十:輸出當前目錄下各子目錄所使用空間

命令:du -h --max-depth=1

輸出:

[root@localhost test]# du -h --max-depth=1
608K ./test6
308K ./test4
32K ./scf
8.0K ./test3
1.3M .
[root@localhost test]#
Copyright © Linux教程網 All Rights Reserved