Can someone show me to use xargs properly? Or if not xargs, what unix command should I use?
I basically want to input more than (1) file name for input <localfile>
, third input parameter.
For example:
1. use `find` to get list of files
2. use each filename as input to shell script
Usage of shell script:
test.sh <localdir> <localfile> <projectname>
My attempt, but not working:
find /share1/test -name '*.dat' | xargs ./test.sh /staging/data/project/ '{}' projectZ \;
Edit:
After some input from everybody and trying -exec
, I am finding that my <localfile>
filename input with find
is also giving me the full path. /path/filename.dat
instead of filename.dat
. Is there a way to get the basename from find
? I think this will have to be a separate question.
I'd just use find -exec
here:
% find /share1/test -name '*.dat' -exec ./test.sh /staging/data/project/ {} projectZ \;
This will invoke ./test.sh
with your three arguments once for each .dat
file under /share1/test
.
xargs
would pack up all of these filenames and pass them into one invocation of ./test.sh
, which doesn't look like your desired behaviour.
If you want to execute the shell script for each file (as opposed to execute in only once on the whole list of files), you may want to use find -exec
:
find /share1/test -name '*.dat' -exec ./test.sh /staging/data/project/ '{}' projectZ \;
Remember:
find -exec
is for when you want to run a command on one file, for each file.xargs
instead runs a command only once, using all the files as arguments.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With