獲取Linux系統中目錄文件大小的方法

你應該已經注意到,在 Linux 中使用 ls 命令 列出的目錄內容中,目錄的大小僅顯示 4KB。這個大小正確嗎?若是不正確,那它表明什麼,又該如何獲取 Linux 中的目錄或文件夾大小?這是一個默認的大小,是用來存儲磁盤上存儲目錄的元數據的大小。

Linux 上有一些應用程序能夠 獲取目錄的實際大小。其中,磁盤使用率(du)命令已被 Linux 管理員普遍使用。linux

我將向您展現如何使用各類選項獲取文件夾大小。ide

什麼是 du 命令?spa

du 命令 表示 磁盤使用率(Disk Usage)。這是一個標準的 Unix 程序,用於估計當前工做目錄中的文件空間使用狀況。orm

它使用遞歸方式總結磁盤使用狀況,以獲取目錄及其子目錄的大小。遞歸

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

$ 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

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

使用如下 du 命令格式獲取給定目錄的總大小。在該示例中,咱們將獲得 /home/daygeek/Documents 目錄的總大小。table

$ du -hs /home/daygeek/Documents

class

$ du -h --max-depth=0 /home/daygeek/Documents/
20G    /home/daygeek/Documents

詳細說明:pdf

du – 這是一個命令

-h – 以易讀的格式顯示大小 (例如 1K 234M 2G)

-s – 僅顯示每一個參數的總數

--max-depth=N – 目錄的打印深度

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

使用如下 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

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

使用以下 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

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

使用以下 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

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

若是要獲取 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/

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

若是要在 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
相關文章
相關標籤/搜索