2

I am using this command to find the folders I want and count up the size.

find . -type d -name 'tmp_c*' | xargs du -hcs {} \; + 

My version of find does not support -exec. But, this works. However I am not sure if its giving me the right totals on the directories that contain my search string. When I run the command and pipe to less, I see it counting up each folders size, and then it outputs a total every so often. Like this:

140K    ./r/g/userid/attach/tmp_c_241091464_2
68K     ./r/g/userid/attach/tmp_c_58367014_undefined
2.3G    total

If I redirect the output to a file then grep on total, I get this:

2.3G    total
978M    total
1.1G    total
2.0G    total
1.1G    total

I think this is giving me the right numbers. But how can I take this command one step further and have it sum up the totals for a grand total on one line?

  • can you not get ncdu from epel? – mikejonesey Oct 28 '16 at 18:29
  • Can't find an epel for RHEL3 – user53029 Oct 28 '16 at 18:38
  • Are you certain that none of the folders you want are nested? If they are, you'll get some files double counted with the approach you are using. What's wrong with du -hs ./r/g/userid/attach? – Wildcard Oct 28 '16 at 18:57
1
find . -type d -name 'tmp_c*' -print0 | du -hcs --files0-from -

This assumes (recent enough?) GNU versions of find and du.

  • Afraid not. RHEL 3 and du (coreutils) 4.5.3. I get an error about the --files-from - argument being an unrecognized option. – user53029 Oct 28 '16 at 18:01
  • --files0-from. My commiserations. – sourcejedi Oct 28 '16 at 18:19
1

This should work, tested on my EL4 box... it's the oldest I have and makes me cringe, I don't know how you use EL3.

find . -type d -name 'tmp_c*' -print0|xargs -0 du -c|tail -1

Done with coreutils-4.5.3-26

1

https://stackoverflow.com/a/1323769/799204

One alternate solution is to use awk:

but the script must be adapted for this case, try

find . -type d -name 'tmp_c*' -print0 | xargs -0 du -s | awk 'BEGIN { sum=0 } { sum+=$1 } END { print sum }'

output will be in decimal

  • This comand works but its sum total does not match what the command I was running totals up to. Not sure whats going on there. – user53029 Oct 28 '16 at 18:40
  • Just as an FYI -F " " is unnecessary as space is the implied default value for FS. – Zachary Brady Oct 28 '16 at 18:55
  • Huh. You could try gawk instead just to be sure, but I dunno. (gawk specifically has docs that the number type should be good up to 2^51 or so). – sourcejedi Oct 28 '16 at 19:13
0

I checked epel starts at 5 not 3, so only option is custom script.

May aswell install ncdu;

https://dev.yorhel.nl/ncduit

I use it all the time, far easier, faster than any custom bash. (Can navigate through the dirs, saves loads of time).

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.