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
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
0
Ready?
find . -type d -name '*log*' -print0 | xargs -0 du -hcs
*log*
in single quotes or else the shell will expand it before find
sees it.-print0
to separate the find output by null characters for xargs
xargs -0
to put each null-separated filename from find
into the command line of du
Easy, right? :)
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 ofdu
and gets ignored. As a result,du -hcs
reports the total size of the whole current directory. Thexargs
in the answer moves those filenames from standard input to the command line wheredu
will see them. In my/var
on cygwin, the command line from the question gives the same output asdu -hcs
without thefind
:79M .
. The command line from the answer gives1.1M ./log
, indicating it only processed ./log. – cxw – 2014-03-28T12:09:27.187