Check directory usage with du Command in Linux

|
Last Updated:
|
|

In this tutorial, you will learn how to check directory usage with du command in Linux. du, an acronym for disk usage can help you as a Linux Users or an administrator to check and report which files or directories are consuming how much disk space in the Linux system.

Syntax:

 du [OPTION]... [FILE]...

Checking directory usage with du Command in Linux

Examples outlined below shows how to use DU command to check directory usage in Linux

1. Reports the disk space for the current directory

Launch your terminal with Ctrl+Alt+T keys and type du. With no arguments, du reports the disk space for the current directory. Normally the disk space is printed in units of 1024 bytes.

du
12984	./.mozilla/firefox/cxbch2t5.default-release-1615898171711
4	./.mozilla/firefox/Pending Pings
4	./.mozilla/firefox/Crash Reports/events
20	./.mozilla/firefox/Crash Reports
8	./.mozilla/firefox/8jpgnpf9.default
13028	./.mozilla/firefox
4	./.mozilla/extensions
13040	./.mozilla
85586428	.

2. To display the disk usage of all files and directories

Use -a flag.

du -a ~/Downloads
9832	/home/frank/Downloads/25 Days of Christmas Writeup (Blurred).pdf
38800	/home/frank/Downloads/vagrant_2.2.14_x86_64.deb
588	/home/frank/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
152756	/home/frank/Downloads/xampp-linux-x64-7.4.11-0-installer.run
201980	/home/frank/Downloads

3. Display disk usage of files in Human Readable Format

Using -h flag it displays sizes of files in:

  • Bytes
  • Kilobytes
  • Megabytes
  • Gigabytes, etc.
du -h ~/
64K	/home/frank/.mozilla/firefox/cxbch2t5.default-release-1615898171711/storage/default
588K	/home/frank/.mozilla/firefox/cxbch2t5.default-release-1615898171711/storage
13M	/home/frank/.mozilla/firefox/cxbch2t5.default-release-1615898171711
4.0K	/home/frank/.mozilla/firefox/Pending Pings
4.0K	/home/frank/.mozilla/firefox/Crash Reports/events
20K	/home/frank/.mozilla/firefox/Crash Reports
8.0K	/home/frank/.mozilla/firefox/8jpgnpf9.default
13M	/home/frank/.mozilla/firefox
4.0K	/home/frank/.mozilla/extensions
13M	/home/frank/.mozilla
82G	/home/frank

4. Display total summary of disk usage size of a directory

Use -s flag

du -sh ~/
82G	/home/frank

5. Display disk usage of a directory in Kilobyte blocks

Use -k flag to print sizes in 1024-byte blocks. This option is equivalent to --block-size=1K.

du -k ~/
64	/home/frank/.mozilla/firefox/cxbch2t5.default-release-1615898171711/storage/default
588	/home/frank/.mozilla/firefox/cxbch2t5.default-release-1615898171711/storage
12984	/home/frank/.mozilla/firefox/cxbch2t5.default-release-1615898171711
4	/home/frank/.mozilla/firefox/Pending Pings
4	/home/frank/.mozilla/firefox/Crash Reports/events
20	/home/frank/.mozilla/firefox/Crash Reports
8	/home/frank/.mozilla/firefox/8jpgnpf9.default
13028	/home/frank/.mozilla/firefox
4	/home/frank/.mozilla/extensions
13040	/home/frank/.mozilla
85591540	/home/frank

6. Display disk usage of a directory in Megabytes blocks

Use -m flag print sizes in 1,048,576-byte blocks. This option is equivalent to --block-size=1M.

du -m ~/
1	/home/frank/.mozilla/firefox/Crash Reports/events
1	/home/frank/.mozilla/firefox/Crash Reports
1	/home/frank/.mozilla/firefox/8jpgnpf9.default
13	/home/frank/.mozilla/firefox
1	/home/frank/.mozilla/extensions
13	/home/frank/.mozilla
83587	/home/frank

7. Print disk usage based on modification time of files

Use --time flag. It show the most recent modification timestamp (mtime) of any file in
the directory, or any of its subdirectories.

du --time ~/
4	2020-09-01 21:38	/home/frank/.mozilla/firefox/Pending Pings
4	2020-09-01 21:38	/home/frank/.mozilla/firefox/Crash Reports/events
20	2020-12-20 12:17	/home/frank/.mozilla/firefox/Crash Reports
8	2020-09-01 21:38	/home/frank/.mozilla/firefox/8jpgnpf9.default
13028	2021-03-16 15:37	/home/frank/.mozilla/firefox
4	2020-09-01 21:38	/home/frank/.mozilla/extensions
13040	2021-03-16 15:37	/home/frank/.mozilla
85593576 2021-03-18 15:19	/home/frank

8. To find out the total disk usage of a given set of files or directories

Use -c or --total flag to print a grand total of all arguments after all arguments have been
processed.

du -ch ~/
du --total -h ~/
20K	/home/frank/.mozilla/firefox/Crash Reports
8.0K	/home/frank/.mozilla/firefox/8jpgnpf9.default
13M	/home/frank/.mozilla/firefox
4.0K	/home/frank/.mozilla/extensions
13M	/home/frank/.mozilla
82G	/home/frank
82G	total

9. Displays the disk usage of all files and directories, but excludes the files that matches given pattern

Use --exclude=PATTERN flag, this will skip subdirectories or files matching PATTERN. For example, du --exclude="*.mozilla" ~/ excludes files whose names start or end in .mozilla.

du --exclude="*.mozilla" ~/
11572	/home/frank/.cache/google-chrome/Profile 4/Code Cache/js
11576	/home/frank/.cache/google-chrome/Profile 4/Code Cache
27648	/home/frank/.cache/google-chrome/Profile 4
27652	/home/frank/.cache/google-chrome
33948	/home/frank/.cache
85580720	/home/frank

10. Displays the disk usage of all files and directories, but excludes the files in the current directory

Use --exclude-from=FILE flag. Like --exclude, except take the patterns to exclude from FILE, one per line.

du -cha ~/Downloads
9.7M	./25 Days of Christmas Writeup (Blurred).pdf
38M	./vagrant_2.2.14_x86_64.deb
588K	./HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
150M	./xampp-linux-x64-7.4.11-0-installer.run
198M	.
198M	total
du -cha --exclude=xampp-linux-x64-7.4.11-0-installer.run
9.7M	./25 Days of Christmas Writeup (Blurred).pdf
38M	./vagrant_2.2.14_x86_64.deb
588K	./HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
49M	.
49M	total

In the above example i used Downloads to be my current directory. and we have seen that xampp-linux-x64-7.4.11-0-installer.run file has been excluded in the above output.

11. List inode usage information instead of block usage

Use --inodes flag. This option is useful for finding directories which contain many files, and therefore eat up most of the inodes space of a file system.

du --inodes ~/
5	/home/frank/.mozilla/firefox/Crash Reports
2	/home/frank/.mozilla/firefox/8jpgnpf9.default
91	/home/frank/.mozilla/firefox
1	/home/frank/.mozilla/extensions
94	/home/frank/.mozilla
17412	/home/frank

12. Finding the depth-ness of a directory

Use -d DEPTH or --max-depth=DEPTH to show the total for each directory (and file if –all) that is at most MAX_DEPTH levels down from the root of the hierarchy.

du -d0 ~/
85604412	/home/frank
du -h --max-depth=2 /home/frank
3.7M	/home/frank/.cache/tracker
37M	/home/frank/.cache/google-chrome
44M	/home/frank/.cache
4.0K	/home/frank/.mozilla/systemextensionsdev
13M	/home/frank/.mozilla/firefox
4.0K	/home/frank/.mozilla/extensions
13M	/home/frank/.mozilla
82G	/home/frank

You can now use du command in Linux perfectly.

For further understanding of du command, feel free to check:

man du

or

info du

Other Tutorials

How to use htop Command in Linux

Example Usage of ps Command in Linux

Example Usage of ls Command in Linux

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
Bett Frankline

Leave a Comment