linuxbashdu

How to list files in a directory, sorted by size, but without listing folder sizes?


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!


Solution

  • 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-
    

    It will work with the filenames which contain special characters such as a whitespace except for a newline character.