Monday, December 21, 2020

The CLI Skeleton Key: 'xargs'

I remember the first time I learned about using pipes ( | ) as a Linux cli newb. I was so excited! ' | grep ... ' or "pipe grep" is standard cli-speak and I can't imagine getting far without pipes today.

This post is for those of you who might be unfamiliar with the 'xargs' command and would like to recapture the magic and mystery of your first foray into the world of pipes (there is a Mario joke in there somewhere).

Here is the MAN PAGE.

'xargs' will pass standard input as arguments to the command that follows it.

This means that, and this is a really simple example, you can move beyond pipe grep to pipe grep xargs. Say you us a command like '$ ls | grep jpg' which should return a list of all your files in directory with jpg in the name; if you wanted to delete all those files, you could simply add xargs to the end of your command like this: '$ ls | grep jpg | xargs rm -i' to interactively delete them all.

The uses of this powerful CLI tool are limited only by your imagination. Have fun!