xargs

xargs lets you chain together and patch up results from piped commands into other commands or applications.

$ find "start/path" -name "filename.txt" -print0 | xargs -0 -o vim
$ git branch --merged | egrep -v "(^\*|master|main|dev|staging)" | xargs git branch -d

It is invoked like xargs [option...] [command [initial-arguments]]. For instance, the end result of that first example would be vim {result of find command}, and the end result of the second would be git branch -d {result of egrep on results of git command}.

Options

$ xargs -0 --max-args=2

Simple Version

You can do simple xargs recipes in shell using command substitution:

$ # xargs version
$ find "start/path" -name "filename.txt" -print0 | xargs -0 -o vim
$ # shell versions
$ vim `find "start/path" -name "filename.txt"`
$ vim $(find "start/path" -name "filename.txt")

References

  1. https://www.man7.org/linux/man-pages/man1/xargs.1.html
  2. https://askubuntu.com/a/1158337
  3. https://unix.stackexchange.com/a/5865

Last modified: 202204131402