...

Understanding the du Command in Linux: A Comprehensive Guide with 10 Practical Examples

Rate this post

Introduction

The du (Disk Usage) command in Linux is a standard Unix/Linux utility used to estimate file space usage—space used under a particular directory or files on a file system. It’s an essential tool for system administrators and users who need to monitor and manage disk space.

In this post, we’ll explore the du command in detail, covering its syntax, options, and practical examples to help you make the most of this powerful utility.

Prerequisites


Table of Contents

  1. What is the du Command?
  2. Basic Syntax
  3. Commonly Used Options
  4. Practical Examples
  5. Tips and Best Practices
  6. Conclusion
  7. References

What is the du Command in Linux?

The du command stands for Disk Usage. It allows you to check the size of files and directories on your system. By default, du summarizes disk usage for the specified files or directories and for each subdirectory.

Key Features:

  • Provides disk usage information.
  • Can display sizes in different units (bytes, kilobytes, megabytes, etc.).
  • Offers options to include or exclude certain files or directories.
  • Useful for identifying large files or directories consuming disk space.

Basic Syntax

du [OPTIONS] [FILE/DIRECTORY]

  • [OPTIONS]: Command-line options to modify the behavior of du.
  • [FILE/DIRECTORY]: The file or directory to analyze. If none is specified, du uses the current directory.
du command in Linux

Commonly Used Options

OptionDescription
-a, --allDisplays disk usage for all files, not just directories.
-h, --human-readableDisplays sizes in human-readable format (e.g., 1K, 234M, 2G).
-s, --summarizeDisplays only the total size of the specified files or directories.
-c, --totalDisplays a grand total at the end of the output.
-kDisplays sizes in kilobytes (default is bytes).
-mDisplays sizes in megabytes.
--exclude=[PATTERN]Excludes files or directories that match the specified pattern.
-d [DEPTH], --max-depth=[DEPTH]Limits the display of directory levels to the specified depth.
-L, --dereferenceFollows symbolic links.
--timeDisplays the last modification time of the files or directories.
--time-style=[STYLE]Specifies the format of the time display.
--apparent-sizePrints the apparent sizes, rather than disk usage.
-b, --bytesDisplays sizes in bytes.
-P, --no-dereferenceDoes not follow symbolic links (default behavior).
options for du command in Linux

Practical Examples

Let’s dive into some practical examples to understand how to use the du command effectively.

Example 1: Basic Usage

Command:

du

Description:

  • Displays the disk usage of the current directory and each of its subdirectories.
  • Output is in kilobytes by default.

Sample Output:

8	./linux-commands/grep
12	./linux-commands
40	.

Example 2: Human-Readable Output

Command:

du -h

Description:

  • Displays size in a human-readable format (K, M, G). Note that the default unit is kilobyte.

Sample Output:

8.0K	./linux-commands/grep
12K	./linux-commands
40K	.


Example 3: Display Total Disk Usage

Command:

du -sh

Description:

  • -s: Summarizes the total disk usage.
  • -h: Human-readable format.

Sample Output:

40K	.

Example 4: Summarize Disk Usage of a Directory

Command:

du -sh /var/log

Description:

  • Displays the total disk usage of the /var/log directory.

Sample Output:

227M /var/log

Example 5: Display Disk Usage of All Files and Directories

Command:

du -ah

Description:

  • -a: Includes all files, not just directories.
  • -h: Human-readable format.

Sample Output:

4.0K	./.viminfo
4.0K	./.bashrc
4.0K	./.profile
4.0K	./.bash_history
4.0K	./.lesshst
4.0K	./linux-commands/grep/sample1.txt
8.0K	./linux-commands/grep
12K	./linux-commands
0	./.sudo_as_admin_successful
4.0K	./.bash_logout
40K	.

Example 6: Exclude Specific Files or Directories

Command:

du -ah --exclude="*.txt"

Description:

  • Excludes all files ending with .txt from the disk usage calculation.

Sample output

4.0K	./.viminfo
4.0K	./.bashrc
4.0K	./.profile
4.0K	./.bash_history
4.0K	./.lesshst
4.0K	./linux-commands/grep
8.0K	./linux-commands
0	./.sudo_as_admin_successful
4.0K	./.bash_logout
36K	.

Note that after excluding txt files we don’t have “4.0K ./linux-commands/grep/sample1.txt” in output above.


Example 7: Display Disk Usage in a Specified Unit

Command:

du -m

Description:

  • Displays sizes in megabytes.

Sample Output:

1 /var/log/apt
17 /var/log/mongodb
1 /var/log/unattended-upgrades
1 /var/log/private
6 /var/log/sysstat
1 /var/log/supervisor
1 /var/log/dist-upgrade
1 /var/log/landscape
201 /var/log/journal/787e210ab71b4bdfb3369945301fd2c8
201 /var/log/journal
1 /var/log/apache2
1 /var/log/chrony
1 /var/log/nginx
1 /var/log/letsencrypt
227 /var/log


Example 8: Sorting Output

Command:

du -ah | sort -h

Description:

  • Sorts the output by size in a human-readable format.

Sample output

0	./.sudo_as_admin_successful
4.0K	./.bash_history
4.0K	./.bash_logout
4.0K	./.bashrc
4.0K	./.lesshst
4.0K	./.profile
4.0K	./.viminfo
4.0K	./linux-commands/grep/sample1.txt
8.0K	./linux-commands/grep
12K	./linux-commands
40K	.

Example 9: Display Disk Usage of a File

Command:

du -h file.txt

Description:

  • Displays the disk usage of a specific file.

Sample output

4.0K	./linux-commands/grep/sample1.txt

Example 10: Combine Multiple Options

Command:

du -ah --max-depth=1

Description:

  • -a: Includes files.
  • -h: Human-readable format.
  • --max-depth=1: Limits the depth to the current directory.

Sample output

4.0K	./.viminfo
4.0K	./.bashrc
4.0K	./.profile
4.0K	./.bash_history
4.0K	./.lesshst
12K	./linux-commands
0	./.sudo_as_admin_successful
4.0K	./.bash_logout
40K	.

Tips and Best Practices

  • Regular Monitoring: Use du regularly to monitor disk usage and identify large files or directories.
  • Automate Reports: Combine du with other commands like cron to automate disk usage reporting.
  • Use Exclude Option: Exclude unnecessary files or directories (e.g., cache directories) to focus on relevant data.
  • Combine with df: Use du along with df (disk free) to get a complete picture of disk usage and available space.

Conclusion

The du command in Linux is a versatile tool that provides valuable insights into disk usage on your Linux system. Whether you’re a system administrator managing servers or a casual user keeping an eye on your personal computer’s storage, understanding how to use du effectively can help you maintain optimal system performance.

By mastering the various options and combining them as needed, you can tailor the du command to suit a wide range of disk usage analysis tasks.
Let me know your suggestions and which examples you liked most.


References

Spread the love

Leave a Comment

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.