du Command Examples in Linux

This tutorial explains Linux “du” command, options and its usage with examples.

du – estimate file space usage.

DESCRIPTION

You need to use the du command:
1. Find and estimate file space usage.
2. Summarize disk usage of each FILE/Directory/Folder.
3. Shows the sizes of directories and files.

SYNOPSIS

du [OPTION]… [FILE]…

OPTIONS

advertisement
advertisement

-a
Displays the space that each file is taking up.
-k
Write the files sizes in units of 1024 bytes, rather than the default 512-byte units.
-s
Instead of the default output, report only the total sum for each of the specified files.
-d
Do not cross filesystem boundaries. For example, du -d / reports usage only on the root partition.
-L
Process symbolic links by using the file or directory which the symbolic link references, rather than the link itself.
-o
Do not add child directories’ usage to a parent’s total. Without this option, the usage listed for a particular directory is the space taken by the files in that directory, as well as the files in all directories beneath it. This option does nothing if -s is used.
-r
Generate messages about directories that cannot be read, files that cannot be opened, and so forth, rather than being silent (the default).
-x
When evaluating file sizes, evaluate only those files that have the same device as the file specified by the file operand.
-c
Display size of each item.
directories
Specifies the directory or directories.

EXAMPLES

All the du examples shown here are executed on a directory containing the following contents:

$ ls
linuxKernel  redhat  testfile.txt  ubuntu

1. Display memory usage by each file

$ du -a
0	./redhat/rh7
4	./redhat
4	./testfile.txt
0	./linuxKernel
0	./ubuntu/ub10
4	./ubuntu
16	.
 
$ du
4	./redhat
4	./ubuntu
16	.

If -a is not used then only directories that are occupying some disk are listed.

2. Display output in human readable form

For human readable format -h option is used.

advertisement
$ du -ah
0	./redhat/rh7
4.0K	./redhat
4.0K	./testfile.txt
0	./linuxKernel
0	./ubuntu/ub10
4.0K	./ubuntu
16K	.

3. Display grand total

Through the -c option, one can get the total usage in the output.

$ du -ahc
0	./redhat/rh7
4.0K	./redhat
4.0K	./testfile.txt
0	./linuxKernel
0	./ubuntu/ub10
4.0K	./ubuntu
16K	.
16K	total

4. Display only the total

advertisement

Only total is displayed using -s option.

$ du -sh
7.3G	.

5. Customize the block size in output

$ du -ac
0	./redhat/rh7
4	./redhat
4	./testfile.txt
0	./linuxKernel
0	./ubuntu/ub10
4	./ubuntu
16	.
16	total

The above output is represented in terms of number of 1024 bytes blocks. Now suppose if we require the output to be in number of 2048 bytes block, then in this case the flag ‘–block-size’ can be used.

$ du -ahc --block-size=2048
0	./redhat/rh7
2	./redhat
2	./testfile.txt
0	./linuxKernel
0	./ubuntu/ub10
2	./ubuntu
8	.
8	total

6. Display output in bytes

For bytes, -b option is used.

$ du -achb
0	./redhat/rh7
4096	./redhat
3	./testfile.txt
0	./linuxKernel
0	./ubuntu/ub10
4096	./ubuntu
12291	.
12291	total

7. Exclude particular types of file(s)

Say excluding txt files

$ du -cbha --exclude="*.txt"
0	./redhat/rh7
4.0K	./redhat
0	./linuxKernel
0	./ubuntu/ub10
4.0K	./ubuntu
12K	.
12K	total

8. Display the modification time and customize the display style

This can be achieved through –time and –time-style options..

$ du -cbha --time
0	2012-05-22 21:52	./redhat/rh7
4.0K	2012-05-22 21:52	./redhat
3	2012-06-18 19:23	./testfile.txt
0	2012-05-22 21:52	./linuxKernel
0	2012-05-22 21:52	./ubuntu/ub10
4.0K	2012-05-22 21:52	./ubuntu
13K	2012-06-18 19:23	.
13K	2012-06-18 19:23	total

$ du -cbha --time --time-style=iso
0	2012-05-22	./redhat/rh7
4.0K	2012-05-22	./redhat
3	2012-06-18	./testfile.txt
0	2012-05-22	./linuxKernel
0	2012-05-22	./ubuntu/ub10
4.0K	2012-05-22	./ubuntu
13K	2012-06-18	.
13K	2012-06-18	total

For –time-style, you can also use full-iso, long-iso, iso.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.