I'd like to run a shell command on each line taken from STDIN.
In this case, I'd like to run xargs mv
. For example, given two lines:
mfoo foo
mbar bar
I'd like to run:
xargs mv mfoo foo
xargs mv mbar bar
I've tried the following strategies with ruby
, awk
, and xargs
. However, I'm doing it wrong:
Just xargs
:
$ echo "mbar bar\nmbaz baz" | xargs mv
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
Through awk
:
$ echo "mbar bar\nmbaz baz" | awk '{ system("xargs $0") }'
Through ruby
:
$ echo "mbar bar\nmbaz baz" | ruby -ne '`xargs mv`'
$ ls
cat foo mbar mbaz
I have some questions:
- How do I do what I'm trying to do?
- What is wrong with each of my attempts?
- Is there a better way to "think about" what I'm trying to do?
I'm especially confused that my xargs
attempt isn't working because the following works:
$ echo "foo\nbar" | xargs touch
$ ls
bar foo
xargs
? Try just runningmv
instead ofxargs
in your AWK and Ruby scripts. Then ponder what happens with filenames containing “special” characters (space…). – Stephen Kitt Apr 09 '17 at 22:07xargs
to give you? – Michael Homer Apr 09 '17 at 22:17xargs mv mfoo foo
andxargs mv mbar bar
to run? – Michael Homer Apr 09 '17 at 22:18xargs
documentation? – Stephen Kitt Apr 09 '17 at 22:22xargs mv mfoo foo
will wait for input and runmv mfoo foo $x_1 $x_2 $x_3...
for every line$x_n
of input it gets. Is that what you wanted? – Michael Homer Apr 09 '17 at 22:22echo "mbar bar\nmbaz baz" | awk '{ system("xargs mv " $0) }'
ought to generate the xargs commands you requested. – Mark Plotnick Apr 10 '17 at 00:03xargs
needs to have the args piped into it. The following will work:echo "mbar bar\nmbaz baz" | awk '{ system("mv " $0) }'
notice the absence ofxargs
– mbigras Apr 10 '17 at 01:02