Get the size of the subdirectories and files in Linux

Amit Merchant · November 10, 2021 ·

If you’re a DevOp or a just regular user who manages their Linux/Unix servers, it’s a recurring task where you would need to keep an eye on the size of the subdirectories and files of your project.

Turns out, it’s pretty easy to do this. It all takes a single command consisting of the disk usage utility (“du” for short) that lists all the important stuff that you would need.

So, for instance, if I want to list the size of all the subdirectories and files for one of my projects called Notepad in ascending order of the size, here’s how I can do that.

$ du -sh /home/amitmerchant/workspace/notepad/* | sort -h

And here’s how the output would look like.

du usage

As you can tell, the command estimates file space usage of all the subdirectories and files which can be specified using the *. It represents the size in a human-readable form using the -sh option.

Ultimately, we can then pipe the output of this command to sort which will sort the files and directories based on the memory they occupy.

👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!

Comments?