<p>I'm trying to construct a find command to process a bunch of files in a directory using two different executables.  Unfortunately, <code>-exec</code> on find doesn't allow to use pipe or even <code>\|</code> because the shell interprets that character first.  </p>

<p>Here is specifically what I'm trying to do (which doesn't work because pipe ends the find command):</p>

<pre><code>find /path/to/jpgs -type f -exec jhead -v {} | grep 123 \; -print
</code></pre>

[{'answer_id': 62054, 'author': 'Xetius', 'author_id': 274, 'author_profile': 'https://Stackoverflow.com/users/274', 'pm_score': 1, 'selected': False, 'text': '<p>As this outputs a list would you not :</p>\n\n<pre><code>find /path/to/jpgs -type f -exec jhead -v {} \\; | grep 123\n</code></pre>\n\n<p>or </p>\n\n<pre><code>find /path/to/jpgs -type f -print -exec jhead -v {} \\; | grep 123\n</code></pre>\n\n<p>Put your grep on the results of the find -exec.</p>\n'}, {'answer_id': 62060, 'author': 'Martin Marconcini', 'author_id': 2684, 'author_profile': 'https://Stackoverflow.com/users/2684', 'pm_score': 8, 'selected': True, 'text': "<p>Try this</p>\n\n<pre><code>find /path/to/jpgs -type f -exec sh -c 'jhead -v {} | grep 123' \\; -print\n</code></pre>\n\n<p>Alternatively you could try to embed your exec statement inside a sh script and then do:</p>\n\n<pre><code>find -exec some_script {} \\;\n</code></pre>\n"}, {'answer_id': 62066, 'author': 'mweerden', 'author_id': 4285, 'author_profile': 'https://Stackoverflow.com/users/4285', 'pm_score': 2, 'selected': False, 'text': '<p>With <code>-exec</code> you can only run a single executable with some arguments, not arbitrary shell commands. To circumvent this, you can use <code>sh -c \'&lt;shell command&gt;\'</code>.</p>\n\n<p>Do note that the use of <code>-exec</code> is quite inefficient. For each file that is found, the command has to be executed again. It would be more efficient if you can avoid this. (For example, by moving the <code>grep</code> outside the <code>-exec</code> or piping the results of <code>find</code> to <code>xargs</code> as suggested by <a href="https://stackoverflow.com/questions/62044/how-do-i-use-a-pipe-in-the-exec-parameter-for-a-find-command#62142">Palmin</a>.)</p>\n'}, {'answer_id': 62142, 'author': 'Palmin', 'author_id': 5949, 'author_profile': 'https://Stackoverflow.com/users/5949', 'pm_score': 4, 'selected': False, 'text': '<p>A slightly different approach would be to use xargs:</p>\n\n<pre><code>find /path/to/jpgs -type f -print0 | xargs -0 jhead -v | grep 123\n</code></pre>\n\n<p>which I always found a bit easier to understand and to adapt (the -print0 and -0 arguments are necessary to cope with filenames containing blanks)</p>\n\n<p>This <em>might</em> (not tested) be more effective than using -exec because it will pipe the list of files to xargs and xargs makes sure that the jhead commandline does not get too long.</p>\n'}, {'answer_id': 5131251, 'author': 'Dimitar', 'author_id': 636119, 'author_profile': 'https://Stackoverflow.com/users/636119', 'pm_score': 2, 'selected': False, 'text': '<p>Using <code>find</code> command for this type of a task is maybe not the best alternative. I use the following command frequently to find files that contain the requested information:</p>\n\n<pre><code>for i in dist/*.jar; do echo "&gt;&gt; $i"; jar -tf "$i" | grep BeanException; done\n</code></pre>\n'}, {'answer_id': 48566089, 'author': 'linuxgeek', 'author_id': 8906994, 'author_profile': 'https://Stackoverflow.com/users/8906994', 'pm_score': 0, 'selected': False, 'text': '<p>There is kind of another way you can do it but it is also pretty ghetto.</p>\n\n<p>Using the shell option extquote you can do something similar to this in order to make find exec stuff and then pipe it to sh.</p>\n\n<pre><code>root@ifrit findtest # find -type f -exec echo ls $"|" cat \\;|sh\nfilename\n</code></pre>\n\n<p><br></p>\n\n<pre><code>root@ifrit findtest # find -type f -exec echo ls $"|" cat $"|" xargs cat\\;|sh\nh\n</code></pre>\n\n<p>I just figured I\'d add that because at least the way i visualized it, it was closer to the OP\'s original question of using pipes within exec.</p>\n'}]
['https://Stackoverflow.com/questions/62044', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3499/']