Linux Commands: du

UY

Umesh Yadav / October 17th, 2020

2 min read326 words

du is used to display disk usage statistics. It's a simple tool to calculate and print the disk space used by files or directories. Let's see how to use it.

You can simply type du or du <directory-name> in your terminal and it will print the size.

Screenshot 2020-10-18 at 12.16.02 AM.png

In the above example, it gives you the size of the directory. The number 8 represents the size in bytes.

To print each and every file and their respective size we will need to use the du -a command. If you just want to print the sizes in the root of the directory and don't want to go deep inside each directory you can use du *.

Screenshot 2020-10-18 at 12.17.09 AM.png

You can apply various options to manipulate the output.

  • -a: Display an entry for each file in a file hierarchy.
  • -c: Display a total size.
  • -d: Singnifes till what level deep this command should print the files.
  • -h: Human-readable output.
  • -g: Display the size in Gigabytes.
  • -k: Display the size in Kilobytes.
  • -m: Display the size in Megabytes.

Screenshot 2020-10-18 at 12.15.21 AM.png

Practical Usage#

Suppose you want to print the top files that are consuming the most disk space. You can pipe the output of the du to sort and then print the head. Screenshot 2020-10-18 at 12.23.29 AM.png

Conclusion#

I have been extensively using this command to resolve production issues. Whenever we have an issue related to space on VM boxes I use this command to figure out what is exhausting the space. So it comes in handy in solving the real-world problem faced while debugging production issues which is related to disk space.

If you liked this post please share it with others so that it can help them as well. You can tag me on Twitter @imumesh18. You can subscribe to my newsletter to read more from me.

Umesh's Newsletter

Subscribe to get my latest blogs delivered straight into your inbox