How to use the du Command in Linux

The Du Comman

In this tutorial, we’ll go over the du command in Linux. “du” stands for disk usage and is used to estimate the amount of disk space used by a given file or directory. In general, it is a handy utility if you want to print out the sizes of all of the files and folders within a certain directory root.

How to use the du command?

The general syntax of the du command is as:

du [OPTIONS] [FILE or DIRECTORY NAME]

If the name of the file or directory is not mentioned then it will display disk usage of your current working directory. When the du command is executed without any option it will display the disk usage by the current directory as well as its subdirectories in kilobytes. You can pass multiple files or multiple directories.

Options with the du command:

  • -0: end each output line with null
  • -a: write count of all file
  • -b: scale size to SIZE in output
  • -c: gives total size occupied
  • -h: print sizes of disk occupied in a human-readable format
  • -S: it will not include the subdirectories
  • -s: only display total space occupied by each directory
  • -time: it shows the time of last modification of any file or directory
  • –exclude: it excludes the file or directory matches the pattern

Default du Command Usage

By default, if you type du and hit the enter key, this is the kind of output that you will see.

du
Du Command In Linux Default
Du Command In Linux Default

View du Data in Human-Readable Format

The general output of the du command is in kilobytes. To view the space in a human-readable format i.e. in terms of kb, Mb, Gb we use option -h along with the du command. The output as follows:

du -h
Human Readable Du
Human Readable Du

Great! Now the file sizes are human-readable.

View Directory Size Summary

As you may have seen the du command in Linux outputs all the sizes of all the files. But if all you want to see is the summarized output, then you can use the -s option which stands for a summary. I’m also combining it with the -h option to view human-readable info.

du -s -h <directory>
Du View Summary
Du View Summary

This is how we can skip the individual file listing and only display the total size of the directory.

Specify Directory Depth with the du Command

You can use the -d option with the du command in Linux to print sizes until a given level. Enter the level number to view the files in that appropriate level. In simple words, the -d parameter, specify the depth of how deep should the command check for files.

By defining level 1, it will investigate 1 directory deep and level 2, will analyze 2 directories deep and so on.

du -d 1 <directory>
Du Directory Depth
Du Directory Depth

Print Modification Time

The –time parameter is often utilized with the du command in Linux by programmers to see the time when the files were last modified. It displays the space occupied based on the modification of time. Use the flag “–time” as given below:

du --time <directory>
Du Time Setting
Du Time Setting

Have a look at how the modification time for each individual file is displayed in the output.

Exclude Files With the du Command in Linux

Now if you want to exclude specific directories or files, you can use the –exclude option with the du command in Linux as shown below.

du -d 1 --exclude=wordpress /root
Du Exclude
Du Exclude

Notice how excluding the WordPress folder now skips displaying it in the output when we run the command the second time.

Display Size in Specified Units

We saw how the du command in Linux displays the size in kilobytes or automatically displays them based on human-readability. But if you want the size in specific sizes, you can set it up to show the data in megabytes or kilobytes with the -m or -k options.

du -m <directory>
du -k <directory>
Du Display Size
Du Display Size

As you can see, the -k option provides us the information in kilobytes which is similar o the information that we had before without the use of the -k option too.

Conclusion

With now you should have a good perception of how to use the du command. You can inspect all possible du command options by entering man du in your terminal. Unlike df command which prints information about the disk usage of the mounted file system, the du command gives you a guesstimate of disk space used by named files or directories.

The du command in Linux can be utilized to trace the files and directories that are consuming unnecessary amounts of space on the hard disk drive.

What is the du command in Linux used for?

The du command in Linux is used to estimate file space usage, showing the disk usage of files and directories.

How can I use the du command to display disk usage in human-readable format?

You can use the “du -h” command in Linux to display disk usage in a human-readable format, showing sizes in bytes, kilobytes, megabytes, etc.

How to use the du command to see disk usage of a specific directory?

To see the disk usage of a specific directory, you can run the command “du directory_path”.

How does the du command calculate file sizes?

The du command calculates file sizes based on the actual size of the files on disk, including any additional space taken up by metadata.

Can the du command be used to exclude files from the disk usage calculation?

Yes, you can use the “–exclude” option with the du command to exclude specific files or directories from the disk usage calculation.

What is the difference between apparent size and disk usage displayed by du command?

The apparent size displayed by du command is usually smaller as it only accounts for the actual size of the file, while the disk usage includes any additional space taken up on the disk.

How can I get the total disk usage of files in a directory with the du command?

To get the total disk usage of files in a directory, you can use the “du -s” command in Linux.

Can I use the du command to check the disk usage of files in subdirectories?

Yes, you can use the “du -h –max-depth=N” command to check the disk usage of files in subdirectories up to N levels below the specified directory.