How to exclude multiple directories from du command in Linux

du command is used to find out the size of directories and files in Linux. It summarizes disk usage of each FILE, recursively for directories.

But in case you want to skip out any specific directory from the usage calculation you can use the below steps

Now I have a directory "work". lets check out the size of the files inside this directory

[root@nfsserver work]# du -sch *
1.2G    deep
58M     dir1
81M     dir2
18M     dir3
595M    myfile.txt
1.9G    total
 -s, --summarize : display only a total for each argument
 -c, --total : produce a grand total
 -h, --human-readable : print sizes in human readable format (e.g., 1K 234M 2G)

This gave us an output with size of all the directories inside work. But what if I want to skip "deep" from the calculation?

Observe the below command

[root@nfsserver work]# du --exclude=deep -sch *
58M     dir1
81M     dir2
18M     dir3
595M    myfile.txt
751M    total

So as you see "deep" directory was not touched for size calculation. Similarly if you want to exclude multiple directories use the below method

[root@nfsserver work]# du --exclude=deep --exclude=dir1 --exclude=dir2 -sch *
18M     dir3
595M    myfile.txt
613M    total

You can also sort the output according to the size of the files using below command

# du -sch * | sort -h -r
1.9G    total
1.2G    deep
595M    myfile.txt
81M     dir2
58M     dir1
18M     dir3
I hope I made my self clear.

Related Articles:
How to check the lock status of any user account in Linux
How to exclude some directories from find command in Linux
How to mount windows share on linux

1 thought on “How to exclude multiple directories from du command in Linux”

Leave a Comment