Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a directory in Linux that contains a large number of files (tens of thousands), plus directories that may contain thousands of files as well.

At some point the following du command fails with an "Argument list too long" error:

du -ch data/*.txt

If I pipe via the find command I don't get the "total total",

find data/ -iname '*.txt' | xargs du -ch

share|improve this question
up vote 4 down vote accepted

Do something like this:

find data -iname '*.txt'  |xargs stat -c '%s' |awk '{total=total+$1}END{mbtotal = total / 1000000 ; print mbtotal}'

Basically, get your filelist using the find, get the file size of each hit using stat (formatting output so that you just display the size), and then total it up using awk (in my example, dividing by 1,000,000 to get something like megabytes; change the denominator at your discretion).

You can also do a similar exercise in Perl, or whatever language you want to use.

share|improve this answer
Perfect. Dividing by 1024*1024 does exactly what I wanted. – tmcallaghan Dec 3 '12 at 18:41
stat and xargs can be avoided find data -iname '*.txt' -printf '%k\n' | awk '{total=total+$1}END{mbtotal = total / 1000000 ; print mbtotal}' – hostmaster Mar 27 '13 at 9:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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