The Wayback Machine - https://web.archive.org/web/20240620092231/https://www.geeksforgeeks.org/du-command-linux-examples/
Open In App

du command in Linux with examples

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
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

Frequently Asked Question

1. What is the du command in Linux, and how does it differ from the df command?

The du command in Linux is used to estimate file space usage. It displays the sizes of directories and files, helping users identify which directories are consuming the most disk space. Unlike df, which shows information about mounted file systems, du focuses on individual files and directories within those file systems.

2. How can I use the du command to find the largest directories on my system?

To find the largest directories, you can use the du command with the --max-depth option.

For example:

du --max-depth=1 /path

It will display the sizes of the immediate subdirectories under the specified path.

3. Is there a way to display disk usage in a human-readable format with the du command?

Yes, the du command supports the -h option to display sizes in a human-readable format (e.g., KB, MB, GB).

For instance:

du -h /path

It will provide a more readable output.

4. Can I use the du command to find large files within a directory?

Absolutely. You can combine the `find` command with du to identify large files.

For example:

find /path -type f -exec du -h {} + | sort -rh | head -n 10

It will list the top 10 largest files under the specified path.

5. How do I exclude certain directories or files from the du command output?

To exclude specific directories, use the --exclude option with the du command.

For example:

du -h --exclude=/path/to/exclude /path

It will display disk usage for the specified path while excluding the specified directory.

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.



Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
ZIP command in Linux with examples
ZIP is a compression and file packaging utility for Unix. Each file is stored in a single .zip {.zip-filename} file with the extension .zip. Zip is used to compress files to reduce file size and is also used as a file package utility. Zip is available in many operating systems like Unix, Linux, windows, etc.If you have limited bandwidth between two
7 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read
tee command in Linux with examples
tee command reads the standard input and writes it to both the standard output and one or more files. The command is named after the T-splitter used in plumbing. It basically breaks the output of a program so that it can be both displayed and saved in a file. It does both the tasks simultaneously, copies the result into the specified files or varia
2 min read
Article Tags :