0
0
Linux CLIscripting~5 mins

du (disk usage by directory) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your computer's storage gets full and you want to find out which folders use the most space. The du command helps you see how much disk space each directory and its contents take up.
When you want to check which folders are using the most space on your hard drive.
When you need to clean up space by deleting large unused directories.
When you want to monitor disk usage growth over time in specific folders.
When you are managing a server and want to avoid running out of disk space.
When you want a quick summary of disk usage for a directory and its subdirectories.
Commands
This command shows the disk usage of the /home/user/documents directory and all its subdirectories in a human-readable format (like KB, MB).
Terminal
du -h /home/user/documents
Expected OutputExpected
4.0K /home/user/documents/notes 12M /home/user/documents/photos 16M /home/user/documents
-h - Shows sizes in human-readable format (KB, MB, GB)
This command shows only the total disk usage of the /home/user/downloads directory in a human-readable format, without listing subdirectories.
Terminal
du -sh /home/user/downloads
Expected OutputExpected
250M /home/user/downloads
-s - Summarizes total size without showing subdirectories
-h - Shows sizes in human-readable format
This command shows disk usage for /var/log and its immediate subdirectories only, making it easier to see which subfolders use the most space.
Terminal
du -h --max-depth=1 /var/log
Expected OutputExpected
20M /var/log/apache2 5.0M /var/log/mysql 25M /var/log
--max-depth=1 - Limits output to the specified directory and its direct children
-h - Shows sizes in human-readable format
Key Concept

If you remember nothing else from this pattern, remember: du helps you find out how much space directories use so you can manage your disk storage better.

Common Mistakes
Running du without -h and seeing large numbers in bytes that are hard to read.
The output is difficult to understand because sizes are shown in bytes without units.
Always use the -h flag to get human-readable sizes like KB, MB, or GB.
Running du without -s and getting a long list of every subdirectory when only total size is needed.
The output becomes cluttered and hard to find the total size quickly.
Use the -s flag to get a summary total size for the directory.
Not using --max-depth and getting too much detail when only top-level folder sizes are needed.
The output is overwhelming and hard to interpret for large directory trees.
Use --max-depth=1 to limit output to the directory and its immediate subfolders.
Summary
Use du -h to see disk usage in a readable format for directories and subdirectories.
Use du -sh to get a quick total size of a directory without details.
Use du -h --max-depth=1 to see sizes of a directory and its immediate subfolders only.