Linux/Unix – List/sort files by size

Most often listing files in Linux/Unix is easy – just use the simple “ls” command and you are done but when you have to list files ordered by their size, it is a bit tricky.On some installations of Unix you have the -S option to “ls” that can be used to get a listing in Size order. When this option is not available just try the following:

du -ksl * | sort +4 -nr

If you just want the files and not the directories, try:

ls -l|sort +4 -nr

The above will order the file listing by size.

On a MAC, try the following:

find . -type f -print0 | xargs -0 ls -l | sort -k5,5rn

202 votes
1 2 3 4 5

4 Responses to “Linux/Unix – List/sort files by size”

  1. Lawrence Marcotte said:

    Jul 10, 13 at 5:51 pm

    this works
    find /opt/somedir -type f -ls 2>/dev/null | sort -rnk7,7 | perl -lane ‘print “$F[6] $F[10]”‘ |tee /tmp/fileSize.txt

  2. Gunit said:

    Sep 20, 12 at 1:00 am

    ls -lshS

  3. Arvind said:

    Jul 04, 12 at 4:39 pm

    Please can you try:

    find . -type f -print0 | xargs -0 ls -l | sort -k5,5rn

    On some *nix systems the “du” command does not support the “l” option and that can result in this error.

  4. test said:

    Jul 04, 12 at 2:33 pm

    it does not work in my env … i get the following:
    sort: open failed: +4: No such file or directory

Leave a Reply