Open In App

du command in Linux with examples

Last Updated : 27 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The `du` command in Linux is a powerful utility that allows users to analyze and report on disk usage within directories and files. Whether you’re trying to identify space-hogging directories, manage disk space efficiently, or simply gain insights into storage consumption, the du command provides valuable information. In this article, we’ll explore the du command, its basic syntax, and various examples to demonstrate its functionality.

Syntax of `du` command in Linux

The basic syntax of the du command is as follows:

du [options] [directory/file]

Here,

[options] = various flags that modify the behavior of the command

[directory/file] = the target directory or file for which disk usage information is desired. If no directory or file is specified, du will default to the current directory.

Example:

du /home/mandeep/test

Output:

44    /home/mandeep/test/data
2012    /home/mandeep/test/system design
24    /home/mandeep/test/table/sample_table/tree
28    /home/mandeep/test/table/sample_table
32    /home/mandeep/test/table
100104    /home/mandeep/test

Options available in `du` command in Linux

The du command offers a range of options that enhance its functionality. Let’s explore some commonly used options:

Options

Describe

-0, –null

end each output line with NULL

-a or –all

Displays disk usage information for all files and directories, including hidden ones.

–apparent-size

print apparent sizes, rather than disk usage

-B, –block-size=SIZE

scale sizes to SIZE before printing on console

-c or –total

Shows the total disk usage in addition to individual usage for directories and files.

-d, –max-depth=N

print total for directory only if it is N or fewer levels below command line argument

-h or –human-readable

Displays sizes in human-readable format, using units such as KB, MB, GB, etc. This option makes it easier to interpret the disk usage information.

-S, -separate-dirs

for directories, don’t include size of subdirectories

-s or –summarize

Provides a summary of the disk usage for the specified directory or file, without displaying individual usage details for subdirectories.

–time

show time of last modification of any file or directory

–exclude

Excludes specific directories or files from disk usage calculation based on patterns or names.

Pratical Example of du Command

1. `-h` option in `du` command

If we want to print sizes in human readable format(K, M, G), use -h option

du -h /home/Mandeep/test 

Output:

44K    /home/mandeep/test/data
2.0M    /home/mandeep/test/system design
24K    /home/mandeep/test/table/sample_table/tree
28K    /home/mandeep/test/table/sample_table
32K    /home/mandeep/test/table
98M    /home/mandeep/test

44K    /home/mandeep/test/data
2.0M    /home/mandeep/test/system design
24K    /home/mandeep/test/table/sample_table/tree
28K    /home/mandeep/test/table/sample_table
32K    /home/mandeep/test/table
98M    /home/mandeep/test

2. To display all files, including directories, with their sizes

Use -a option for printing all files including directories.

du -a -h /home/mandeep/test

Output: This is partial output of above command.

4.0K    /home/mandeep/test/blah1-new
4.0K    /home/mandeep/test/fbtest.py
8.0K    /home/mandeep/test/data/4.txt
4.0K    /home/mandeep/test/data/7.txt
4.0K    /home/mandeep/test/data/1.txt
4.0K    /home/mandeep/test/data/3.txt
4.0K    /home/mandeep/test/data/6.txt
4.0K    /home/mandeep/test/data/2.txt
4.0K    /home/mandeep/test/data/8.txt
8.0K    /home/mandeep/test/data/5.txt
44K    /home/mandeep/test/data
4.0K    /home/mandeep/test/notifier.py

3. To calculate the total size of a directory and its subdirectories

Use -c option to print total size

du -c -h /home/mandeep/test

Output:

44K    /home/mandeep/test/data
2.0M    /home/mandeep/test/system design
24K    /home/mandeep/test/table/sample_table/tree
28K    /home/mandeep/test/table/sample_table
32K    /home/mandeep/test/table
98M    /home/mandeep/test
98M    total

`-d` option in `du` command

To print sizes to particular level, use -d option with level no.

du -d 1 /home/mandeep/test

Output:

44    /home/mandeep/test/data
2012    /home/mandeep/test/system design
32    /home/mandeep/test/table
100104    /home/mandeep/test

Now try with level 2, you will get some extra directories

du -d 2 /home/mandeep/test

Output:

44    /home/mandeep/test/data
2012    /home/mandeep/test/system design
28    /home/mandeep/test/table/sample_table
32    /home/mandeep/test/table
100104    /home/mandeep/test

4. To obtain the disk usage summary for a directory

Get summary of file system using -s option

du -s /home/mandeep/test

Output:

100104    /home/mandeep/test

5. To view the timestamp of the last modification of files and directories

Get the timestamp of last modified using –time option

du --time -h /home/mandeep/test

Output:

44K    2018-01-14 22:22    /home/mandeep/test/data
2.0M    2017-12-24 23:06    /home/mandeep/test/system design
24K    2017-12-30 10:20    /home/mandeep/test/table/sample_table/tree
28K    2017-12-30 10:20    /home/mandeep/test/table/sample_table
32K    2017-12-30 10:20    /home/mandeep/test/table
98M    2018-02-02 17:32    /home/mandeep/test

Conclusion

In this article we have discussed the `du` command in Linux which provides us essential insights into file and directory space usage, enabling users to effectively manage their storage. We also discussed the variety of options available. Overall, we can say that mastering `du` command and its options, a user can efficiently analyze disk usage, identify storage areas, and optimize their system’s storage utilization.

du command in Linux with examples – FAQs

What is the du command in Linux?

The du (Disk Usage) command is used to estimate and display the disk space used by files and directories. It provides insights into the size of files and directories in a filesystem.

How do I check the size of a directory in Linux using du?

Use the command: du -sh /path/to/directory

How can I list the sizes of all subdirectories in a directory?

To list the sizes of all subdirectories in a directory, run: du -h /path/to/directory

How do I exclude certain files or directories while using du?

Use the --exclude option: du -h –exclude=”*.log” /path/to/directory
This excludes files matching the pattern *.log.



Next Article
Article Tags :

Similar Reads