I’m writing a bash script that should output the first 10 heaviest files in a directory and all subfolders (the directory is passed to the script as an argument).
And for this I use the following command:
sudo du -haS ../../src/ | sort -hr
, but its output contains folder sizes, and I only need files. Help!
Would you please try the following:
dir="../../src/" sudo find "$dir" -type f -printf "%s\t%p\n" | sort -nr | head -n 10 | cut -f2-
find "$dir" -type fsearches
$dirfor files recursively.
-printf "%s\t%p\n"option tells
findto print the filesize
and the filename delimited by a tab character.
- The final
cut -f2-in the pipeline prints the 2nd and the following
columns, dropping the filesize column only.
It will work with the filenames which contain special characters such as a whitespace except for
a newline character.
Answered By – tshiono