Using --exclude with the du command

Unix & Linux Asked on January 3, 2022

This is probably something basic but I’m not able to make it work. I’m trying to use DU to get a total size of files minus certain directories. I need to exclude one specific directory called uploads but not every directory called uploads. For example, my file structure looks a bit like this:


I can run the following command:

du -ch --exclude=uploads* 

and it gives me the file size minus all the “uploads” directories. However, in trying to exclude certain directories (and all its sub-directories) I fail. I’ve tried variations of:

du -ch --exclude=./uploads*
du -ch --exclude='/full/path/to/uploads/*'

but can’t seem to figure it out. How do I exclude a specific directory?

7 Answers

To get the total size of files in files/ excluding all sub-directories

du -ch path/to/files --exclude=path/to/files/*/*

Answered by michfuer on January 3, 2022

Just adding a Mac example

du -skI "Downloads" -I "Caches" -I "Logs" -I "OneDrive" .

I do not see a way to use the -I with a path, so for example, I haven't gotten

du -skI "Downloads" -I "Caches" -I "Logs" -I "OneDrive" -I "Library/Application Support"  .

to work. May be possible, but I haven't gotten it yet.

Answered by Rob Morton on January 3, 2022

To exclude multiple folders

du -ch --exclude={path/to/folder1,path/to_folder2/,...,}

Answered by jedi on January 3, 2022

If you have to be on macOS, you install GNU Coreutils with the following command.

brew install coreutils

Most of the commands installed by Coreutils are prefixed by g, since they have a BSD namesake preinstalled on macOS. Then you can do the following.

gdu --exclude=Microsoft /Library/Fonts/

Answered by Lei Zhao on January 3, 2022

awesome to skip the virtual file systems like so

du -hsx --exclude=/{proc,sys,dev} /*

Answered by Ben Lutgens on January 3, 2022

To exclude multiple directories, just pass the --exclude flag again.

du -ch --exclude=relative/path/to/uploads --exclude other/path/to/exclude

Answered by Dave Neeley on January 3, 2022

You've almost found it :)

du -ch --exclude=./relative/path/to/uploads

Note no asterisk at the end. The asterisk means all subdirectories under "upload" should be omitted - but not the files directly in that directory.

Answered by rozcietrzewiacz on January 3, 2022

Add your own answers!

Related Questions

kdevtmpfsi process is using 100% CPU, restarting on kill

1  Asked on December 29, 2021 by syed-tahoor-ali


cannot find -luuid error make

2  Asked on December 26, 2021 by belmer1416


Can’t start sxhkd as a systemd service

1  Asked on December 26, 2021


How can I run sox as a service?

0  Asked on December 26, 2021


how can extract single character before the number in a string

4  Asked on December 26, 2021 by nas-ahmet


Add border arond text in pdf

0  Asked on December 26, 2021 by kalpesh-bhoj


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP