Sort Directories Actual Size Linux
To sort directories by their actual size on Linux, use du -h --max-depth=1 | sort -h from the parent directory. This shows the real disk usage of each subdirectory (including all files within it) and sorts them from smallest to largest. For a more common largest-first view, add -r to the sort command.
What “Actual Size” Means in Linux
When you run ls -l, the sizes you see are the sizes of individual files as reported by the filesystem metadata. For directories, ls shows the size of the directory entry itself (usually 4 KB), not the sum of everything inside. The du (disk usage) command walks the entire directory tree and adds up the actual bytes used by every file and subdirectory. This is what most people mean when they ask for the “real” or “actual” size of a directory.
There is a subtle difference between “apparent size” and “disk usage” that often confuses users:
- Apparent size (
du --apparent-size): the sum of the file lengths as reported byls. This is the logical size of the files. - Disk usage (default
du): the actual blocks allocated on the disk. Because files are stored in blocks (typically 4 KB), a 1-byte file still takes 4 KB of disk space. Sparse files and compressed filesystems make the two numbers diverge even more.
For most purposes, the default du output is what you want because it reflects how much space is actually being consumed on your drive.
Basic Commands to Sort Directories by Size
The most straightforward approach combines du with sort. Here are the common variations:
- Human-readable sizes, smallest first:
du -h --max-depth=1 | sort -h - Human-readable sizes, largest first:
du -h --max-depth=1 | sort -hr - Exact bytes, largest first:
du --max-depth=1 | sort -nr - Include hidden directories (those starting with a dot):
du -h --max-depth=1 .[!.]* * | sort -hr
In these commands, --max-depth=1 tells du to show only the immediate subdirectories of the current directory, not to recurse endlessly. Without it, du would print every subdirectory at every level, which is usually too much output. The -h flag makes the sizes human-readable (e.g., 4.2G, 300M). The sort -h flag is essential when you use -h: it understands human-readable suffixes (K, M, G) and sorts numerically rather than alphabetically. If you used sort -n with -h, you would get wrong results because sort -n only understands pure numbers.
Using du Effectively: Options and Examples
The du command has several options that change its behavior. Here are the ones most relevant to sorting directories by size:
-h, --human-readable: prints sizes in a format like 4.2G, 300M, 12K. This makes output easier to read but requiressort -hfor correct ordering.-mor-k: prints sizes in megabytes or kilobytes respectively. These are plain numbers, so you can usesort -nwith them.--max-depth=N: limits the depth of recursion.--max-depth=1shows only the subdirectories of the current directory.--max-depth=0shows only the total for the current directory.-s, --summarize: shows only the total for each argument, which is equivalent to--max-depth=0when used with a single directory.-x, --one-file-system: skips directories that are on a different filesystem (e.g., mounted network drives or other partitions). This is useful if you only want to measure the current filesystem.-d N: a shorthand for--max-depth=N(GNU coreutils).
Here is a practical example. Suppose you want to find the largest directories in your home folder:
cd ~
du -h --max-depth=1 | sort -hr | head -20
This will list the 20 largest subdirectories in your home, from biggest to smallest. The head -20 limits the output, which is helpful when you have many directories.
Common Pitfalls and How to Avoid Them
Several mistakes can lead to incorrect or confusing results when sorting directories by size:
- Using
sort -nwith-houtput: As mentioned,sort -ntreats “4.2G” as a string and fails to order it correctly. Always usesort -hwhen you usedu -h. - Forgetting
--max-depth: Without it,duprints every subdirectory recursively, creating a huge unsorted list. You usually want--max-depth=1to see only the immediate children. - Missing hidden directories: By default,
dudoes not include hidden files and directories (those starting with a dot). If you want to include them, you need to specify them explicitly, for example withdu -h --max-depth=1 .[!.]* *. Note that this pattern may miss some entries, so a more robust way is to usedu -h --max-depth=1 .and then filter out the total line. - Hard links counted multiple times: If the same file is hard-linked in multiple directories,
ducounts the file once per directory by default. Use-lto count hard links multiple times (which is the default fordu? Actually, the default is to count each hard link as a separate file, but with-lyou count them multiple times; the default is to count them once? Let’s clarify: The default behavior ofduis to count a hard-linked file only once, even if it appears in multiple directories. If you want to count it multiple times, use the-loption. This is rarely an issue for typical use. - Filesystem overhead: The size reported by
duis the actual allocated space, which may be larger than the sum of file sizes due to block alignment. This is normal and not an error.
Alternative Tools and Commands
While du + sort is the classic command-line approach, there are other tools that provide a more visual or interactive way to see directory sizes:
- ncdu: A terminal-based disk usage analyzer. It scans a directory and presents an interactive, sortable list. You can navigate into directories and delete files directly from the interface. Install it with your package manager (e.g.,
sudo apt install ncduon Debian/Ubuntu,sudo dnf install ncduon Fedora). - dua: A modern Rust-based alternative to ncdu, with a more polished interface.
- gdmap or Filelight: Graphical tools that show directory sizes as a treemap or pie chart. They are available in most Linux distributions.
- baobab: The default disk usage analyzer on GNOME desktops. It provides a graphical representation and can scan remote locations.
These tools are especially useful when you want to explore interactively rather than just get a sorted list.
Sorting by Size in a Script or for Monitoring
If you need to sort directories by size regularly, you can create a simple alias or a shell function. For example, add this to your ~/.bashrc:
alias dus='du -h --max-depth=1 | sort -hr'
Then you can just type dus in any directory to see its subdirectories sorted by size. For more advanced monitoring, you could write a script that runs du on a specific directory and logs the results to a file. For instance, to find which subdirectory of /var is growing the most, you could run:
du -h --max-depth=1 /var | sort -hr
This is a common first step in troubleshooting disk space issues. Remember that du can take a while on large filesystems because it has to read every directory entry. On very large servers, you might want to use --max-depth=1 to avoid scanning the entire tree, or use --exclude to skip certain directories (e.g., --exclude=/proc).
Frequently Asked Questions
Why does du show a different size than ls?
ls shows the logical size of files, while du shows the actual disk space used. Because files are stored in blocks (usually 4 KB), a file that is 1 byte long still occupies 4 KB on disk. Additionally, du includes the space used by directory entries and metadata, while ls only reports the file length. Sparse files, compression, and filesystem features like reflinks can also cause differences.
How do I sort directories by size including hidden ones?
By default, du does not include hidden files and directories (those starting with a dot). To include them, you can use a glob pattern that matches them, like du -h --max-depth=1 .[!.]* *. However, this pattern might miss some edge cases. A more reliable method is to run du -h --max-depth=1 . and then remove the last line (which is the total) from the output. For example: du -h --max-depth=1 . | sort -hr | head -n -1.
What is the difference between du -h and du --apparent-size?
du -h (without --apparent-size) reports the actual disk usage, which is the number of blocks allocated on the filesystem. du --apparent-size reports the apparent size, which is the sum of the file lengths as reported by ls. The apparent size is usually smaller because it does not account for block alignment or filesystem metadata. For sparse files, the apparent size can be much larger than the actual disk usage.
Why does sort -h not work on some systems?
The -h option for sort is part of GNU coreutils. On BSD or macOS systems, the default sort may not support it. If you get an error, you can use gsort (if GNU coreutils is installed) or use a different approach: run du -k to get sizes in kilobytes and then sort numerically with sort -n. For example: du -k --max-depth=1 | sort -nr.
How can I see the total size of all directories combined?
To see the total size of the current directory and all its contents, just run du -h without --max-depth. The last line of the output shows the total. Alternatively, use du -sh (where -s stands for summarize) to get only the total. For example: du -sh /home/user.
Is there a way to sort directories by size in real time?
There is no built-in real-time monitoring, but you can use tools like watch to run the command periodically. For example: watch -n 5 'du -h --max-depth=1 | sort -hr' will update the list every 5 seconds. For a more graphical real-time view, tools like ncdu can refresh the display after a rescan, but they do not continuously monitor by default.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.