I know we can use find -exec ... to specify a command to run on each file, and output only those files for which the command succeeds, for example, find . -exec test -d {} \; -print will print out all the directories. I will like to give -exec a pipeline, and have find return files for which the last command of the pipeline returned true.
To be specific, I would like to run jar -t on each file, and grep the output for a class name. I tried find . -name \*jar -exec jar -tf {} \|grep -q foo \; -print, but its returning all the files. How can I change that?
Using a command pipe as argument to
-execwould only work if find would utilize a subshell. But find just executes one command with the an exec() function call and all following tokens up to the;will be passed as command arguments.So when in need for nifty shell features, you will have to call the subshell on your own: