229

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.

10 Answers 10

269

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.

Update:

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.

  • 5
    du gives the disk usage which is different from the file size. With (GNU) du -h, numerical sort won't work (you'll need the -h GNU option to sort). xargs expect a list of possibly quoted words as input so it won't work if filenames contain blanks or quoted characters. – Stéphane Chazelas Nov 3 '12 at 23:57
  • 1
    I see that -S does descending size order, what about ascending order? – demongolem Mar 8 '16 at 15:40
  • 3
    Following @StéphaneChazelas, this works for me: find . -type f -print0 | xargs -0 du -h | sort -rh. If you just want say the largest 30 files: find . -type f -print0 | xargs -0 du -h | sort -rh | head -n 30. – user7543 May 16 '16 at 11:13
  • Hi! Could you please explain the + | syntax in + | sort -r -h? – Victor Nov 9 '18 at 11:44
  • @Victor, please see unix.stackexchange.com/questions/195939/…. – Hew Wolff Nov 27 '18 at 0:33
24

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 '\0' | sort -zk7rn | tr -d '\0'
  • This assumes "group" column (from -ls) not containing any spaces (hence -k 7 parameter), but it isn't necessarily the case. – kolistivra Nov 3 '14 at 16:00
  • 1
    why not simply doing find ... -print0 | sort -zk7rn0 ? – Andre de Miranda Sep 28 '16 at 10:24
9

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 -rl -- **/*(.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 -rl -- **/*(.DOL)
  • what does the .OL do? Is that part of the command? – alpha_989 Jun 12 '18 at 15:36
  • @alpha_989, see edit – Stéphane Chazelas Jun 12 '18 at 15:44
  • Thanks for the explanation, Stephane.. However, for some reason.. the command doesn't work on my workstation .. I am using ls (GNU coreutils) 8.25 and zsh 5.1.1 (x86_64-ubuntu-linux-gnu). Does it work only for certain versions of ls and zsh? – alpha_989 Jun 12 '18 at 18:18
  • @alpha_989, in which way does it not work for you? It works for me with those same versions and there's nothing in that code that wouldn't have worked in the versions from 15 years ago at least. – Stéphane Chazelas Jun 12 '18 at 20:40
8

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
  • Would you mind explaining the options to improve the quality of the answer? – Rui F Ribeiro Jun 29 '16 at 18:53
6

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.

  • 1
    this is nice as it allows you to use the -h modifier on ls to show nice file sizes – shmish111 May 16 '15 at 8:48
3

File list display in reverse order: ls -lSrh

For ascending order: ls -lSh

  • 1
    +1 for using the -h flag for human-friendly output. – computingfreak Jul 3 '16 at 0:34
2

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 () { 
    NUM_FILES=10;
    if [ $1 ]; then
        NUM_FILES=$1;
    fi;
    du | sort -nr | head -n $NUM_FILES
}
2

Try these, it works fine for me.

$ find /home/san -type f -printf '%s %p\n'| 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 
  • 1
    awk works fine if filenames don't contain any spaces. – Eir Nym Mar 20 '16 at 8:01
  • yes .. use 'sed' and replace space with underscore? if that helps – Sandjaie Apr 12 '16 at 9:29
  • No, I use sed to cut output – Eir Nym Apr 12 '16 at 11:21
1

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 https://unix.stackexchange.com/a/188375/82895.

0

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

#!/bin/bash
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.

protected by Community Jan 25 '17 at 10:35

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.