Get Total Size of a Directory in Linux - Stack Abuse

Get Total Size of a Directory in Linux

Introduction

In Linux, ls -l would list the files and directories in a particular path, with their names, dates, and sizes (disk usage). The first thing you'll notice using that command is that size of directories is always shown as 4096 bytes (or 4,0K if you're using ls -lh) even though they contain files that greater than 4 KB in size. The reason is that ls returns meta-data for the directories, not the actual size.

So what's the shortest and easiest way to get the size of a directory in Linux, you ask? To get the total size of a directory in Linux, you can use the du (disk-usage) command.

In this article, we'll take a look at some of the most common usages of the du commands, including but not limited to du -sh, du -ch, and du --max-depth.

Getting Size of Directory in Linux with du

To see the full description and argument list of du command, refer to the man du.

If we type du without any arguments, it will list all the directory names and sizes for the current working directory and all sub-directories recursively:

rus:~/nltk_data$ du
2156	./corpora/state_union
64	./corpora/names
7624	./corpora/conll2002
432	./corpora/toolbox/rotokas
### ...
246984	./corpora
16792	./tokenizers/punkt/PY3
36028	./tokenizers/punkt
49420	./tokenizers
296408	.

Get Size of the Current Working Directory

To get the size of the current working directory only, and not the sub-directories, we can use du -s or du --summarize:

rus:~/nltk_data$ du -s
296408	.

We can add the -h parameter to get the size in a more human-readable format:

rus:~/nltk_data$ du -sh
290M	.

We can also use du with $PATH parameter to get the size of a directory that is located somewhere other than the current working directory:

rus:~/nltk_data$ sudo du /var -sh # or "du -sh /var" if you prefer
11G	/var

Note that you would need to use it with sudo privileges for some directories, otherwise you would get a Permission denied error.

Get Size of First-Level Sub-Directories

To get size of first-level sub-directories as well as the total size of the path directory:

rus:~$ sudo du /var/* -shc
6,1M	/var/backups
144M	/var/cache
4,0K	/var/crash
7,2G	/var/lib
4,0K	/var/local
0	/var/lock
3,0G	/var/log
4,0K	/var/mail
4,0K	/var/metrics
4,0K	/var/opt
0	/var/run
3,8M	/var/snap
52K	/var/spool
72K	/var/tmp
28K	/var/www
11G	total

-c or --total returns the total size of the path (11G total). * lists all the first-level sub-directories in the /var/ directory. We can also add --exclude to exclude any directory:

rus:~$ sudo du /var/* -shc --exclude=lib
6,1M	/var/backups
144M	/var/cache
4,0K	/var/crash
4,0K	/var/local
0	/var/lock
3,0G	/var/log
4,0K	/var/mail
4,0K	/var/metrics
4,0K	/var/opt
0	/var/run
3,8M	/var/snap
52K	/var/spool
72K	/var/tmp
28K	/var/www
3,2G	total

Note that excluding lib also affects the total size (3,2G total). This is also equivalent of sudo du /var/ -h --exclude=lib --max-depth=1

rus:~$ sudo du /var/ -h --exclude=lib --max-depth=1
4,0K	/var/mail
52K	/var/spool
3,8M	/var/snap
4,0K	/var/metrics
144M	/var/cache
6,1M	/var/backups
72K	/var/tmp
4,0K	/var/crash
3,0G	/var/log
4,0K	/var/opt
28K	/var/www
4,0K	/var/local
3,2G	/var/

Git Essentials

Check out this hands-on, practical guide to learning Git, with best-practices and industry-accepted standards. Stop Googling Git commands and actually learn it!

--max-depth=N will return all sub-directory levels that are equal or less than the number N. Setting --max-depth to 1 returns the first-level, 2 for the second, and so on.

Get Size of All Sub-Directories

To recursively get all subdirectories of /var/, you can use sudo du /var/ -h. Or you can pass a number to the --max-depth that you're sure is greater than or equal to the max level of sub-directory depth: sudo du /var/ -h --max-depth=999.

The second option is more of a workaround rather than the most efficient way.

Get Size of Directory in Linux with tree --du -h

tree is a recursive directory listing program that will list directories and files in a tree-like format. Note that tree is not installed by default. For Debian/Ubuntu, we can install the tree by running sudo apt install tree.

After the installation complete, we use the tree command to list names and sizes of all directories and files in a particular path, in a tree-like format:

rus:~$ tree /var/www/ --du -h
/var/www/
├── [4.2K]  demosite
│   └── [ 189]  index.html
└── [ 15K]  html
    └── [ 11K]  index.html

  23K used in 2 directories, 2 files

Conclusion

In this article, we learned how to get directory sizes in Linux. You can use these commands on Linux remote machines, servers, and/or Linux machines with or without GUI.

For most of the cases du command would be sufficient. It has also the advantage of being installed by default. On the other hand, the tree command would provide a more visual and detailed user interface to display almost the same results, making it a powerful alternative for du.

Last Updated: February 2nd, 2021

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Ruslan HasanovAuthor

Full-stack software developer.
Python, C#, Linux.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:
     
     
     

    © 2013-2021 Stack Abuse. All rights reserved.