您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Linux下怎么獲取目錄大小,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在使用Linux時,我們可能需要獲取特定目錄的大小。 在GUI中,有文件管理器,如Dolphin,Gnome,Nautilus等。但是,如果我們需要在命令行中獲取大小,該怎么辦。
Linux 上有一些應用程序可以 獲取目錄的實際大小。其中,磁盤使用率(du)命令已被 Linux 管理員廣泛使用。
我將向您展示如何使用各種選項獲取文件夾大小。
du 命令 表示 磁盤使用率(Disk Usage)。這是一個標準的 Unix 程序,用于估計當前工作目錄中的文件空間使用情況。
它使用遞歸方式總結磁盤使用情況,以獲取目錄及其子目錄的大小。
如同我說的那樣, 使用 ls 命令時,目錄大小僅顯示 4KB。參見下面的輸出。
$ ls -lh | grep ^d drwxr-xr-x 3 daygeek daygeek 4.0K Aug 2 13:57 Bank_Details drwxr-xr-x 2 daygeek daygeek 4.0K Mar 15 2019 daygeek drwxr-xr-x 6 daygeek daygeek 4.0K Feb 16 2019 drive-2daygeek drwxr-xr-x 13 daygeek daygeek 4.0K Jan 6 2019 drive-mageshm drwxr-xr-x 15 daygeek daygeek 4.0K Sep 29 21:32 Thanu_Photos
使用以下 du 命令格式獲取給定目錄的總大小。在該示例中,我們將得到 /home/daygeek/Documents 目錄的總大小。
$ du -hs /home/daygeek/Documents
或
$ du -h --max-depth=0 /home/daygeek/Documents/ 20G /home/daygeek/Documents
詳細說明:
du – 這是一個命令
-h – 以易讀的格式顯示大小 (例如 1K 234M 2G)
-s – 僅顯示每個參數的總數
–max-depth=N – 目錄的打印深度
使用以下 du 命令格式獲取每個目錄(包括子目錄)的總大小。
在該示例中,我們將獲得每個 /home/daygeek/Documents 目錄及其子目錄的總大小。
$ du -h /home/daygeek/Documents/ | sort -rh | head -20 20G /home/daygeek/Documents/ 9.6G /home/daygeek/Documents/drive-2daygeek 6.3G /home/daygeek/Documents/Thanu_Photos 5.3G /home/daygeek/Documents/Thanu_Photos/Camera 5.3G /home/daygeek/Documents/drive-2daygeek/Thanu-videos 3.2G /home/daygeek/Documents/drive-mageshm 2.3G /home/daygeek/Documents/drive-2daygeek/Thanu-Photos 2.2G /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month 916M /home/daygeek/Documents/drive-mageshm/Tanisha 454M /home/daygeek/Documents/drive-mageshm/2g-backup 415M /home/daygeek/Documents/Thanu_Photos/WhatsApp Video 300M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Jan-2017 288M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Oct-2017 226M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Sep-2017 219M /home/daygeek/Documents/Thanu_Photos/WhatsApp Documents 213M /home/daygeek/Documents/drive-mageshm/photos 163M /home/daygeek/Documents/Thanu_Photos/WhatsApp Video/Sent 161M /home/daygeek/Documents/Thanu_Photos/WhatsApp Images 154M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/June-2017 150M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Nov-2016
使用如下 du 命令格式僅獲取每個目錄的摘要。
$ du -hs /home/daygeek/Documents/* | sort -rh | head -10 9.6G /home/daygeek/Documents/drive-2daygeek 6.3G /home/daygeek/Documents/Thanu_Photos 3.2G /home/daygeek/Documents/drive-mageshm 756K /home/daygeek/Documents/Bank_Details 272K /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png 172K /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png 164K /home/daygeek/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 132K /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png 112K /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png 96K /home/daygeek/Documents/distro-info.xlsx
使用如下 du 命令格式來展示每個目錄的總大小,不包括子目錄。
$ du -hS /home/daygeek/Documents/ | sort -rh | head -20 5.3G /home/daygeek/Documents/Thanu_Photos/Camera 5.3G /home/daygeek/Documents/drive-2daygeek/Thanu-videos 2.3G /home/daygeek/Documents/drive-2daygeek/Thanu-Photos 1.5G /home/daygeek/Documents/drive-mageshm 831M /home/daygeek/Documents/drive-mageshm/Tanisha 454M /home/daygeek/Documents/drive-mageshm/2g-backup 300M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Jan-2017 288M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Oct-2017 253M /home/daygeek/Documents/Thanu_Photos/WhatsApp Video 226M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Sep-2017 219M /home/daygeek/Documents/Thanu_Photos/WhatsApp Documents 213M /home/daygeek/Documents/drive-mageshm/photos 163M /home/daygeek/Documents/Thanu_Photos/WhatsApp Video/Sent 154M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/June-2017 150M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Nov-2016 127M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Dec-2016 100M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Oct-2016 94M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Nov-2017 92M /home/daygeek/Documents/Thanu_Photos/WhatsApp Images 90M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Dec-2017
如果要獲取 Linux 上給定目錄的一級子目錄(包括其子目錄)的大小,請使用以下命令格式。
$ du -h --max-depth=1 /home/daygeek/Documents/ 3.2G /home/daygeek/Documents/drive-mageshm 4.0K /home/daygeek/Documents/daygeek 756K /home/daygeek/Documents/Bank_Details 9.6G /home/daygeek/Documents/drive-2daygeek 6.3G /home/daygeek/Documents/Thanu_Photos 20G /home/daygeek/Documents/
如果要在 du 命令輸出中獲得總計,請使用以下 du 命令格式。
$ du -hsc /home/daygeek/Documents/* | sort -rh | head -10 20G total 9.6G /home/daygeek/Documents/drive-2daygeek 6.3G /home/daygeek/Documents/Thanu_Photos 3.2G /home/daygeek/Documents/drive-mageshm 756K /home/daygeek/Documents/Bank_Details 272K /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png 172K /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png 164K /home/daygeek/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 132K /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png 112K /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png via: https://www.2daygeek.com/find-get-size-of-directory-folder-linux-disk-usage-du-command/
關于“Linux下怎么獲取目錄大小”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。