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

前端 未结 7 1019
长发绾君心
长发绾君心 2020-12-30 10:12

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:

fin         


        
7条回答
  •  爱一瞬间的悲伤
    2020-12-30 10:53

    The xargs program breaks things up into batches, to account for the limits due to the maximum length of a unix command line. It's still more efficient than running your subcommand one at a time but, for a long list of inputs, it will run the command enough times that each "run" is short enough that it won't cause issues.

    Because of this, you're likely seeing one output line per "batch" that xargs needs to run.

    Because you may find it useful/interesting, the man page can be found online here: http://unixhelp.ed.ac.uk/CGI/man-cgi?xargs


    One other thing to note (and this may be a typo in your post or my misunderstanding) is that you have the "*.txt" unescaped/quoted. Ie, you have

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

    where you probably want

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

    The difference being that the command line may be expanding the * into the list of filenames that match... rather than passing the * into find, which will use it as a pattern.

提交回复
热议问题