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

Linux du 命令詳解:看檔案與目錄實際佔用的磁碟空間

du 與 df 都在看空間,但角度不同:df 看的是檔案系統層級的使用狀況,du 看的是檔案與目錄實際佔了多少。

格式與功能

du [選項][檔案]

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

常用參數

-a, --all          顯示目錄中個別檔案的大小
-b, --bytes        以 byte 為單位
-c, --total        最後加上總和
-k                 以 KB (1024 bytes) 為單位(預設)
-m                 以 MB 為單位
-s, --summarize    只列總計
-h, --human-readable  以 K/M/G 顯示,可讀性最佳
-x, --one-file-xsystem  只算同一檔案系統,遇其他系統略過
-L                 解引用符號連結,顯示來源檔案大小
-S, --separate-dirs  計算目錄時不含子目錄
--exclude=PATTERN  略過指定項目
-H, --si           同 -h,但以 1000 為換算單位
-l, --count-links  重複計算硬連結

實例

# 1. 當前目錄總量
du
52 .

# 2. 指定檔案
du install.log
44 install.log

# 3. 指定目錄(會展開子目錄)
du hello
12  hello/aa
16  hello

# 4. 多個檔案
du test/install.log test/WorldCount.jar

# 5. 只要總和
du -s test
52  test

# 6. 易讀格式
du -h test
52K test

# 7. 檔案與目錄都顯示
du -ah test

# 8. 各自大小 + 總和
du -c hello.txt zookeeper.out
12  hello.txt
56  zookeeper.out
68  total

# 9. 依大小排序
du | sort -nr | more

# 10. 只列第一層子目錄
du -h --max-depth=1

第 10 個寫法最實用:加上 --max-depth=1 就不會遞推到所有子層,輸出乾淨、一眼看出哪個目錄吃空間。

du -h --max-depth=1
4.0K  ./Videos
48K   ./.gnome2
15M   ./.mozilla
14M   ./.cache
31M   .
Copyright © Linux教程網 All Rights Reserved