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 file or directory is not mentioned then it will display disk usage of your current working directory. When du command executed without any option it will display the disk usage by 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 that 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 which are consuming unnecessary amounts of space on the hard disk drive.

Leave a Comment

Your email address will not be published. Required fields are marked *