Understanding the UNIX command xargs

前端 未结 5 937
[愿得一人]
[愿得一人] 2021-01-31 16:37

I\'m pretty much confused on this. Need some clarifications.

Example 1 :

pgrep string | xargs ps

Example 2 :

5条回答
  •  面向向阳花
    2021-01-31 16:59

    A good example of what xargs does is to try getting sorted checksums for every file in a directory using find.

    find . | cksum  | sort
    

    returns just one checksum, and it's not clear what it's the checksum for. Not what we want. The pipe sends the stdout from find into stdin for cksum. What cksum really wants is a list of command line args, e.g.

    cksum file001.blah file002.blah  file003.blah
    

    will report three lines, one per file, with the desired checksums. Xargs does the magic trick - converting stdout of the previous program to a temporary and hidden command line to feed to the next. The command line that works is:

    find . | xargs cksum | sort
    

    Note no pipe between xargs and cksum.

提交回复
热议问题