3

I was wanting to run a find and then execute a script on each match; however, I was wanting to print the name of the matched file above the output from each exec. How can I produce the following output:

$ find . -name 'something' -exec sh script.sh {} \;
./something_1
output from 
script.sh something_1
./something_2
output from
script.sh something_2

I am currently only getting the output from script.sh. I tried -exec echo {} && sh script.sh {} \; with no success.

I would prefer a solution using -exec or xargs -print0, i.e., not prone to problems with white space.

1
  • If you can modify script.sh, the do so to make it emit the file it operates on just as it start. If you can not, make a wrapper.sh which emits the output and calls script.sh. If your script can take multiple file names as arguments then you should investigate xargs, eg find -find_options | xargs script.sh Commented Mar 1, 2013 at 5:17

2 Answers 2

9

find prints all matching files by default if you don't specify any other action.

When you do -exec ..., it replaces the -print.

When you put two actions next to each other, it means AND.

So just do both:

$ find . -name 'something' -print -exec sh script.sh {} \;
0

Try doing this :

$ find . -name 'something' -exec sh script.sh {} \; |
    script.sh |
    script.sh

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.