居然這樣獲取 Linux 系統(tǒng)目錄大小,我佛了~
大家好,我是良許。
使用過 Linux 系統(tǒng)的小伙伴都知道應(yīng)該都知道, Linux 系統(tǒng)下的 ls 命令通常被我們用來查看文件目錄的內(nèi)容,但是不知道大家有沒有細(xì)心留意,ls 命令所顯示出來的每一個目錄的大小竟然都只有 4 KB ?
- $ ls -lh | grep ^d
- drwxr-xr-x 3 alvin alvin 4.0K Aug 2 13:57 Bank_Details
- drwxr-xr-x 2 alvin alvin 4.0K Mar 15 2019 alvin
- drwxr-xr-x 6 alvin alvin 4.0K Feb 16 2019 drive-alvin
- drwxr-xr-x 13 alvin alvin 4.0K Jan 6 2019 drive-mageshm
- drwxr-xr-x 15 alvin alvin 4.0K Sep 29 21:32 Thanu_Photos
其實啊,這是因為 Linux 下一切皆文件,相信大家都聽過這句話,所以你看到的 4 KB 只是用于存儲目錄元信息的文件大小而已,并不是我們通常意義上的目錄大小。那么問題來了,如何去獲取到文件目錄的實際大小呢?別急,Linux 下的 du 命令可以幫到你。
du 是英文 disk usage 的縮寫,代表磁盤使用率,它是一個標(biāo)準(zhǔn)的 Unix 程序,用于估算當(dāng)前工作目錄中文件空間的使用情況。
下面將以 /home/alvin/Documents 為例,來介紹 du 命令的使用。
查看指定目錄的總大小
- $ du -hs /home/alvin/Documents
- or
- $ du -h --max-depth=0 /home/alvin/Documents/
- 20G /home/alvin/Documents
上面兩種方式都可以算出目標(biāo)目錄的總大小,其中:
- -h 表示顯示成 K、M、G 等大小格式
- -s 表示顯示總和
- --max-depth=N 表示目錄級別為 N
查看指定目錄下每個目錄(包括子目錄)的大小
將 -s 選項去掉,不進(jìn)行統(tǒng)計總和,就能顯示每一個目錄的大小了,以下僅顯示前 20 條:
- $ du -h /home/alvin/Documents/ | sort -rh | head -20
- 20G /home/alvin/Documents/
- 9.6G /home/alvin/Documents/drive-alvin
- 6.3G /home/alvin/Documents/Thanu_Photos
- 5.3G /home/alvin/Documents/Thanu_Photos/Camera
- 5.3G /home/alvin/Documents/drive-alvin/Thanu-videos
- 3.2G /home/alvin/Documents/drive-mageshm
- 2.3G /home/alvin/Documents/drive-alvin/Thanu-Photos
- 2.2G /home/alvin/Documents/drive-alvin/Thanu-photos-by-month
- 916M /home/alvin/Documents/drive-mageshm/Tanisha
- 454M /home/alvin/Documents/drive-mageshm/2g-backup
- 415M /home/alvin/Documents/Thanu_Photos/WhatsApp Video
- 300M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017
- 288M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017
- 226M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017
- 219M /home/alvin/Documents/Thanu_Photos/WhatsApp Documents
- 213M /home/alvin/Documents/drive-mageshm/photos
- 163M /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent
- 161M /home/alvin/Documents/Thanu_Photos/WhatsApp Images
- 154M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017
- 150M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016
查看指定目錄下每個文件和目錄的大小
結(jié)合通配符 * 的使用,可以實現(xiàn)顯示出指定目錄下的文件和目錄的大小信息的效果:
- $ du -hs /home/alvin/Documents/* | sort -rh | head -10
- 9.6G /home/alvin/Documents/drive-alvin
- 6.3G /home/alvin/Documents/Thanu_Photos
- 3.2G /home/alvin/Documents/drive-mageshm
- 756K /home/alvin/Documents/Bank_Details
- 272K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png
- 172K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png
- 164K /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf
- 132K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png
- 112K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png
- 96K /home/alvin/Documents/distro-info.xlsx
查看指定目錄下的每個目錄的大小(排除子目錄)
使用 -S 選項,以排除子目錄的形式來統(tǒng)計大?。?/p>
- $ du -hS /home/alvin/Documents/ | sort -rh | head -20
- 5.3G /home/alvin/Documents/Thanu_Photos/Camera
- 5.3G /home/alvin/Documents/drive-alvin/Thanu-videos
- 2.3G /home/alvin/Documents/drive-alvin/Thanu-Photos
- 1.5G /home/alvin/Documents/drive-mageshm
- 831M /home/alvin/Documents/drive-mageshm/Tanisha
- 454M /home/alvin/Documents/drive-mageshm/2g-backup
- 300M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017
- 288M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017
- 253M /home/alvin/Documents/Thanu_Photos/WhatsApp Video
- 226M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017
- 219M /home/alvin/Documents/Thanu_Photos/WhatsApp Documents
- 213M /home/alvin/Documents/drive-mageshm/photos
- 163M /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent
- 154M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017
- 150M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016
- 127M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2016
- 100M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2016
- 94M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2017
- 92M /home/alvin/Documents/Thanu_Photos/WhatsApp Images
- 90M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2017
查看指定目錄中的一級子目錄的大小
這個就很簡單了,將 max-depth 設(shè)定為 1 就可以了:
- $ du -h --max-depth=1 /home/alvin/Documents/
- 3.2G /home/alvin/Documents/drive-mageshm
- 4.0K /home/alvin/Documents/alvin
- 756K /home/alvin/Documents/Bank_Details
- 9.6G /home/alvin/Documents/drive-alvin
- 6.3G /home/alvin/Documents/Thanu_Photos
- 20G /home/alvin/Documents/
查看指定目錄的大小(包含統(tǒng)計功能)
使用 -c 選項能夠?qū)Y(jié)果進(jìn)行統(tǒng)計,可以看到,指定目錄下的文件和目錄的大小的總和是 20 G,這和上面第一條命令中所得出的 /home/alvin/Documents/ 的大小 20 G 是一致的:
- $ du -hsc /home/alvin/Documents/* | sort -rh | head -10
- 20G total
- 9.6G /home/alvin/Documents/drive-alvin
- 6.3G /home/alvin/Documents/Thanu_Photos
- 3.2G /home/alvin/Documents/drive-mageshm
- 756K /home/alvin/Documents/Bank_Details
- 272K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png
- 172K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png
- 164K /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf
- 132K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png
- 112K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png
本文授權(quán)轉(zhuǎn)載自公眾號「良許Linux」。良許,世界500強(qiáng)外企Linux開發(fā)工程師,公眾號里分享大量Linux干貨,歡迎關(guān)注!