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?

  • username: store$ du -ch --exclude=./uploads worked (with and without the wildcard) for me, from within the store directory. Are you running that command exactly and from within the store folder? And it has to be the relative path (that find sees/prints), not absolute. – Kevin Oct 31 '11 at 18:19
up vote 66 down vote accepted

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.

  • 3
    Strange... syntax with dot (du -sb --exclude=./relative/path/to/uploads) doesn't work for me. This does: du -sb --exclude relative/path/to/uploads – Nux Oct 22 '14 at 17:01
  • 3
    @Nux probably your flavor of du. Mine (OSX) doesn't even have --exclude, instead it has -I mask to "Ignore files and directories matching the specified mask." In your working example you also omit the =, making me further think it's just your version of du being slightly dufferent. – phatskat Nov 21 '14 at 15:51
  • Yes [OSX][1] version seems quite more simplified version of [Linux du][2]. [1]: developer.apple.com/library/mac/documentation/Darwin/Reference/… [2]: linux.die.net/man/1/du – Nux Nov 23 '14 at 14:53
  • What if I want to exclude multiple directories for example here excluding directories junk_to_ignore and uploads directories? – Kiran Telukunta Sep 10 '16 at 8:35
  • Why does it work with a relative path, but not an absolute path? Very odd! – Nick Feb 24 '17 at 16:55

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

du -ch --exclude=relative/path/to/uploads --exclude other/path/to/exclude
  • you ca also use du -ch --exclude={relative/path/to/uploads,other/path/to/exclude} – Hajo Thelen Jul 5 at 10:43

awesome to skip the virtual file systems like so

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.