In this article we are going to shows how to use du command in Linux. If you want to learn about du command in Linux with Examples then this post is ideal for you.
In the Linux, du (disk usage) is a command line utility that is used to estimate disk space used by given files or directories. With the help of du command, you can track the files and directories which are consuming large amount of disk space.
How to Use du command in Linux
In the Linux, the basic syntax of the du command is show as below:
du [OPTIONS]... FILE...
If you run the du command without file, it will print the disk usage information of the current working directory. If you run the du command with the file and given file is a directory then it will display the disk usages of each file and subdirectory in that directory.
du ~/Documents
Du command also allows you to use multiple files and directories as arguments:
du ~/Documents ~/Pictures
If we want to get disk usage information in human readable format(K, M, G), use -h option with the du command as show below:
sudo du -h /studiesonline
Output
24K /studiesonline/test/data
12M /studiesonline/test/system_design
27K /studiesonline/test/chart/sample_table_data/tree
49K /studiesonline/test/chart/sample_table_data
23K /studiesonline/test/chart
118M /studiesonline/test
1.5G /studiesonline
You can get the total size of the given directory by using -s
option with the du command as show below:
sudo du -sh /studiesonline
Output
1.5G /studiesonline
Use -c
option to display a grand total. you can use it when you want to get the size of two or more directories:
sudo du -csh /studiesonline/test /studiesonline/test/system_design
Output
118M /studiesonline/test
12M /studiesonline/test/system_design
130M total
To display the disk usage information of the n-level subdirectories, use the --max-depth
option with the du command as show below:
sudo du -h --max-depth=1 /studiesonline/test
Output
24K /studiesonline/test/data
12M /studiesonline/test/system_design
27K /studiesonline/test/chart/sample_table_data/tree
49K /studiesonline/test/chart/sample_table_data
23K /studiesonline/test/chart
118M /studiesonline/test
Use the –time option to get the timestamp information of last modification:
sudo du --time -h /studiesonline/test
Output
24K 2021-02-14 12:17 /studiesonline/test/data
12M 2020-01-05 14:26 /studiesonline/test/system_design
27K 2021-03-11 10:02 /studiesonline/test/chart/sample_table_data/tree
49K 2020-09-17 23:12 /studiesonline/test/chart/sample_table_data
23K 2020-07-24 17:22 /studiesonline/test/chart
118M 2020-03-14 23:22 /studiesonline/test
Use du With other Commands
You can also use the du
command with other commands using pipes.
For an example, if you wan to display the 2 largest directories inside the /studiesonline
directory run the command below:
sudo du -h /studiesonline/ | sort -rh | head -2
Output
1.5G /studiesonline
118M /studiesonline/test
That’s all.
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.