DEV Community

Cover image for Linux Commands: du
Umesh Yadav
Umesh Yadav

Posted on • Edited on • Originally published at umesh.wtf

9 4

Linux Commands: du

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. Also please follow me here and on Twitter for future blogs which I write over here

Neon image

Resources for building AI applications with Neon Postgres 🤖

Core concepts, starter applications, framework integrations, and deployment guides. Use these resources to build applications like RAG chatbots, semantic search engines, or custom AI tools.

Explore AI Tools →

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay