Cogs and Levers A blog full of technical stuff

xargs -i

This post is a just a tid-bit for the use of xargs in bash.

If you can get a list of work to do from a file or stream, you can pipe these into xargs to do further work. An example of this, I’ve taken from here. This will find all of the *.bak in or below the current directory and delete them.

find . -name "*.bak" -type f -print | xargs /bin/rm -f

Extending the usage of xargs to incorporate the -i switch, you can replace “{}” with the line of text read in the preprended command.

cat url_endings | xargs -i wget "http://somewhere.com"

In this command, it’s expected that the file “url_endings” would be data that looks like this:

/files/first
/files/second
/files/third
etc . .