我需要有關 bash 腳本的幫助。問題是我想按大小順序對所有檔案進行排序,但我只需要檔案,而不是檔案夾,并且還需要顯示它們的大小。我有這個代碼,但檔案夾也出現:
read -p "Enter the size of the top: " MARIMETOP
du -a | sort -n -r | head -n $MARIMETOP | /usr/bin/awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total $1; x = $1; y = 1; while( x > 1024 ) { x = (x 1023)/1024; y ; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total 1023)/1024; y ; } ; }'
uj5u.com熱心網友回復:
這將列印當前目錄和按大小排序的子目錄中的所有常規檔案:
find . -type f -print0 | xargs -0 -n100000 ls -Sl
或者如果您只想要大小和檔案名:
find . -type f -print0 | xargs -0 -n100000 stat -f "%z %N" | sort -n -k1 -r
使用該-n100000標志,這將處理100000由find.
uj5u.com熱心網友回復:
find從 GNU findutils 可以列印檔案大小
find . -type f -printf '%s\t%p\n' | sort -k1,1nr
%s= 檔案大小%p= 檔案路徑(%f僅用于檔案的基本名稱)
uj5u.com熱心網友回復:
雖然 OP 用bash標記了這個問題,但他在評論中說 zsh 解決方案也可以。在zsh你可以把它寫成
du *(.OL)
解釋:
. : Only consider plain files
O : Sort descending
L : Sorting criterium is the length of the file
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/505004.html
