I was trying to execute several commands on a single record found by the GNU/Linux find command. The first attempt was to create something like:

find . -name "*.pdf" -exec echo 'test' && echo 'test2' \;

But it’s not the way the find command works.

If you want to execute more than one command for each hit, you should use multiple -exec actions:

find . -name "*.pdf" -exec echo 'test' \; -exec echo 'test2' \;

I used this feature to list JAR files in a specified location along with classes they consist of:

find . -name "*.jar" -exec echo {} > /tmp/list \; -exec jar -tf {} \; >> /tmp/list

Of course the above code is purely exemplary, as the same goal can be achieved using the print action:

find . -name "*.jar" -exec jar -tf {} \; -print > /tmp/list


I found it quite handy, especially when the JBoss was throwing strange ClassNotFoundExceptions and I needed to find which JARs contained the same class more than once.

Did you like this? Share it:

Leave a reply



<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>