The du Command In Linux

There’s always time for a basic article or two and today we’ll do that as we just play around with the du command in Linux. This is a functional tool and one you can learn to use in just a few minutes. Because of that, I’ll try to keep the article reasonably short.

Storage…

On Linux, everything is a file. These files are stored with a system – such as a particular formatting method like Ext4. Collectively, this is your file system.

Because of this, you may have done similar things, you’ll find that file sizes are all divisible by four. That’s because of the way things are stored. They’re stored in blocks and 4KB is (generally speaking) the smallest block you can have. So, it only makes sense that things can be divided by four.

Running out of storage space is A BAD THING®. It can even prevent you from booting! You do not want to run out of space on your drives. You don’t want to run out of space on any of your partitions.

When you installed Linux, you should have taken care to ensure that the partitions will be large enough for you to grow into. It’s possible to add storage. You can even modify your system to move your ~/home directory onto a partition all its own.

It’s easier to ensure you have enough room when you start. Storage space is relatively inexpensive these days. You can get really fast storage (such as an NVMe M.2 SSD) for little money. If your motherboard doesn’t support NVMe, you can get a cheap PCIe riser card to add that feature.

We’ll be using the du command to see how much space you’ve used.

The du Command:

The du command is a terminal-based application. You shouldn’t need to install anything for this article, it should be installed by default. We’ve previously used this command to sort files by size if you’re curious.

Anyhow, you can ensure du is available with the following terminal command:

Which should give you an output like this:

If you get an output similar to that, you’re good to go and won’t need to install anything to finish this article. The man page can be accessed with the following terminal command:

The output from that command should be a good indicator that we’re using one of the correct tools for the job. This being Linux, there are many choices. In this case, the du command is described as:

As you can tell by the introductory paragraphs, that’s a good tool for the task we’ve set for ourselves today. We want to see how much file space we’re using.

Using The du Command In Linux:

If you read the above paragraphs you’d know that this is another one of those things you do in the Linux terminal. Yes, there are graphical tools that will let you do this – but this way is universal. You’re going to find the du command on any Linux system you’re likely to touch. So, press CTRL + ALT + T and we can get going.

With your terminal open, you can just start with the basics:

That will show you the bytes used by the directories. We want to see the storage space consumed by directories and files, so we’ll add the -a flag to the following command:

That should show file and directory sizes in bytes. That’s not very readable by a human unless you want to do some math in your head. So instead we’re going to add the -h flag. Like so:

That’s all well and good. You can use cd to navigate around your computer or you can specify the path. How do you do that? Well, the syntax follows:

So, if we want to view the storage consumption of the ~/Downloads directory we’d simply use the following command:

Now, if you just want to see the total value of the directory you can use the following command with the -s flag:

If you want to find out how much storage space is consumed by your entire ~/home/<user> directory, that’s a very simple command:

I’d not suggest using the du command to check the entire drive of a running system. It will want to also calculate anything plugged into the computer and take forever. There are other ways to view drive usage information.

If you want to use a GUI, here’s a few tools for you:

A Few Ways To Visualize Disk Usage In Linux

There are innumerable ways to do this in the terminal and I’ve covered many of them. Most recently, I showed you a way to use Python to view disk usage. I admit my bias, but that’s a neat way to give you some basic information in a handy format that’s easily understood.

This is one of those things you can do with a multitude of tools. It is also a fairly basic task and something we should all be aware of. Know how much free storage you have and act accordingly.

Don’t wait for the drives to fill up before acting because that just makes it harder on you. You know the adage about how an ounce of prevention is better than a pound of cure? Well, keep that in mind when you think about your storage solutions.

Closure:

Oh, I’m sure I’ve mentioned the du command before. It sounds like something I’ve written about. I do know that I used it so that you could sort files by size, which is nice. At least I’m pretty sure I did…

At this point, who knows? I could do a lot of searching just to check, but the search doesn’t always do a good job and it’s too late now because I’ve written the article!

As of today, you should be fairly fluent in the du command. You might want to read the man page. There’s quite a bit that I didn’t cover. I only covered the basics. More specifically, I only covered the ways I use the du command. You may have very different needs. You may have a very different process. With that in mind, read the man page.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Let’s Find The Size Of A Directory

Today’s article is one where we’re going to find the size of a directory. Why? Because that sounds like something fun to do. The site has more than 250 articles, most of which are reasonably productive and authored by me. (I do love me some guest articles – so long as they’re from the community. No more paid stuff! That lesson was learned.) They can’t all be winners!

So, why would you want to know the size of a directory? How should I know? Maybe you need to make space? You could want to upload the directories and want to know if you have space at the destination? Perhaps you are going to transfer the files and you want to estimate how long that will take? You do you. I don’t care why you want to find the size of a directory. That sort of thing is between you and your directories!

Anyhow, we’ll be using ‘du’ in this article. If you want a GUI way to find the size of your directories, you can visualize disk usage easily enough. You can also easily find large files. You shouldn’t need to install ‘du’, it should be installed as a part of the standard toolkit (GNU coreutils). To find out if you have ‘du’ installed, you can just try du --v in the terminal, which will spit out some version information.

So this should be a fairly short article – nice and easy. Let’s get on with it!

Find The Size Of A Directory:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

We’re going to us ‘du -h‘ for everything, because that -h stands for ‘human readable’ and it’s just easier than seeing byte sizes. You don’t actually have to use the -h, it just makes good sense to do so.

The format for this is largely ‘du -h /directory‘, like so (using the Documents directory as an example):

You can just add more directories as needed (we’ll add downloads):

That will output the size of both directories. If you are unaware, the ~ (tilde) is a shortcut meaning your home folder. For example, if you’ve navigated away from your desktop, you can cd ~ and get back home. It’s faster than clicking your heels together three times!

By the way, if you’re stringing directories together, you can get the total size of the directories with the following:

That’s about it. That’s all you really need to know for this exercise. I told you that it’d be quick and easy! Most of my articles involve opening the terminal, and yet so many of them are really quite simple to learn and remember. I strongly urge new Linux users learn a bit about the terminal, not because it’s hard but because it’s often easier and faster than using a graphical tool.

Closure:

I wasn’t kidding about there being more than 250 articles. There are 256 of them at this time, and this will make 257. So far, I haven’t run out of ideas and I’ve been able to get one out to you every other day. I hope you find these articles as useful as I’d like them to be. It does take me longer to come up with ideas, so it might have to slow down at some point. I mean, I can’t keep writing these kind of articles forever,  can I? No… No, I don’t think I can.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.