Gonzalo Matheu
Gonzalo Matheu

Reputation: 10064

Is there a better way to 'use arguments in a pipes sequence' with xargs?

On several occasions I want to use xargs generated arguments and use them in an elaborated command pipeline (piping the results through several steps and using).

I typically end up using a bash -c with xargs-generated-argument as only argument. E.g:

find *.obj | xargs -I{} bash -c 'sha256sum $0 | tee $0.sha256' {}

Is there any better or more concise way to do this?

Upvotes: 1

Views: 124

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

I think it is more concise with GNU Parallel as follows:

find "*.obj" -print0 | parallel -0 sha256sum {} \| tee {}.sha256

In addition, it is:

  • potentially more performant since it works in parallel across all CPU cores
  • debuggable by using parallel --dry-run ...
  • more informative by using --bar or --eta to get a progress bar or ETA for completion
  • more flexible, since it gives you many predefined variables, such as {.} meaning the current file minus its extension, {/} meaning "base name" of current file, {//} meaning directory of current file and so on

Upvotes: 2

Related Questions