Getty Images/iStockphoto

Manage Linux storage space with du and df commands

The du and df commands detailed here enable sys admins to review and understand the storage capabilities of their on-premises or cloud-based servers, whether physical or virtual.

Has a single concern ever been as much of a hassle for sys admins as storage space? It seems there's always resources and attention being dedicated to managing storage capacity, whether it's exploding log files, package manager caches, unused VM images or malware.

Plenty of approaches can help. You might implement disk quotas for users or periodically clear package manager caches, but the first step is finding the culprit. Linux contains two crucial tools to help you investigate your storage issues: df and du.

The df and du utilities are certainly not the only information-gathering tools Linux contains for managing storage space. You might also use the lsblk, fdisk, mount and cat /proc/partitions commands to display disk information. And you could certainly combine any number of these for more comprehensive data. But, for this article's purposes, there's enough to consider with df and du commands to manage Linux storage space.

Show available space with df

Use the df (disk free) command to display available Linux storage space. If you don't provide any arguments, it shows the free capacity for all mounted file systems.

The first thing most users notice when running df is the awkward display of capacity units. By default, df shows available space in bytes -- not a particularly useful result. It shows this information for the total storage capacity, amount consumed and amount available.

Screenshot of df command
The df command with no options displays storage space in bytes.

Try running df with the -h option instead. This flag displays the results in a human-readable format. In other words, it uses standard increments, like KB, MB and GB, depending on the quantity of space. That information is much easier to deal with.

Screenshot of df -h command
Use the df -h command to add storage space increments.

That's not the only useful option for df. You might also add the -T option to display file system types in the output. If you need inode information, attach the -i option. And -a shows all file systems, including those with no available Linux storage space.

Screenshot of df -T command
The df -T command displays the file system type in the second column.

The df command is a great auditing tool. Some sys admins create basic Bash scripts that regularly gather system information and report it to a central source or email the results to the administrator. Such auditing can be useful, and the df command is probably the first storage reporting tool you add to such a script.

The df command provides an effective, high-level, systemwide overview of file system capacity information. You can also target specific partitions to narrow its scope.

Screenshot of df command's output
Narrow the scope of the df command's output to specific storage spaces.

Alias the df command

Edit your ~/.bashrc file with an alias that customizes df to your specific needs. The obvious example is causing df to show the -h human-readable format by default.

The basic alias looks like this.

alias df='df -h'

However, the -T file system output may also be useful. That alias looks like this.

alias df='df -hT'

When you type in the df command, it automatically adds the specified options, saving you time and providing you with more complete information. Note that you may need to reload the file after editing it by running the source ~/.bashrc command.

Show consumed Linux storage space with du

What if your investigation takes you deeper into the file system? Maybe you're looking at which user's home directory is consuming so much space, or you need to know which log file is out of control in the /var/log directory. Use the du command to find storage utilization information at the file and directory level.

Like df, the default output of du shows information in bytes. Add the same -h option you used with df to cause du to display more logical sizes.

Screenshot of -h option added to du command
Adding the -h option to the du command displays size increments, as seen in the second command example.

The du command provides detailed information at the file and directory level. Use it when investigating storage issues related to specific files or locations.

As with other Linux commands, a wide range of options tailors the du command's output. Use the -c flag to display total storage utilization for the directories you're examining. The --total flag does the same.

The other helpful option is -s, which summarizes the results for the targeted directory instead of showing sizes on a per-file basis. This is a good trick for gathering general storage information.

Screenshot of du -sh command
Rather than displaying a result for each directory and file, the du -sh command displays a summary total.

Sort the results

The du command does not include its own sorting functionality. However, you can still organize the results into a list from largest to smallest or smallest to largest using other commands. Use the pipe character to redirect the du command output into the sort command.

Use the -r option with sort to display the results from the largest file to the smallest. If you want to get fancy, pipe that result into the head -n 5 command for a list of the five largest files or directories in the sorted output.

The final command looks like this.

du -h /var/log | sort -r | head -n 5
Screenshot of du command with tools
Combine the du command with tools like sort and head to gather exactly the information you need.

Alias the du command

The du command is another good candidate for an alias. You almost certainly want to add the -h option, and you should consider using the -c (total) option, too. You could always create a second alias that handles the sorting and top five list.

The basic output alias with human-readable format and a total looks like this.

alias du='du -ch'

Create similar results sorted for the five largest results and tied to the du5 command like this.

alias du5='du -h | sort -r | head -n 5'

Place these in your ~/.bashrc file along with the df command alias you created above. Don't forget to run source ~/.bashrc.

Screenshot of aliasing commands
Aliasing these commands is a simple way to enhance their functionality each time you use them.

But wait, there's more

The df and du commands work on physical and virtual servers, whether hosted on-premises or in the cloud. Since auditing a system's storage capacity is a critical task, you may use these commands frequently.

You also get great use out of both commands when troubleshooting. Remember to combine them with other tools to narrow down the scope of the system's issues. You might use tools like top for overall performance information, free and vmstat for memory use, and ps and lsof to understand running processes.

Begin experimenting today with the df and du commands. Once you're comfortable with the type of information they provide, create aliases of each that at least display the results in a human-readable format. From there, consider integrating them into a larger server auditing script. Be sure to make them part of your regular troubleshooting routine, too.

Damon Garn owns Cogspinner Coaction and provides freelance IT writing and editing services. He has written multiple CompTIA study guides, including the Linux+, Cloud Essentials+ and Server+ guides, and contributes extensively to Informa TechTarget, The New Stack and CompTIA Blogs.

Dig Deeper on Storage management and analytics