Issue
I need help with a bash script. The problem is that I want to sort all the files in order of size, but I only need files, not folders,and to show me their size as well. I have this code but folders also appear:
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++; } ; }'
Solution
This will print all regular files in the current directory and subdirectories sorted by size:
find . -type f -print0 | xargs -0 -n100000 ls -Sl
or if you want only size and filenames:
find . -type f -print0 | xargs -0 -n100000 stat -f "%z %N" | sort -n -k1 -r
With the -n100000
flag this will handle at most 100000
files found by find
.
Answered By - Özgür Murat Sağdıçoğlu Answer Checked By - Mary Flores (WPSolving Volunteer)