xargs Command - Converts Standard Input Line Output to Command Line Arguments and Runs the Program

The xargs command translates the line output of standard input into command line arguments and runs the program.

CommandThatOutputLines | xargs Program

The command to output the line can be anything, but the find command and the grep. It is assumed that "-rl" is specified in the command.

find * | xargs Program
grep -rl 'Hello' dir | xargs Program

As an example, let's take a look at the output of the find command. Get the file name in templates/blog.

find templates/blog

This is the output result.

templates/blog/20190421171737.html
templates/blog/20190103171737.html
templates/blog/20190127171737.html
templates/blog/20190212171737.html
templates/blog/20200520082545.html
templates/blog/20190309171737.html
templates/blog/20191205081119.html
templates/blog/20191218072611.html

When you want to pass this list of filenames to the command line arguments of another program, you need to use xargs.

For example, consider using Perl to make a replacement.

For the above file, replace "Hello" with "Good evening". Write the following using the xargs command:

find templates/blog | xargs perl -pi -e 's/hello/good evening/g'

Associated Information