Using –exclude with the du command

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:

/store
  /uploads
    /junk_to_ignore
    /more_junk_to_ignore
  /user_one
    /uploads
  /user_two

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?

Asked By: Will

||

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

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

Awesome, to skip the virtual file systems do this:

du -hsx --exclude=/{proc,sys,dev,run} /*
Answered By: Ben Lutgens

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

To exclude multiple folders

du -ch --exclude={path/to/folder1,path/to_folder2/,...,}
Answered By: jedi

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

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
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.