Home Beginner's Guide Mastering disk analysis in Linux using the ‘du’ command

Mastering disk analysis in Linux using the ‘du’ command

The 'du' command in Linux is essential for disk usage analysis. This guide explains its use with real-world examples, helping you to effectively manage your file system's space.

by Arun Kumar
du command linux with examples

Among the myriad of commands at your disposal, the du (disk usage) command stands out as a fundamental tool for anyone looking to manage their disk space effectively. Whether you’re a seasoned system administrator, a developer, or just starting your Linux journey, understanding how to leverage the du command is essential. It’s not just about freeing up space; it’s about gaining insights into how your storage is utilized.

In this guide, we’ll embark on a comprehensive exploration of du, from its basic usage to more advanced applications. Let’s dive in and discover how this powerful command can simplify your Linux experience and enhance your file management strategies.

What is the du command?

The du command in Linux is a standard utility used to estimate the file space usage—space used under a particular directory or files on a file system. It’s a powerful tool, especially when you’re managing resources on servers or tracking down space hogs.

Syntax of du

The basic syntax of the du command is as follows:

du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F

Common options

  • -h: Human-readable, shows sizes in KB, MB, GB, etc.
  • -s: Summarize, display only a total for each argument.
  • -a: All, includes files, not just directories.
  • -c: Produce a grand total.
  • --max-depth=N: Shows the directory tree up to ‘N’ levels deep.

Examples

Let’s dive into some examples that show how the du command can be used in real-world scenarios.

Example 1: Basic usage

To get the disk usage of a directory, simply navigate to the directory and type:

du

The output will list the disk usage of the directory and all subdirectories in bytes.

8 ./dir1
16 ./dir2
24 .

Example 2: Human-readable format

For easier understanding, use the -h option:

du -h

This will display the disk usage in a human-readable format (KB, MB, GB).

8.0K ./dir1
16K ./dir2
24K .

Example 3: Summarizing disk usage

If you’re only interested in the total size of a directory, use the -s option:

du -sh

This command will give you the total size of the current directory.

24K .

Example 4: Including all files

To include all files, not just directories, in the disk usage summary, use the -a option:

du -ah

4.0K ./dir1/file1.txt
8.0K ./dir1
12K ./dir2/file2.txt
16K ./dir2
24K .

Example 5: Generating a grand total

If you want to see a grand total of multiple directories or files, use the -c option:

du -ch /dir1 /dir2 /file1

8.0K /dir1
16K /dir2
4.0K /file1
28K total

Example 6: Limiting depth of the directory tree

To limit the depth of the displayed directory tree, use --max-depth=N. For instance, for two levels deep:

du -h --max-depth=2

8.0K ./dir1
16K ./dir2
24K .

Example 7: Checking disk usage of specific files or directories

Sometimes, you might want to check the disk usage of specific files or directories. You can do this by specifying the path:

du -sh /var/log /home/user/Documents

200M /var/log
1.5G /home/user/Documents

This command shows the size of the /var/log directory and the Documents directory in the user’s home folder.

Example 8: Exclude certain files or directories

If you want to exclude specific files or directories from the disk usage report, use the --exclude flag:

du -h --exclude="*.log" /var

This command shows the disk usage of the /var directory but excludes all .log files.

50M /var

Example 9: Compare disk usage before and after an operation

A practical use of du is to compare disk usage before and after a certain operation, such as installation or file creation. First, check the initial usage:

du -sh /some/directory

After completing the operation, run the same command to see the change in disk usage. This can be particularly useful for monitoring the impact of new software installations or large file transfers.

Initial Output:

100M	/some/directory

After some operation, Command:

du -sh /some/directory

After Output:

150M	/some/directory

Example 10: Using du with find command

Combining du with the find command can be powerful. For example, to find the size of all .png files in a directory and its subdirectories:

find /path/to/directory -type f -name "*.png" -exec du -ch {} +

This command lists the size of each .png file individually and then gives a total size at the end.

4.0K /path/to/directory/image1.png
8.0K /path/to/directory/subdir/image2.png
12K total

Example 11: Displaying the largest directories

To display the top 5 largest subdirectories in a directory:

du -hs * | sort -hr | head -n 5

This command is one of my personal favorites for quick disk space diagnostics. It sorts all items in the current directory by size and displays the top 5.

1.5G dir2
500M dir3
400M dir1
300M dir4
200M dir5

Bonus tip: Using du with xargs

For more advanced users, combining du with xargs can be quite efficient, especially when dealing with a large number of files:

find /path/to/directory -type f -name "*.mp4" | xargs du -ch

This command will find all .mp4 files in the specified directory and calculate their total disk usage.

100M /path/to/directory/video1.mp4
150M /path/to/directory/video2.mp4
250M total

My insights and tips

Favorite options

I personally love using du -sh * in a directory to quickly see which subdirectory or file is taking up the most space. It’s a lifesaver when I’m doing a quick cleanup.

Combining with other commands

du becomes even more powerful when combined with other commands. For example, du -sh * | sort -hr sorts the files and directories by size in a human-readable format. It’s like having a bird’s eye view of your disk usage!

A word of caution

Remember, du estimates the file space usage, so the numbers might not exactly match the disk usage reported by other tools that calculate block-level usage.

du command quick reference table

This table serves as a quick guide for readers to understand and remember the key options of the du command. It’s always handy to have such a reference, especially when working on the command line and needing to quickly check the usage of a specific option.

Option Description
-h Display sizes in a human-readable format (e.g., KB, MB, GB).
-s Display only a total for each argument (summarize).
-a Include files, not just directories, in the output.
-c Produce a grand total for all arguments.
--max-depth=N Limit output to N levels of directory depth.
--exclude=PATTERN Exclude files that match PATTERN.
-k Display sizes in kilobytes.
-m Display sizes in megabytes.

Frequently Asked Questions about the du command in Linux

Adding an FAQ section is a great idea to address common queries and concerns about the du command. Here are some frequently asked questions that users often have:

1. What does du stand for in Linux?

Answer: The du in Linux stands for “Disk Usage.” It is used to estimate the file space usage of directories and files on a file system.

2. How do I find the size of a folder in Linux?

Answer: To find the size of a folder, use the du -sh /path/to/folder command. This will display the total size of the folder in a human-readable format.

3. Can du show file sizes in megabytes or gigabytes?

Answer: Yes, use the -h option with du to display sizes in a human-readable format (e.g., KB, MB, GB). For specific units, -k and -m options can be used for kilobytes and megabytes, respectively.

4. How do I see the disk usage of all files and directories in a directory?

Answer: Use du -ah /path/to/directory. This command lists all files and directories along with their disk usage in a human-readable format.

5. Is there a way to exclude certain files or directories when using du?

Answer: Yes, you can exclude files or directories by using the --exclude option, like du -h --exclude="*.log" /path/to/directory.

6. How can I sort the output of du by size?

Answer: To sort the output by size, pipe the du command to sort, like du -sh * | sort -hr. This command will list directories and files sorted by size in a human-readable format.

7. Does du count hidden files?

Answer: Yes, du includes hidden files (those starting with a dot) in its calculation by default.

8. What is the difference between du and df in Linux?

Answer: du calculates the space used by files and directories, whereas df shows the available disk space on different file systems. du gives a detailed analysis at the file and directory level, while df provides a high-level overview of disk usage.

9. How can I limit the depth of directories du reports?

Answer: Use the --max-depth=N option, where N is the level of depth. For example, du -h --max-depth=2 /path/to/directory will show the disk usage up to two levels deep within the directory.

10. Can I use du to monitor disk usage changes over time?

Answer: While du does not track changes over time by itself, you can run it at different intervals and compare the outputs manually. For continuous monitoring, you may need to use additional tools or scripts.

Conclusion

Throughout our exploration of the du command in Linux, we’ve seen its versatility and importance in managing disk space effectively. From basic syntax to advanced usage examples, du proves to be an invaluable tool for anyone navigating the Linux environment. Whether you’re freeing up disk space, monitoring storage usage, or conducting system maintenance, mastering du enhances your command-line proficiency. Remember, the key is to experiment and integrate these commands into your daily tasks, allowing you to handle disk space management with confidence and ease. Embrace the power of du and make it a staple in your Linux toolkit!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.