Linux系統(tǒng)目錄大小通過du命令獲取實例
使用過 Linux 系統(tǒng)的小伙伴都知道應該都知道, Linux 系統(tǒng)下的 ls 命令通常被我們用來查看文件目錄的內(nèi)容,但是不知道大家有沒有細心留意,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 的縮寫,代表磁盤使用率,它是一個標準的 Unix 程序,用于估算當前工作目錄中文件空間的使用情況。
下面將以 /home/alvin/Documents 為例,來介紹 du 命令的使用。
查看指定目錄的總大小
$ du -hs /home/alvin/Documents or $ du -h --max-depth=0 /home/alvin/Documents/ 20G /home/alvin/Documents
上面兩種方式都可以算出目標目錄的總大小,其中:
- -h 表示顯示成 K、M、G 等大小格式
- -s 表示顯示總和
- --max-depth=N 表示目錄級別為 N
查看指定目錄下每個目錄(包括子目錄)的大小
將 -s 選項去掉,不進行統(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
查看指定目錄下的每個目錄的大?。ㄅ懦幽夸洠?br />
使用 -S 選項,以排除子目錄的形式來統(tǒng)計大小:
$ 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/
查看指定目錄的大?。òy(tǒng)計功能)
使用 -c 選項能夠?qū)Y(jié)果進行統(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
到此這篇關(guān)于Linux系統(tǒng)目錄大小通過du命令獲取實例的文章就介紹到這了,更多相關(guān)用du命令獲取Linux系統(tǒng)目錄大小內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Centos7的apache網(wǎng)站環(huán)境搭建wordpress
本篇文章給大家詳細分析了在Centos7的apache網(wǎng)站環(huán)境搭建wordpress的詳細操作方法,有興趣的朋友參考下。2018-02-02讓Apache 2支持.htaccess并實現(xiàn)目錄加密的方法
這篇文章主要介紹了讓Apache 2支持.htaccess并實現(xiàn)目錄加密的方法,文中給出了詳細的方法步驟,并給出了示例代碼,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-02-02springcloud alibaba nacos linux配置的詳細教程
這篇文章主要介紹了springcloud alibaba nacos linux配置,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10Linux Vim提示:E325: ATTENTION的問題及解決
這篇文章主要介紹了Linux Vim提示:E325: ATTENTION的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11Apache No space left on device的解決辦法
[Fri Aug 15 10:54:31 2008] [emerg] (28)No space left on device: Couldn't create accept lockdf一下發(fā)現(xiàn)不是磁盤空間的問題。Google了一下就找到了解決方案,原來是系統(tǒng)的信號量(?)不夠用了。2008-08-08Linux運維基礎(chǔ)交換分區(qū)和lvm管理教程
這篇文章主要介紹了Linux運維基礎(chǔ)中的交換分區(qū)和lvm管理教程,附含源碼示例,有需要的朋友可以借鑒參考下,祝大家共同學習共同進步2021-09-09