# 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:

/store
/junk_to_ignore
/more_junk_to_ignore
/user_one
/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*


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

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

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

## Related Questions

### Is there a Linux graphics program that displays man commands in a browser?

12  Asked on November 6, 2021 by vfclists

### How to comment in a windows.tmux file

2  Asked on November 6, 2021

### Expect PECL for PHP 7

1  Asked on November 6, 2021 by erik-figueiredo

### How do I force VPN clients to only go through a local SOCKS5 proxy?

1  Asked on November 6, 2021 by user3573987

### Usability is compromised on Ubuntu because of read-only filesystem error

1  Asked on November 6, 2021 by julio-kirk

### Compile linux kernel error

1  Asked on November 6, 2021 by bluesning

### My dotfiles bash script somehow deleted its own directory?

1  Asked on November 6, 2021 by drake-p

0  Asked on November 6, 2021

### systemd timer, how can I restart the service after power loss?

2  Asked on November 6, 2021

### Backup tool for pulling files out of encrypted disks?

1  Asked on November 6, 2021

### Enter text into executable file immediately after it’s run

1  Asked on November 6, 2021 by the-codesee

### Check if service is Enabled, Disabled, Running or Stopped

1  Asked on November 6, 2021

### How can I set file permissions for new files in Maildir?

0  Asked on November 6, 2021 by magrega

### Copy file recursively to all deepest subdirectories

2  Asked on November 6, 2021 by bactro

### How to reply to a mail meeting invitation with neomutt

1  Asked on November 6, 2021 by l-niesen

### Asterisks keep going in traceroute

1  Asked on November 6, 2021 by amirreza-a

### Ever growing iptables

3  Asked on October 31, 2021

### how to grep both files , but exclude other files

2  Asked on October 31, 2021

### zsh file tab completion: path preceded by non-space character

1  Asked on October 31, 2021 by sameers

### Is it safe to remove contents from var / swapfile / tmp?

2  Asked on October 31, 2021