Mastering Disk Usage with du Command in Linux: A Beginner’s Guide

Check-the-size-of-directory-in-Linux

Have you noticed “how can you see the size of directory in Linux?” where have you seen the size of a directory in Linux?

Don’t you remember?

But in fact, you can’t see the directory size in a general way. You must do some extra effort for completing this task.

I will cover everything in this article.

It is quite easy in windows, You just move the cursor over the directory and you will see the file and directory size. and It is the total size of the directory.

To check the properties of file and directory is another way of getting its size.

Today I will cover in Linux size of directory and subdirectories. How to use the du command with examples and sort the files by its size in Linux. To find the size of the directory is easier than windows. You can use the graphical interface to find the size of a directory in Linux instead of du command. If you are not a CLI lover.

In Linux size of the directory

When I used the “ls” command to print the contents of the directory. I saw the content of /var directory. I run the following command and get the following result.

vijay@Ubuntu-19:~$ls -l /var
total 48
drwxr-xr-x  2 root root     4096 Jul  1 10:51 backups
drwxr-xr-x 17 root root     4096 Jun 19 10:42 cache
drwxrwsrwt  2 root whoopsie 4096 Apr 17 00:34 crash
drwxr-xr-x 67 root root     4096 Jun 28 11:03 lib
drwxrwsr-x  2 root staff    4096 Apr 16 13:06 local
lrwxrwxrwx  1 root root        9 Jun 19 08:36 lock -> /run/lock
drwxrwxr-x 13 root syslog   4096 Jul  1 10:50 log
drwxrwsr-x  2 root mail     4096 Apr 17 00:29 mail
drwxrwsrwt  2 root whoopsie 4096 Apr 17 00:34 metrics
drwxr-xr-x  2 root root     4096 Apr 17 00:29 opt
lrwxrwxrwx  1 root root        4 Jun 19 08:36 run -> /run
drwxr-xr-x 10 root root     4096 Jun 19 08:55 snap
drwxr-xr-x  7 root root     4096 Apr 17 00:32 spool
drwxrwxrwt  9 root root     4096 Jul  1 11:14 tmp
vijay@Ubuntu-19:~$

In the above result, What have you seen different?

You may have noticed that the size of most directories and files are almost 4096 bytes (4KB) only.

Then I checked multiple directories and got the same result. You can check right now on your Linux system.

Actually, this is not size what it contains, It is the size of space in disk used to store metadata (meta information) for the directory.

You can du command in Linux to check the actual size of the directory in Linux. The du stand for “disk usage”. I will show you how to use this command in the next section

Continue reading..

In Linux get the size of Directory

Let’s start with du command.

The du command is used to display the amount of disk space used by files and directories.

If you run command du without specified path then it will give the result behalf of the current working directory.

If you run du command followed by a specific path, it will summarize disk usage of each file and subdirectories in that directory.

I used du command without any argument and path. I got a shocking result. You run the command now, check the result and tell me our experience in the comment box.

vijay@Ubuntu-19:~$sudo du /var
5432	/var/cache/debconf
4	/var/cache/private
4	/var/cache/app-info/xmls
3684	/var/cache/app-info/gv
3692	/var/cache/app-info
36	/var/cache/snapd/aux
1380	/var/cache/snapd
684	/var/cache/fwupd
2400	/var/cache/fontconfig
40	/var/cache/dictionaries-common
6252	/var/cache/cracklib
4	/var/cache/PackageKit/downloads

If you do it then you will get summarize disk usage space of current directory and subdirectories, files in bytes.

Reading a result in bytes is equal to the killing ant and grasshopper in Jungle.

Do you want to get the result in the human-readable format then use -h option? Get the result and enjoy. but only -h option is not enough to understand the du command

du –help command will help you with available options for the du tool.

In the Example, I am going to display the result of /var directory. If you want to follow the same process then run the following command

$sudo du -sh /var
vijay@Ubuntu-19:~$sudo du -sh /var
2.1G	/var
vijay@Ubuntu-19:~$

Let’s explain the command and its arguments:

I used the command as sudo user because most of the files and directories inside the /var directory are owned by the root user and are not allowed to read by the regular users. If you omit sudo the du command will print “du: cannot read directory”. See the example below

vijay@Ubuntu-19:~$du -sh /root
du: cannot read directory '/root': Permission denied
4.0K	/root
vijay@Ubuntu-19:~$

Arguments explanation

  • s – Display only the total size of the specified directory, do not display file size totals for subdirectories.
  • h – Print sizes in a human-readable format (h).
  • /var – The path to the directory I am looking for getting the size.

In Linux find size fo directory and subdirectories

What if you want to see the disk usage of the first-level subdirectories. You have two options

  • Option 1: use directory path followed by the asterisk symbol as shown below which means “everything that doesn’t start with a period (.)“. Show all non-hidden files’ storage. The -c option is used to tell du to print a grand total of all sizes:
$sudo du -shc /var/*
vijay@Ubuntu-19:~$sudo du -shc /var/*
4.0M	/var/backups
111M	/var/cache
4.0K	/var/crash
1.5G	/var/lib
4.0K	/var/local
0	/var/lock
517M	/var/log
4.0K	/var/mail
4.0K	/var/metrics
4.0K	/var/opt
0	/var/run
120K	/var/snap
52K	/var/spool
60K	/var/tmp
2.1G	total
vijay@Ubuntu-19:~$
  • Option 2: you can use another option to list the disk usage by subdirectories. It is –max-depth=n switch and specify the subdirectories level.
    Here n=depth level of subdirectories.

I am using –max-depth=1 in following example:

$sudo du -h --max-depth=1 /var
vijay@Ubuntu-19:~$sudo du -h --max-depth=1 /var
111M	/var/cache
517M	/var/log
4.0K	/var/crash
4.0K	/var/metrics
4.0M	/var/backups
4.0K	/var/opt
4.0K	/var/local
120K	/var/snap
60K	/var/tmp
52K	/var/spool
4.0K	/var/mail
1.5G	/var/lib
2.1G	/var
vijay@Ubuntu-19:~$

To know the apparent size of a directory –apparent-size command will be used. The apparent size of the directory differs from disk space used by the same directory.

The apparent size of a file how much data is actually stored in the file. it is not including metadata.

When you transfer data over the network by using any protocol like SCP Rsync or SFTP. The amount of data transferred over the network is actually the apparent size of the files.

why so space of disk usage data and apparent size is different.

Check out the apparent size of any directory by executing following command

$sudo du -sh --apparent-size /var
vijay@Ubuntu-19:~$sudo du -sh --apparent-size /var
2.1G	/var
vijay@Ubuntu-19:~$
vijay@Ubuntu-19:~$sudo du -shc --apparent-size /var/*
[sudo] password for vijay: 
3.9M	/var/backups
111M	/var/cache
4.0K	/var/crash
1.5G	/var/lib
4.0K	/var/local
9	/var/lock
517M	/var/log
4.0K	/var/mail
4.0K	/var/metrics
4.0K	/var/opt
4	/var/run
121K	/var/snap
41K	/var/spool
60K	/var/tmp
2.1G	total
vijay@Ubuntu-19:~$

the du command to sort directories by size

you can combine the du command with other commands with pipes. Look at following

vijay@Ubuntu-19:~$sudo du -h /var/ | sort -rh | head -10
2.1G	/var/
1.5G	/var/lib
980M	/var/lib/snapd
624M	/var/lib/snapd/snaps
517M	/var/log
513M	/var/log/journal/4f14bff2e47e446f88896b31b0f9e7e4
513M	/var/log/journal
353M	/var/lib/snapd/seed/snaps
353M	/var/lib/snapd/seed
179M	/var/lib/apt/lists
vijay@Ubuntu-19:~$

Use GUI to Check File and Directory Size in Linux

Ubuntu has Gnome desktop graphical interface for users, So the users feel more comfortable with Ubuntu. And they don’t feel bored when moving to Linux from other OS.

You can use this GUI to see the file and directory size in Ubuntu. It is very simple and similar as windows.

Step 1: Open the file location.

30-go-to-location-where-is-ver

Step 2: Right clikc on directory then click on properties.

31-go-to-properties-of-var

Step 3: See the size of directory

32-check-size-of-directory-in-Linux

Conclusion

Now, I think you have learned much enough about du command, Now you can get easily the size of a directory in Linux.

If you still have any question write in the comment box.

You may like some other article related directory check here.

If you like our content, please consider buying us a coffee.
Thank you for your support!

Leave a Reply

Your email address will not be published. Required fields are marked *