如何獲取 Linux 中的目錄大小

2019-10-25 17:14:00

你應該已經注意到,在 Linux 中使用 ls 命令 列出的目錄內容中,目錄的大小僅顯示 4KB。這個大小正確嗎?如果不正確,那它代表什麼,又該如何獲取 Linux 中的目錄或資料夾大小?這是一個預設的大小,是用來儲存磁碟上儲存目錄的後設資料的大小。

Linux 上有一些應用程式可以 獲取目錄的實際大小。其中,磁碟使用率(du)命令已被 Linux 管理員廣泛使用。

我將向您展示如何使用各種選項獲取資料夾大小。

什麼是 du 命令?

du 命令 表示 磁碟使用率Disk Usage。這是一個標準的 Unix 程式,用於估計當前工作目錄中的檔案空間使用情況。

它使用遞迴方式總結磁碟使用情況,以獲取目錄及其子目錄的大小。

如同我說的那樣, 使用 ls 命令時,目錄大小僅顯示 4KB。參見下面的輸出。

$ ls -lh | grep ^ddrwxr-xr-x  3 daygeek daygeek 4.0K Aug  2 13:57 Bank_Detailsdrwxr-xr-x  2 daygeek daygeek 4.0K Mar 15  2019 daygeekdrwxr-xr-x  6 daygeek daygeek 4.0K Feb 16  2019 drive-2daygeekdrwxr-xr-x 13 daygeek daygeek 4.0K Jan  6  2019 drive-mageshmdrwxr-xr-x 15 daygeek daygeek 4.0K Sep 29 21:32 Thanu_Photos

1) 在 Linux 上如何只獲取父目錄的大小

使用以下 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 – 目錄的列印深度

2) 在 Linux 上如何獲取每個目錄的大小

使用以下 du 命令格式獲取每個目錄(包括子目錄)的總大小。

在該範例中,我們將獲得每個 /home/daygeek/Documents 目錄及其子目錄的總大小。

$ du -h /home/daygeek/Documents/ | sort -rh | head -2020G    /home/daygeek/Documents/9.6G    /home/daygeek/Documents/drive-2daygeek6.3G    /home/daygeek/Documents/Thanu_Photos5.3G    /home/daygeek/Documents/Thanu_Photos/Camera5.3G    /home/daygeek/Documents/drive-2daygeek/Thanu-videos3.2G    /home/daygeek/Documents/drive-mageshm2.3G    /home/daygeek/Documents/drive-2daygeek/Thanu-Photos2.2G    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month916M    /home/daygeek/Documents/drive-mageshm/Tanisha454M    /home/daygeek/Documents/drive-mageshm/2g-backup415M    /home/daygeek/Documents/Thanu_Photos/WhatsApp Video300M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Jan-2017288M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Oct-2017226M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Sep-2017219M    /home/daygeek/Documents/Thanu_Photos/WhatsApp Documents213M    /home/daygeek/Documents/drive-mageshm/photos163M    /home/daygeek/Documents/Thanu_Photos/WhatsApp Video/Sent161M    /home/daygeek/Documents/Thanu_Photos/WhatsApp Images154M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/June-2017150M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Nov-2016

3) 在 Linux 上如何獲取每個目錄的摘要

使用如下 du 命令格式僅獲取每個目錄的摘要。

$ du -hs /home/daygeek/Documents/* | sort -rh | head -109.6G    /home/daygeek/Documents/drive-2daygeek6.3G    /home/daygeek/Documents/Thanu_Photos3.2G    /home/daygeek/Documents/drive-mageshm756K    /home/daygeek/Documents/Bank_Details272K    /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png172K    /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png164K    /home/daygeek/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf132K    /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png112K    /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png96K    /home/daygeek/Documents/distro-info.xlsx

4) 在 Linux 上如何獲取每個目錄的不含子目錄的大小

使用如下 du 命令格式來展示每個目錄的總大小,不包括子目錄。

$ du -hS /home/daygeek/Documents/ | sort -rh | head -205.3G    /home/daygeek/Documents/Thanu_Photos/Camera5.3G    /home/daygeek/Documents/drive-2daygeek/Thanu-videos2.3G    /home/daygeek/Documents/drive-2daygeek/Thanu-Photos1.5G    /home/daygeek/Documents/drive-mageshm831M    /home/daygeek/Documents/drive-mageshm/Tanisha454M    /home/daygeek/Documents/drive-mageshm/2g-backup300M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Jan-2017288M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Oct-2017253M    /home/daygeek/Documents/Thanu_Photos/WhatsApp Video226M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Sep-2017219M    /home/daygeek/Documents/Thanu_Photos/WhatsApp Documents213M    /home/daygeek/Documents/drive-mageshm/photos163M    /home/daygeek/Documents/Thanu_Photos/WhatsApp Video/Sent154M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/June-2017150M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Nov-2016127M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Dec-2016100M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Oct-201694M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Nov-201792M    /home/daygeek/Documents/Thanu_Photos/WhatsApp Images90M    /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Dec-2017

5) 在 Linux 上如何僅獲取一級子目錄的大小

如果要獲取 Linux 上給定目錄的一級子目錄(包括其子目錄)的大小,請使用以下命令格式。

$ du -h --max-depth=1 /home/daygeek/Documents/3.2G    /home/daygeek/Documents/drive-mageshm4.0K    /home/daygeek/Documents/daygeek756K    /home/daygeek/Documents/Bank_Details9.6G    /home/daygeek/Documents/drive-2daygeek6.3G    /home/daygeek/Documents/Thanu_Photos20G    /home/daygeek/Documents/

6) 如何在 du 命令輸出中獲得總計

如果要在 du 命令輸出中獲得總計,請使用以下 du 命令格式。

$ du -hsc /home/daygeek/Documents/* | sort -rh | head -1020G    total9.6G    /home/daygeek/Documents/drive-2daygeek6.3G    /home/daygeek/Documents/Thanu_Photos3.2G    /home/daygeek/Documents/drive-mageshm756K    /home/daygeek/Documents/Bank_Details272K    /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png172K    /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png164K    /home/daygeek/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf132K    /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png112K    /home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png