du Command in Linux
When a Linux server starts running out of storage, it is important to identify which files or directories are consuming the most space. The du command is a simple command-line utility that helps you analyze disk usage.
Unlike df, which shows filesystem-level available and used space, du is useful for examining individual directories and files.
What Is the du Command?
du stands for disk usage.
The basic command is:
du
It displays the disk usage of the current directory and its subdirectories.
Use du -h for Readable Output
The -h option displays disk usage in a human-readable format.
du -h
Instead of displaying difficult-to-read values, it shows sizes using units such as KB, MB, or GB.
Check a Specific Directory
You can check the usage of a particular directory:
du -h /var/log
This can be useful when investigating directories that may contain large log files.
Show Total Directory Usage
To display only the total size of a directory, use:
du -sh /var/log
Here:
-s shows only the total.
-h displays the result in a human-readable format.
This is one of the most useful du commands for everyday Linux administration.
Find Large Directories
You can combine du with other Linux commands to identify directories that are consuming significant storage.
For example:
du -h --max-depth=1 /var
This provides a summary of directories at the selected depth and can help narrow down where disk space is being used.
du vs df
These commands are often used together, but they have different purposes.
df shows available and used space at the filesystem level.
du helps identify how much space particular files and directories are consuming.
For example, you might first run:
df -h
and then investigate a full filesystem using:
du -sh /var/*
Why Is du Useful?
The du command is commonly used for:
- Finding large directories.
- Investigating disk-space problems.
- Monitoring storage consumption.
- Cleaning unnecessary files.
- Managing Linux servers.
Best Practices
Use du regularly on systems that generate large logs, store uploads, or process significant amounts of data.
Before deleting large files, always confirm what they are used for. Removing important application or system files can cause unexpected problems.
Conclusion
The Linux du command is a simple and effective way to analyze disk usage. While df tells you how much storage is available, du helps you determine where that storage is being used.
Commands such as du -h and du -sh are particularly useful for everyday server monitoring and troubleshooting.