When managing a Linux server or desktop, you will inevitably run into “disk space full” warnings. While the standard ls command is great for listing files, it only shows the size of individual files, not the total size of the folders containing them. To find out exactly how much disk space a specific folder is consuming, you must use the disk usage (du) command.
The Basic Command: du -sh
The fastest and most common way to check the size of a single directory is to use the du command paired with two essential flags: -s (summarize) and -h (human-readable).
Open your terminal and type:
du -sh /var/log
What this does:
-s(Summarize): Prevents the terminal from printing the size of every single individual file inside the folder. It only outputs the grand total.-h(Human-readable): Converts the output from raw bytes into Megabytes (M) or Gigabytes (G), so you see “2.4G” instead of “2516582400”.
How to Find the Largest Sub-folders
If you know your “Downloads” folder is huge, but you don’t know which folder inside it is causing the problem, you need to check the size of all immediate sub-directories.
Use this command, adding an asterisk (*) to the end of the path:
du -sh ~/Downloads/*
This will print a list of every folder inside your Downloads directory, showing the total size next to each name (e.g., “4.1G Movies”, “12M Documents”).
How to Sort the Output by Size
If a directory contains hundreds of sub-folders, reading through the list to find the biggest one is tedious. You can pipe the output into the sort command to automatically arrange the folders from smallest to largest.
du -sh /var/* | sort -h
What this does:
- The
|symbol (pipe) takes the output of theducommand and hands it to thesortcommand. - The
-hflag on the sort command tells it to understand human-readable numbers (so it knows that 2G is larger than 900M).
The largest directories will now be neatly printed at the very bottom of your terminal screen.
Conclusion
The du -sh command is an indispensable tool in the Linux administrator’s toolkit. Memorizing it will save you hours of hunting when trying to free up valuable disk space on a crowded server.