Why does “find . -name *.txt | xargs du -hc” give

2019-02-06 21:56发布

I have a large set of directories for which I'm trying to calculate the sum total size of several hundred .txt files. I tried this, which mostly works:

find . -name *.txt | xargs du -hc

But instead of giving me one total at the end, I get several. My guess is that the pipe will only pass on so many lines of find's output at a time, and du just operates on each batch as it comes. Is there a way around this?

Thanks! Alex

7条回答
做自己的国王
2楼-- · 2019-02-06 22:33

One alternate solution is to use bash for loop:

for i in `find . -name '*.txt'`; do du -hc $i | grep -v 'total'; done

This is good for when you need more control of what happens in the loop.

查看更多
登录 后发表回答