How to Sort Files By Size in Linux? [Command Line]

How to Sort Files By Size in Linux? [Command Line]

`ls -lh` can leave `backup.img` halfway down the screen because the names set the order. I’m curious to see how that same directory reads when size calls the shots.

A size-ranked listing points you to an entry to inspect without turning the directory into one disk-usage total. GNU `ls -S` starts by ordering the entries by size.

Sort directory entries by size with ls

GNU ls sorts a directory’s entries by size with -S, putting larger entries first. Add -l to show the size beside each name, and use the full ls command reference when you need its other listing options.

ls -lhS

The -S option sorts by each entry’s file size, while -l supplies the size column and -h makes the values readable. The capture below uses ls -laS, so it includes hidden entries and shows the sizes in bytes.

ls -laS output ordered from larger entries to smaller entries
ls -laS lists hidden entries in descending size order, with sizes shown in bytes.

GNU ls does not combine every file under a directory tree into one global ranking. It sorts the entries in the directory it lists.

Choose which entries and sizes to show

These options change the contents or display of the listing without changing the size-sort task.

OptionEffectUse it when
-lShows a long listing with a size columnYou need to compare sizes beside file names
-hDisplays sizes with readable unit suffixesByte counts are hard to scan
-aIncludes dot-prefixed names, plus . and ..Hidden entries belong in the listing
-AIncludes dot-prefixed names but omits . and ..You want hidden files without the two directory markers

The -a option adds hidden entries, as in this ls -la output. For a fuller explanation of dot-prefixed names, see how to show hidden files in Linux.

ls -la output showing dot-prefixed hidden entries
ls -la includes hidden names, along with the current and parent directory entries.

Readable suffixes make a listing quicker to scan, but they round the displayed values. This ls -laSh capture shows the same size order with human-readable units.

ls -laSh output with human-readable file sizes in descending order
ls -laSh preserves largest-first sorting and displays rounded size suffixes.

The -S sort still compares file sizes, not the rounded labels. Different file sizes can both display as 4.0K after rounding.

Reverse the order or limit the listing

Replace size-demo in the examples with the directory you want to inspect. Reverse the size order when the smallest entries matter, or limit the long listing when you only need the largest few names.

List the smallest entries first

Add -r to reverse the size sort. The following command puts smaller entries first.

ls -lhSr

This ls -laShr capture shows the same reverse order while including hidden entries. Its zero-byte entries come before the larger files.

ls -laShr output sorted from smaller entries to larger entries
ls -laShr reverses the size sort, placing smaller entries first.

Show only the largest entries

Pipe the largest-first listing to head when you need only its first rows. The total line uses one row, so head -n 4 shows the total plus up to three entries.

ls -lhS size-demo | head -n 4

A directory’s listed size is not its disk usage

In a long ls listing, the size beside a directory describes the directory entry itself, not the combined contents underneath it. Use du when you need a disk-space estimate for files and directories.

du -ah --max-depth=1 size-demo | sort -hr | head -n 6

The -a option includes files, while –max-depth=1 stops at the named directory and its immediate entries. GNU sort’s -h option compares readable units numerically, and -r places larger values first. The du command in Linux reports disk usage, while the sort command handles the ordering step.

GNU du output sorted by disk usage
du -ah reports disk usage. sort -hr puts larger entries first.

For recursive directory totals, see how to list directories by their total size. To search individual files across descendants, use the steps for how to find large files across a directory tree.

What you need to compareCommandWhat the size means
Entries in one directoryls -lhSEach entry’s displayed file or directory-entry size
Files and disk use in a directorydu -ah –max-depth=1Disk space attributed to files and directory contents at this depth
Individual files through a treefind with a size filterFile-by-file search, rather than one ls listing

Use the size that answers your question

Use ls when you want to rank entries in one directory. Switch to du when the question is how much space a directory’s contents use.

ls -lhS

Frequently asked questions

Use these distinctions when the basic size sort is not quite the listing you need.

Does ls -S search subdirectories?

No. It sorts the entries in each directory listing and does not produce one combined recursive file list.

Does -h change the sort order?

No. -S compares file sizes, while -h changes how long format displays each size, so rounded labels can match even when exact sizes differ.