AnswerBun.com

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
  /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?

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

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

     

Compile linux kernel error

1  Asked on November 6, 2021 by bluesning

       

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

   

Ask a Question

Get help from others!

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