溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Linux系統(tǒng)中利用du命令獲取系統(tǒng)目錄

發(fā)布時間:2020-12-01 15:45:54 來源:億速云 閱讀:206 作者:Leah 欄目:開發(fā)技術

這篇文章將為大家詳細講解有關怎么在Linux系統(tǒng)中利用du命令獲取系統(tǒng)目錄,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

使用過 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

查看指定目錄下每個文件和目錄的大小

結合通配符 * 的使用,可以實現(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)計大?。?/p>

$ 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 設定為 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果進行統(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

關于怎么在Linux系統(tǒng)中利用du命令獲取系統(tǒng)目錄就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI