127

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?

Will
  • 1,373
  • 2
  • 8
  • 5
  • `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

7 Answers7

157

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.

rozcietrzewiacz
  • 38,754
  • 9
  • 94
  • 102
  • 6
    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
  • 6
    @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 `du`fferent. – phatskat Nov 21 '14 at 15:51
  • Yes [OSX][1] version seems quite more simplified version of [Linux du][2]. [1]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/du.1.html [2]: http://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 K Telukunta Sep 10 '16 at 08:35
  • 1
    Why does it work with a relative path, but not an absolute path? Very odd! – Nick Feb 24 '17 at 16:55
  • Doesn't work either. I tr to use it with `du -hs *` – Suncatcher Aug 18 '17 at 07:22
  • is there an example of how to use the `-I` flag which the macOS / OS X version has? Its not working with any syntax shown here, nor `du -sh -I demo/` style syntax – user5359531 Jan 13 '18 at 00:38
28

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

du -ch --exclude=relative/path/to/uploads --exclude other/path/to/exclude
Dave Neeley
  • 389
  • 3
  • 4
17

Awesome, to skip the virtual file systems do this:

du -hsx --exclude=/{proc,sys,dev,run} /*
Marc
  • 352
  • 1
  • 2
  • 12
Ben Lutgens
  • 271
  • 2
  • 4
6

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/
Lei Zhao
  • 161
  • 1
  • 3
5

To exclude multiple folders

du -ch --exclude={path/to/folder1,path/to_folder2/,...,}
jedi
  • 401
  • 4
  • 11
  • hi, what if we want to exclude all sub-folder? only count files that direct children to current directory. – Permana May 06 '20 at 03:28
5

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.

guntbert
  • 1,597
  • 1
  • 17
  • 23
Rob Morton
  • 51
  • 1
  • 1
-1

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

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

michfuer
  • 7
  • 1