Linux系統(tǒng)目錄大小通過du命令獲取實例
使用過 Linux 系統(tǒng)的小伙伴都知道應(yīng)該都知道, Linux 系統(tǒng)下的 ls 命令通常被我們用來查看文件目錄的內(nèi)容,但是不知道大家有沒有細(xì)心留意,ls 命令所顯示出來的每一個目錄的大小竟然都只有 4 KB ?
$ ls -lh | grep ^ddrwxr-xr-x 3 alvin alvin 4.0K Aug 2 13:57 Bank_Detailsdrwxr-xr-x 2 alvin alvin 4.0K Mar 15 2019 alvindrwxr-xr-x 6 alvin alvin 4.0K Feb 16 2019 drive-alvindrwxr-xr-x 13 alvin alvin 4.0K Jan 6 2019 drive-mageshmdrwxr-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/Documentsor$ 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 -2020G /home/alvin/Documents/9.6G /home/alvin/Documents/drive-alvin6.3G /home/alvin/Documents/Thanu_Photos5.3G /home/alvin/Documents/Thanu_Photos/Camera5.3G /home/alvin/Documents/drive-alvin/Thanu-videos3.2G /home/alvin/Documents/drive-mageshm2.3G /home/alvin/Documents/drive-alvin/Thanu-Photos2.2G /home/alvin/Documents/drive-alvin/Thanu-photos-by-month916M /home/alvin/Documents/drive-mageshm/Tanisha454M /home/alvin/Documents/drive-mageshm/2g-backup415M /home/alvin/Documents/Thanu_Photos/WhatsApp Video300M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017288M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017226M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017219M /home/alvin/Documents/Thanu_Photos/WhatsApp Documents213M /home/alvin/Documents/drive-mageshm/photos163M /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent161M /home/alvin/Documents/Thanu_Photos/WhatsApp Images154M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017150M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016查看指定目錄下每個文件和目錄的大小
結(jié)合通配符 * 的使用,可以實現(xiàn)顯示出指定目錄下的文件和目錄的大小信息的效果:
$ du -hs /home/alvin/Documents/* | sort -rh | head -109.6G /home/alvin/Documents/drive-alvin6.3G /home/alvin/Documents/Thanu_Photos3.2G /home/alvin/Documents/drive-mageshm756K /home/alvin/Documents/Bank_Details272K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png172K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png164K /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf132K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png112K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png96K /home/alvin/Documents/distro-info.xlsx查看指定目錄下的每個目錄的大小(排除子目錄)
使用 -S 選項,以排除子目錄的形式來統(tǒng)計大小:
$ du -hS /home/alvin/Documents/ | sort -rh | head -205.3G /home/alvin/Documents/Thanu_Photos/Camera5.3G /home/alvin/Documents/drive-alvin/Thanu-videos2.3G /home/alvin/Documents/drive-alvin/Thanu-Photos1.5G /home/alvin/Documents/drive-mageshm831M /home/alvin/Documents/drive-mageshm/Tanisha454M /home/alvin/Documents/drive-mageshm/2g-backup 300M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017288M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017253M /home/alvin/Documents/Thanu_Photos/WhatsApp Video226M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017219M /home/alvin/Documents/Thanu_Photos/WhatsApp Documents213M /home/alvin/Documents/drive-mageshm/photos163M /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent154M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017150M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016127M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2016100M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-201694M /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-201792M /home/alvin/Documents/Thanu_Photos/WhatsApp Images90M /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-mageshm4.0K /home/alvin/Documents/alvin756K /home/alvin/Documents/Bank_Details9.6G /home/alvin/Documents/drive-alvin6.3G /home/alvin/Documents/Thanu_Photos20G /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 -1020G total9.6G /home/alvin/Documents/drive-alvin6.3G /home/alvin/Documents/Thanu_Photos3.2G /home/alvin/Documents/drive-mageshm756K /home/alvin/Documents/Bank_Details272K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png172K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png164K /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf132K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png112K /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png
到此這篇關(guān)于Linux系統(tǒng)目錄大小通過du命令獲取實例的文章就介紹到這了,更多相關(guān)用du命令獲取Linux系統(tǒng)目錄大小內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 在FreeBSD 8.1下搭建Git服務(wù)器步驟2. deepin文件有個鎖頭怎么刪除? deepin刪除帶鎖頭文件的技巧3. deepin linux怎么切換到root權(quán)限? Deepin切換root用戶的技巧4. Mac無線網(wǎng)絡(luò)怎么設(shè)置?蘋果Mac電腦無線網(wǎng)絡(luò)設(shè)置教程5. 深度操作系統(tǒng)20.5發(fā)布 deepin 20.5更新內(nèi)容匯總6. mac安全模式怎么進(jìn)入? Mac系統(tǒng)進(jìn)入安全模式方法7. 微軟Win11專業(yè)版即將默認(rèn)禁用 SMB 來賓認(rèn)證服務(wù)8. Win11 正式版 Build 22621.1105一月累積更新補丁KB5022303發(fā)布(附完整更新日志)9. Linux Mint系統(tǒng)輸入法怎么刪除? Linux Mint刪除多余輸入法的技巧10. 深度操作系統(tǒng)Deepin20.2.1發(fā)布(附下載和更新日志)
