How to list all files ordered by size

I would like to list all files in the order of big to small in size and the files could be present anywhere in a certain folder.

Asked By: Joe


Simply use something like:

ls -lS /path/to/folder/

Capital S.

This will sort files by size.

Also see:

man ls

-S     sort by file size

If you want to sort in reverse order, just add -r switch.


To exclude directories (and provided none of the file names or symlink targets contain newline characters):

ls -lS | grep -v '^d' 

Update 2:

I see now how it still shows symbolic links, which could be folders. Symbolic links always start with a letter l, as in link.

Change the command to filter for a -. This should only leave regular files:

ls -lS | grep '^-'

On my system this only shows regular files.

update 3:

To add recursion, I would leave the sorting of the lines to the sort command and tell it to use the 5th column to sort on.

ls -lR | grep '^-' | sort -k 5 -rn

-rn means Reverse and numeric to get the biggest files at the top. Down side of this command is that it does not show the full path of the files.

If you do need the full path of the files, use something like this:

find . -type f  -exec du -h {} + | sort -r -h

The find command will recursively find all files in all sub directories of . and call du -h (meaning disk usage -humanreadable) and then sort the output again. If your find/sort doesn’t support -h, replace with du -k and sort -rn. Note that size and disk usage are not the same thing.

Answered By: delh

With zsh and GNU ls:

ls -ldU -- **/*(.OL)

Where (.OL) is a glob qualifier, . to select regular files only, OL to reverse order by length (file size, o for ascending order, O for descending).

(note that older versions of zsh had issues with file sizes over 2^32).

Some operating systems have a limit on the size of the argument list passed to a command. In those cases, you’d need:

autoload -U zargs
zargs ./**/*(.OL) -- ls -ldU

If you just want the list of files and not the detailed output, just do:

print -rC1 -- **/*(N.OL)

If you want to include hidden files (whose name starts with a dot, except . and ..) and search in hidden directories as well, add the D globbing qualifier:

print -rC1 -- **/*(ND.OL)
Answered By: Stéphane Chazelas

Saying that “the files could be present anywhere in a certain folder” implies that you want to recursively descend all directories (folders) within the starting directory (folder). This is what find is meant to do:

find . -type f -exec ls -lSd {} +

This “finds” all files in the current working directory (.). For each file found, an ls process is run to sort the objects found in size order. The + terminator to the -exec causes multiple arguments to be passed as a list to ls. Unless your directory (folder) contains a very large number of files, you should have one list (and thus one process forked), leading to the result you desire.

Answered By: JRFerguson

You could use something like find and sort.

find . -type f -ls | sort -r -n -k7

(the -ls option is not standard but is found in many find implementations, not only the GNU one. In GNU find and others, it displays something similar to ls -li with a few exceptions, for instance, files with ACLs are not marked with a +)

If the file names may contain newline characters, with GNU find and GNU sort:

find . -type f -ls -printf '' | sort -zk7rn | tr -d ''
Answered By: Mark Cohen

I wrote something to this extent a while back. You could pass an argument to specify how many files to list, or just type big, in what case you get 10.

big () { 
    if [ $1 ]; then
    du | sort -nr | head -n $NUM_FILES
Answered By: Emanuel Berg

Try these, it works fine for me.

$ find /home/san -type f -printf '%s %pn'| sort -nr | head -n 10

# find /root -type f -exec ls -lS {} + | head -n 10 | awk '{ print $5, $9 }'

Not perfect answer though but works to some extent

$ ls -lS |grep  '^-' | head -n 6 
Answered By: Sandjaie Ravi

Adding to delh’s answer and St├ęphane Chazelas’ comment…

find -print0 combined with xargs -0 adds support for blanks / spaces / whatnots.

du -h | sort -rn doesn’t sort properly between different byte multiples, e.g. 1.1M will show after 128K, which is wrong.

sort -rh (–human-numeric-sort) takes care of that, but it only works on GNU’s version.

The commands below will provide the desired output.

Human-readable, on GNU’s sort / Linux:

find . -type f -print0 | xargs -0 du -h | sort -rh

In kilobyte units, on BSD / OSX / others:

find . -type f -print0 | xargs -0 du -k | sort -rn

For BSD / OSX, also see

Answered By: djule5

File list display in reverse order: ls -lSrh

For ascending order: ls -lSh

Answered By: user174839

List files by size ascending would be:

ls -lSr

The options are:

  • l: long, shows detailed user,group,other attributes, date, etc.
  • S: orders listing by size (descending by default)
  • r: reverses order of listing
Answered By: Baker

As a variation of the original question, if you want to see the cumulative size of files in the subdirectories:

find ${1:-.} -maxdepth 1 -type d -exec du -sm {} ; | sort -nr

The sizes will be displayed in Megabytes ( the m in du -sm). Other values accepted by du are -k for kilobytes, -g for gigabytes. Using -h for human-readable display is not possible because it will break sorting.

Here is a version that uses sed to append the M for megabyte:

find ${1:-.} -maxdepth 1  -type d  -exec du -sm {} ; | sort -nr | sed -E 's/^([0-9]+)/1M/g'

The directory to display is set via ${1:-.} which will use the first command line argument if provided, or use the current directory if called without arguments.

NOTE: This can take a long time with a lot of files. The option -type d will only list subdirectories and exclude files in the current folder; if you also want to see the files in the current folder then remove it.

Note: you might want to use ncdu instead which is available in most linux repos (on ubuntu/debian apt install ncdu) as well as on osx (brew install ncdu). Apart from visualising the sorted tree ncdu can also delete a selected folder with d.

Answered By: ccpizza
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.