How do I run a command on each file within a directory that matches a pattern?

2

I'm trying to run a command on every file within a directory (recursively) that matches a pattern. I need the filename of each item that matches for the command however. This is how far I've got:

find . -name '*.jar'

That gives me all the files I'm interested in. Now, I need to run the following command on all those files:

jarsigner -keystore ***** -storepass ****** $FILENAMEHERE

How do I reference the individual items in the output of find, for the command?

Josh Smeaton

Posted 2010-08-19T08:56:56.933

Reputation: 263

Answers

6

If jarsigner takes only one file name at a time, use either

find . -iname '*.jar' | xargs -l jarsigner -keystore ***** -storepass ******

or

find . -iname '*.jar' -exec jarsigner -keystore ***** -storepass ***** {} \;

garyjohn

Posted 2010-08-19T08:56:56.933

Reputation: 29 085

Yes, xargs makes things more readable! However the man says the '-l' option is deprecated: '-L 1' should do the same. – cYrus – 2010-08-19T09:23:12.903