May 202015

Simply put, the du command gives you the size (disk usage) of a directory and files.

In the face of growing storage requirements and huge data warehouses, familiarity with the du command would stand Linux newbies in good stead.

Here’s the synopsis of du from the man pages.

du [OPTION]… [FILE]…
du [OPTION]… –files0-from=F

Let’s now consider some key du commands that Linux system administrators will find extremely handy.

Default Command

$ du

Running the above command will spit out a list of all directories in the current working directory and their size.

Total size of the current directory (including sub-directories and files) is provided at the end.

Although the results will not mention it, disk usage output is given in Kilobytes.

Summary Output
Far too often, we just need summary size of a directory, not pages of details.

In such situations, we’ll go with the -s option.

$ du -s

It’s simple but not that user-friendly since the output is provided in Kilobytes (who uses Kilobytes in an era of Gigabytes and Terabytes).

Human Readable Format
When you use the -h option (-h stands for human readable format) with du, you’ll see an easy to grasp output.

$ du -h

Depending on their size, file and directory sizes are suffixed with K (kilobytes), M (megabytes) or G (gigabytes).

Summary Plus Human Readable
By default, the du command will run into several pages, and even scroll off the top of the screen if your directory has several sub-directories and files.

But you can get a succinct output omitting all the details by combining the -s and -h options.

$ du -sh

When I’m in a rush, -sh is the option I first resort to unless circumstances require me to dig deeper.

Here’s an example from my Pictures directory.

$ du -sh Pictures
12G	Pictures

Specific Directories
More often than not we don’t need to know the disk usage for all directories but only for some specific directories.

In such a situation, the following command will come in handy.

$ du -sh dir1 dir2 dir3

Here’s the output I got when I ran du with the -sh option for the Pictures, Downloads and Document directories on my Ubuntu system.

$ du -sh Pictures Documents Downloads
12G	Pictures
116M	Documents
11G	Downloads

You can do the same thing with files instead of directories.

$ du -hs file-1 file-2 file-3

Talking of specifics, here’s an example as to how we can get file or directory sizes for all file/directories starting with I

$ du -h I*

The possibilities with du, like with a lot of Linux commands, are endless.

How much disk space is your home directory taking up?

Not a problem, kiddo. Just run the below command.

$ du -sh /home/michael
163G	/home/michael

I suppose my home folder is small compared to that of most readers. 🙂

All Files

When you want a count for every file and directory, use the -a option.

$ du -a

Combining -h with the -a displays the output in an easy to understand format.

Sort em
What if you want to sort the files in the directory by size?

Easy as pie with the below command.

du | sort -n

The output starts with smaller files with the largest file coming up at the very end.

Here’s another example of du with sort that displays the top five folders by size in the current directory.

$ du -hsx * | sort -rh | head -5

You can change the number to head -n substituting n for the number of top folders by size you want to see.

Grep It
The below command is a lot similar to the -sh option in its succinct output.

$ du -ch | grep total

Your output consists of a single line displaying the total size of current directory including all subdirectories.

mike@michael ~/Documents $ du -ch | grep total
116M	total

What if you want to exclude certain types of files.

Not an issue with du.

Here’s how to do it.

$ du --exclude=html

In the above example, I’ve excluded all html files in the command.

So when the final output on disk usage is present it will exclude the html files.

You can substitute html for other file types like jpg etc.

I encourage you to look at the man pages of du to browse through the various options.

$ man du

You’ll be surprised at how much you can do with all the various options of du.

Sorry, the comment form is closed at this time.