Linux系統目錄大小通過du命令獲取實例
使用過 Linux 系統的小伙伴都知道應該都知道, Linux 系統下的 ls 命令通常被我們用來查看文件目錄的內容,但是不知道大家有沒有細心留意,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 的縮寫,代表磁盤使用率,它是一個標準的 Unix 程序,用于估算當前工作目錄中文件空間的使用情況。
下面將以 /home/alvin/Documents 為例,來介紹 du 命令的使用。
查看指定目錄的總大小$ du -hs /home/alvin/Documentsor$ du -h --max-depth=0 /home/alvin/Documents/20G /home/alvin/Documents
上面兩種方式都可以算出目標目錄的總大小,其中:
-h 表示顯示成 K、M、G 等大小格式 -s 表示顯示總和 --max-depth=N 表示目錄級別為 N查看指定目錄下每個目錄(包括子目錄)的大小將 -s 選項去掉,不進行統計總和,就能顯示每一個目錄的大小了,以下僅顯示前 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查看指定目錄下每個文件和目錄的大小
結合通配符 * 的使用,可以實現顯示出指定目錄下的文件和目錄的大小信息的效果:
$ 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 選項,以排除子目錄的形式來統計大小:
$ 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 設定為 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/查看指定目錄的大小(包含統計功能)
使用 -c 選項能夠對結果進行統計,可以看到,指定目錄下的文件和目錄的大小的總和是 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
到此這篇關于Linux系統目錄大小通過du命令獲取實例的文章就介紹到這了,更多相關用du命令獲取Linux系統目錄大小內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: