I have a script, run.sh, that looks like this:

FILES=$(find corpus/ -type f)
for i in $FILES
    ./individual.sh $i

It runs without problem.

I want to do away with the run script by piping each file from find to ./individual.

I would think that I could just do:

find corpus/ -type f | ./individual.sh

But in trying that it just pipes in a file with file name "" (leading to an error).

What is the proper syntax for this?

  • Something like find corpus/ -type f -print0 | xargs -0 -n1 ./individual.sh would work. No particular reason to not use -exec for this though (seems simpler, though runtime should be very close) – BroSlow Oct 26 '14 at 12:00
  • Also see this for the proper way of looping over find results. – BroSlow Oct 26 '14 at 12:00

You'll want to use find's -exec option:

find corpus/ -type f -exec ./individual.sh {} \;

For each match that find finds, it'll execute individual.sh, replacing {} with the name of the file it found. \; is how you end an exec with find.

The reason your pipe doesn't work is that the output from find is being provided to individual.sh via STDIN, not as an argument. Your script would need to know to look for EITHER input, or arguments through STDIN to make use of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.