Find & du to calculate total size vs xargs

0

Can someone confirm that the following one liner will produce the total human readable size of all the directories which FIND locates as having log in the name?

find -type d -name *log* | du -hcs

Tony

Posted 2014-03-13T18:37:28.337

Reputation: 241

Answers

0

Ready?

find . -type d -name '*log*' -print0 | xargs -0 du -hcs

  • Wrap the *log* in single quotes or else the shell will expand it before find sees it.
  • Use -print0 to separate the find output by null characters for xargs
  • use xargs -0 to put each null-separated filename from find into the command line of du

Easy, right? :)

cxw

Posted 2014-03-13T18:37:28.337

Reputation: 1 389

They both seem to do the job - yours outputs each directory as it finds it though where mine only prints the total. Not sure which one is 'better', I suppose it depends on the need. – Tony – 2014-03-13T18:54:52.397

Actually strike that comment :) - both came up with drastically different numbers hrm. – Tony – 2014-03-13T19:04:12.593

Still looking for feedback here on why the two values are different? – Tony – 2014-03-14T15:03:22.487

Can you post a tarball of your test-case directory tree somewhere? – cxw – 2014-03-28T12:04:12.610

Oh! It's because "du" doesn't take its input from the command line. In the original command line, the output of find goes to the standard input of du and gets ignored. As a result, du -hcs reports the total size of the whole current directory. The xargs in the answer moves those filenames from standard input to the command line where du will see them. In my /var on cygwin, the command line from the question gives the same output as du -hcs without the find: 79M .. The command line from the answer gives 1.1M ./log, indicating it only processed ./log. – cxw – 2014-03-28T12:09:27.187