自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

居然這樣獲取 Linux 系統(tǒng)目錄大小,我佛了~

系統(tǒng) Linux
如何去獲取到文件目錄的實際大小呢?別急,Linux 下的 du 命令可以幫到你。

[[401577]]

大家好,我是良許。

使用過 Linux 系統(tǒng)的小伙伴都知道應(yīng)該都知道, Linux 系統(tǒng)下的 ls 命令通常被我們用來查看文件目錄的內(nèi)容,但是不知道大家有沒有細(xì)心留意,ls 命令所顯示出來的每一個目錄的大小竟然都只有 4 KB ?

  1. $ ls -lh | grep ^d 
  2.  
  3. drwxr-xr-x  3 alvin alvin 4.0K Aug  2 13:57 Bank_Details 
  4. drwxr-xr-x  2 alvin alvin 4.0K Mar 15  2019 alvin 
  5. drwxr-xr-x  6 alvin alvin 4.0K Feb 16  2019 drive-alvin 
  6. drwxr-xr-x 13 alvin alvin 4.0K Jan  6  2019 drive-mageshm 
  7. drwxr-xr-x 15 alvin alvin 4.0K Sep 29 21:32 Thanu_Photos 

其實啊,這是因為 Linux 下一切皆文件,相信大家都聽過這句話,所以你看到的 4 KB 只是用于存儲目錄元信息的文件大小而已,并不是我們通常意義上的目錄大小。那么問題來了,如何去獲取到文件目錄的實際大小呢?別急,Linux 下的 du 命令可以幫到你。

du 是英文 disk usage 的縮寫,代表磁盤使用率,它是一個標(biāo)準(zhǔn)的 Unix 程序,用于估算當(dāng)前工作目錄中文件空間的使用情況。

下面將以 /home/alvin/Documents 為例,來介紹 du 命令的使用。

查看指定目錄的總大小

  1. $ du -hs /home/alvin/Documents 
  2. or 
  3. $ du -h --max-depth=0 /home/alvin/Documents/ 
  4.  
  5. 20G    /home/alvin/Documents 

上面兩種方式都可以算出目標(biāo)目錄的總大小,其中:

  • -h 表示顯示成 K、M、G 等大小格式
  • -s 表示顯示總和
  • --max-depth=N 表示目錄級別為 N

查看指定目錄下每個目錄(包括子目錄)的大小

將 -s 選項去掉,不進(jìn)行統(tǒng)計總和,就能顯示每一個目錄的大小了,以下僅顯示前 20 條:

  1. $ du -h /home/alvin/Documents/ | sort -rh | head -20 
  2.  
  3. 20G    /home/alvin/Documents/ 
  4. 9.6G    /home/alvin/Documents/drive-alvin 
  5. 6.3G    /home/alvin/Documents/Thanu_Photos 
  6. 5.3G    /home/alvin/Documents/Thanu_Photos/Camera 
  7. 5.3G    /home/alvin/Documents/drive-alvin/Thanu-videos 
  8. 3.2G    /home/alvin/Documents/drive-mageshm 
  9. 2.3G    /home/alvin/Documents/drive-alvin/Thanu-Photos 
  10. 2.2G    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month 
  11. 916M    /home/alvin/Documents/drive-mageshm/Tanisha 
  12. 454M    /home/alvin/Documents/drive-mageshm/2g-backup 
  13. 415M    /home/alvin/Documents/Thanu_Photos/WhatsApp Video 
  14. 300M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017 
  15. 288M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017 
  16. 226M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017 
  17. 219M    /home/alvin/Documents/Thanu_Photos/WhatsApp Documents 
  18. 213M    /home/alvin/Documents/drive-mageshm/photos 
  19. 163M    /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent 
  20. 161M    /home/alvin/Documents/Thanu_Photos/WhatsApp Images 
  21. 154M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017 
  22. 150M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016 

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

結(jié)合通配符 * 的使用,可以實現(xiàn)顯示出指定目錄下的文件和目錄的大小信息的效果:

  1. $ du -hs /home/alvin/Documents/* | sort -rh | head -10 
  2.  
  3. 9.6G    /home/alvin/Documents/drive-alvin 
  4. 6.3G    /home/alvin/Documents/Thanu_Photos 
  5. 3.2G    /home/alvin/Documents/drive-mageshm 
  6. 756K    /home/alvin/Documents/Bank_Details 
  7. 272K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png 
  8. 172K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png 
  9. 164K    /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 
  10. 132K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png 
  11. 112K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png 
  12. 96K    /home/alvin/Documents/distro-info.xlsx 

查看指定目錄下的每個目錄的大小(排除子目錄)

使用 -S 選項,以排除子目錄的形式來統(tǒng)計大?。?/p>

  1. $ du -hS /home/alvin/Documents/ | sort -rh | head -20 
  2.  
  3. 5.3G    /home/alvin/Documents/Thanu_Photos/Camera 
  4. 5.3G    /home/alvin/Documents/drive-alvin/Thanu-videos 
  5. 2.3G    /home/alvin/Documents/drive-alvin/Thanu-Photos 
  6. 1.5G    /home/alvin/Documents/drive-mageshm 
  7. 831M    /home/alvin/Documents/drive-mageshm/Tanisha 
  8. 454M    /home/alvin/Documents/drive-mageshm/2g-backup  
  9. 300M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017 
  10. 288M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017 
  11. 253M    /home/alvin/Documents/Thanu_Photos/WhatsApp Video 
  12. 226M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017 
  13. 219M    /home/alvin/Documents/Thanu_Photos/WhatsApp Documents 
  14. 213M    /home/alvin/Documents/drive-mageshm/photos 
  15. 163M    /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent 
  16. 154M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017 
  17. 150M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016 
  18. 127M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2016 
  19. 100M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2016 
  20. 94M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2017 
  21. 92M    /home/alvin/Documents/Thanu_Photos/WhatsApp Images 
  22. 90M    /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2017 

查看指定目錄中的一級子目錄的大小

這個就很簡單了,將 max-depth 設(shè)定為 1 就可以了:

  1. $ du -h --max-depth=1 /home/alvin/Documents/ 
  2.  
  3. 3.2G    /home/alvin/Documents/drive-mageshm 
  4. 4.0K    /home/alvin/Documents/alvin 
  5. 756K    /home/alvin/Documents/Bank_Details 
  6. 9.6G    /home/alvin/Documents/drive-alvin 
  7. 6.3G    /home/alvin/Documents/Thanu_Photos 
  8. 20G    /home/alvin/Documents/ 

查看指定目錄的大小(包含統(tǒng)計功能)

使用 -c 選項能夠?qū)Y(jié)果進(jìn)行統(tǒng)計,可以看到,指定目錄下的文件和目錄的大小的總和是 20 G,這和上面第一條命令中所得出的 /home/alvin/Documents/ 的大小 20 G 是一致的:

  1. $ du -hsc /home/alvin/Documents/* | sort -rh | head -10 
  2.  
  3. 20G    total 
  4. 9.6G    /home/alvin/Documents/drive-alvin 
  5. 6.3G    /home/alvin/Documents/Thanu_Photos 
  6. 3.2G    /home/alvin/Documents/drive-mageshm 
  7. 756K    /home/alvin/Documents/Bank_Details 
  8. 272K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png 
  9. 172K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png 
  10. 164K    /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 
  11. 132K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png 
  12. 112K    /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png 

本文授權(quán)轉(zhuǎn)載自公眾號「良許Linux」。良許,世界500強(qiáng)外企Linux開發(fā)工程師,公眾號里分享大量Linux干貨,歡迎關(guān)注!

 

責(zé)任編輯:趙寧寧 來源: 良許Linux
相關(guān)推薦

2022-11-16 08:33:29

監(jiān)聽Spring監(jiān)聽器

2022-04-11 08:20:36

編程輔助工具GitHubCopilot

2020-08-06 09:03:44

Go語言獲取文件代碼

2023-12-12 07:18:47

Linux系統(tǒng)虛擬化

2022-07-20 23:08:55

互聯(lián)網(wǎng)業(yè)務(wù)EDAC設(shè)備故障

2017-04-07 10:00:11

Linux目錄結(jié)構(gòu)分區(qū)大小

2017-07-14 15:20:43

Linux文件系統(tǒng)目錄結(jié)構(gòu)

2021-11-30 09:14:02

Linux目錄命令

2016-11-21 21:28:51

根目錄日志代碼

2015-10-23 15:49:55

程序員加薪升職

2015-10-26 09:10:38

程序員加薪新方法

2021-08-03 22:26:46

Go函數(shù)分頁

2009-05-31 16:19:51

LinuxWin結(jié)構(gòu)

2009-03-18 08:50:59

LinuxWindows系統(tǒng)目錄

2024-05-20 08:45:46

2009-04-15 20:32:39

Linux工作目錄用戶主目錄

2021-10-27 13:51:54

Windows XP微軟系統(tǒng)

2014-07-04 09:41:11

微軟Win8

2021-10-12 06:40:02

cp 命令機(jī)械盤

2021-06-02 08:17:05

門面模式設(shè)計
點贊
收藏

51CTO技術(shù)棧公眾號