[XARGS] Do you need to repeat a command in from a set of resul | LinuxCheatSheet
[XARGS] Do you need to repeat a command in from a set of results? Try xargs This is one of the old school Unix commands, and it's still useful. Xargs reads by default from a pipe (stdin) a series of values, and use them as a parameter for the command you want to repeat. Let's say you have a bunch of docker images you want to remove from your laptop [cheats@telegram ~]$ docker images -aq 29c1b56be99f b802d38857fc Instead of repeat docker rmi {image_id} many times, you combine the two commands this way docker images -aq | xargs -r docker rmi The -r option tell xargs to not execute commands if it receive no input (that is: you have no docker images at all to delete). xargs will automatically append every line in the output of docker images -aq as the last parameter of docker rmi, as if you typed docker rmi {image_id}. If you want to execute a command and the parameter you want to pass is not the last one, use -I option of xargs, e.g. command1 | xargs -I{} command2 {} otherparam. See man xargs for more details.
I hope you like it! Please vote for the channel or share it https://tchannels.me/c/linuxcheatsheet http://www.telegramitalia.it/linuxcheatsheet/
This channel is dedicated to broadcast linux suggestions, tricks on the command line, and black magic done with the shell. It is inspired to the (now closed) portico.org web site. The channel post wil...