Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy shell solution to execute a command for each line of stdout [duplicate]

Tags:

shell

This should be an absurdly easy task: I want to take each line of the stdout of any old command, and use each to execute another command with it as an argument.

For example:

ls | grep foo | applycommand 'mv %s bar/'

Where this would take everything matching "foo" and move it to the bar/ directory.

(I feel a bit embarrassed asking for what is probably a ridiculously obvious solution.)

like image 734
jameshfisher Avatar asked Mar 04 '10 01:03

jameshfisher


1 Answers

That program is called xargs.

ls | grep foo | xargs -I %s mv %s bar/
like image 75
Chris Jester-Young Avatar answered Sep 23 '22 23:09

Chris Jester-Young