3

I am trying to get the disk usage of the owncloud directory except not including the files and dirs containing the string nobackup in the name/dir.

This is my command so far :

find . -type f -not -regex '.+?nobackup.*'  -exec du -c {} +  

But it gives me a false total when used on the cloud dir, on a test dir it works just fine.

  • You're using +? which is defined as "undefined behaviour" in a POSIX ERE, but has a special meaning in other types of regex, like PCRE. GNU find for example does not support PCRE. Which OS and/or version of find? – mr.spuratic Oct 24 '14 at 13:56
  • Do your results change if you use find . -type f -not -name "*nobackup*" -exec du -c {} + ? – Omnipresence Oct 24 '14 at 15:03
  • i am running Ubuntu 14.04.1 with find (GNU findutils) 4.4.2 if i use : find . -type f -not -name "*nobackup*" -exec du -c {} + it gives also the files in a dir called nobackup. – k.mooijman Oct 24 '14 at 17:23
  • The strange thing is that the initial find command lists the files well but it looks like something gets lost in the du command . is there a limit on content that can be conducted true this -exec ? it looks like some get lost ( there are about 120000 files in this path ) – k.mooijman Oct 24 '14 at 17:42
  • @mr.spuratic emacs re's are used by default with -regex in find. +? is the non greedy +, though not particularly sure why the distinction is needed here. – BroSlow Oct 26 '14 at 13:31
2

If you use either find ... -exec COMMAND {} + or find ... -print0 | xargs -0 COMMAND, xargs or find will be build a list of filename arguments no longer than each command’s defined buffer space (bounded by ARG_MAX). If the buffer space is exhausted, each command will pass the list of filename arguments to COMMAND and start creating a new list. That process will repeat as many times as necessary, until there are no more filenames, at which point, the list of filenames that was being built will be passed to COMMAND a final time.

So if you're not expecting your find command to return a large number of filenames

find . -type f -not -regex '.+?nobackup.*' -exec du -c {} +  

may be sufficient. If you are and your du supports --files0-from you can use, --files0-from=- it to read null-terminated files directly from stdin (du considers - to mean coming from stdin)

find . -type f -not -regex '.+?nobackup.*' -print0 | du -c --files0-from=-

Note: --files0-from is not specified by posix, but it should be extended in the most recent gnu du versions. If your implementation has it omitted you will most likely have to loop over the results of find in some way and add up the totals in the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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