My file system is set up like:


("*"denotes directory names that change)

I'd like to find out how much space is being used for 2011 products. Something like

du -ks /ftp/data/*/*/2011

However, I need the sum of all these directories - there are hundreds of them and I don't want to have to add them all up (too error prone!)

Would anyone have a suggestion?

  • 1
    and du's -c option isn't what you are looking for? – llua Dec 27 '12 at 18:15
  • 2
    @llua, not all du implementations support the -c option as that's not a standard option. – Stéphane Chazelas Dec 27 '12 at 18:33
  • Ah, never knew that^ – llua Dec 27 '12 at 19:48

Simply pipe the results from du into something that will perform the addition for you:

du -ks /ftp/data/*/*/2011 |
    sed -e 's/\(^[0-9]*\).*$/\1 +/' -e '1s/^/0 /' -e '$s/$/ p/' | dc
  • 2
    I think using awk to sum the column would be more elegant some thing like du -ks /ftp/data/*/*/211 | awk '{ n+=$1; }; END { print n; }' – donothingsuccessfully Dec 27 '12 at 18:32
  • Sure, that looks less cryptic than mine :-) – Celada Dec 27 '12 at 19:18

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.