A simple shell solution to execute a command for each stdout line

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

For instance:

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

If it takes everything that matches "foo", and move it to the bar / directory.

(I feel a little confused, asking what is probably an absurdly obvious solution.)

+3
source share
3 answers

This program is called xargs.

ls | grep foo | xargs -I %s mv %s bar/
+12
source
ls | grep foo | while read FILE; do mv "$FILE" bar/; done

This particular operation can be performed more simply:

mv *foo* bar/

Or for a recursive solution:

find -name '*foo*' -exec mv {} bar/ \;

find {} , .

+6

for your business, do it just like that.

for file in *foo*
do
   if [ -f "$file" ];then
      mv "$file" /destination
   fi
done

OR just mv if you don't need directories or files

mv *foo* /destination
0
source

Source: https://habr.com/ru/post/1735358/


All Articles