Search code examples
linuxfilebash

How to list the size of each file and directory and sort by descending size in Bash?


I found that there is no easy to get way the size of a directory in Bash?

I want that when I type ls -<some options>, it can list of all the sum of the file size of directory recursively and files at the same time and sort by size order.

Is that possible?


Solution

  • Simply navigate to directory and run following command:

    du -a --max-depth=1 | sort -n
    

    Or add -h for human readable sizes and -r to print bigger directories/files first.

    du -a -h --max-depth=1 | sort -hr