Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

List Files by Size in Linux: Sort & Find Largest

· 5 min read · Home Tech

In this article
  1. Why Sorting by Size Matters
  2. The Basic Command: ls -lS
  3. Recursive Listing with du and sort
  4. Using find to List Files by Size
  5. Human-Readable Sizes and Sorting Pitfalls
  6. Sorting by Size in Different Scenarios
  7. Frequently Asked Questions

To list files ordered by size in Linux, use ls -lS in the current directory, or ls -lS /path/to/dir for another location. For a recursive listing that includes subdirectories, use du -ah /path | sort -rh or find /path -type f -exec ls -l {} + | sort -k5 -rn.

Why Sorting by Size Matters

Knowing which files consume the most disk space helps you manage storage, clean up old logs, and identify large backups or media files. Without sorting, a directory listing shows files in alphabetical order, making it hard to spot the culprits behind a full disk. Sorting by size gives you an immediate overview of what to archive or delete first.

The Basic Command: ls -lS

The ls command with the -S flag sorts files by size, largest first. Combine it with -l for a long listing that shows size in bytes, permissions, owner, and modification time:

ls -lS

To reverse the order (smallest first), add the -r flag:

ls -lSr

If you want human-readable sizes (e.g., 1.2M instead of 1234567), add -h:

ls -lSh

Note that ls only lists files in the specified directory, not subdirectories. For a recursive view, you need du or find.

Recursive Listing with du and sort

The du command (disk usage) is designed to sum file sizes. Use -a to show all files, not just directories, and -h for human-readable output. Pipe it to sort -h to sort by human-readable sizes correctly:

du -ah /path/to/start | sort -rh

This lists every file and directory under the given path, sorted by size descending. The -r flag reverses the order, and -h makes sort understand suffixes like K, M, G. If you want only files (not directories), add -f to filter, but note that du does not have a direct file-only flag; you can use find instead.

Using find to List Files by Size

The find command is more flexible when you need to filter by file type, extension, or modification time. To list all regular files sorted by size, you can combine find with ls and sort:

find /path -type f -exec ls -l {} + | sort -k5 -rn

This prints the long listing for each file, then sorts by the 5th field (size in bytes) numerically descending. To get only the file paths and sizes without permissions, use:

find /path -type f -printf '%s %p
' | sort -rn

The %s prints the size in bytes, and %p prints the path. This output is easy to parse and works well with head to see the top 10 largest files:

find /path -type f -printf '%s %p
' | sort -rn | head -10

Human-Readable Sizes and Sorting Pitfalls

When using ls -lS, the sizes are shown in bytes by default. Adding -h makes them human-readable, but then the output is not directly sortable by sort because sort sees strings like "1.2M" and "500K" as text, not numbers. That is why sort -h exists: it understands K, M, G suffixes. However, ls -lS already sorts by size before displaying, so you do not need to pipe it to sort. For du and find, always use sort -h or sort -n with raw byte sizes to get correct ordering.

A common mistake is using sort -n on human-readable output, which will sort lexicographically (e.g., 10M before 2M). Always sort raw numbers or use sort -h.

Sorting by Size in Different Scenarios

Depending on what you need, you might want to sort only certain file types or exclude certain directories. For example, to list the largest log files in /var/log:

find /var/log -type f -name "*.log" -printf '%s %p
' | sort -rn | head

To exclude a directory like /proc or /sys, use the -prune option:

find / -path /proc -prune -o -type f -printf '%s %p
' | sort -rn | head

This prevents find from descending into /proc, which would generate errors and irrelevant data.

Frequently Asked Questions

How do I list files by size in Linux with human-readable sizes?

Use ls -lSh for a single directory, or du -ah /path | sort -rh for a recursive listing. Both show sizes in K, M, G format and sort correctly.

Why does ls -lS not sort subdirectories?

Because ls only lists the contents of the directory you specify, not its subdirectories. To include subdirectories, you need du -a or find, which traverse the directory tree.

What is the difference between du and ls for file sizes?

ls -l shows the logical file size (the number of bytes in the file), while du shows the disk usage, which may be larger due to filesystem block allocation. For most files, they are similar, but for sparse files or filesystems with compression, they can differ.

How do I list the largest files in a directory including hidden files?

By default, ls ignores hidden files (those starting with a dot). To include them, add the -a flag: ls -lSha. For du, hidden files are already included by default.

Can I sort by size in a GUI file manager?

Yes, most Linux file managers (like Nautilus, Dolphin, Thunar) have a "Size" column that you can click to sort. Some also offer a "Sort by Size" option in the view menu, but they may not show subdirectories recursively by default.

Is there a way to sort by size and show only the top N files?

Yes, pipe the output to head. For example: find /path -type f -printf '%s %p ' | sort -rn | head -20 shows the 20 largest files.