I would like to do the same as How can I use two bash commands in -exec of find command? but with grep as the second command. The solutions posted for that prior solution don't work when grep is the second command. Another question Combination of find and grep command with exec option inquires about using grep, but all the answers don't use grep. I think I need grep.
For example,
sudo find -D exec . -maxdepth 1 -type f -iname "*" -exec file -N '{}' \; -exec echo 'asdf' \;
works fine, but
sudo find -D exec . -maxdepth 1 -type f -iname "*" -exec file -N '{}' \; -exec grep "JPEG" {} \;
shows no evidence of grep doing anything. How can I force the stdout of the first command to stdin for grep? If I instead, pipe the file command output to a file and run grep separately on the file it works great:
dell@DELL-E6440:~$ rm junk.txt
dell@DELL-E6440:~$ sudo find -D exec . -maxdepth 1 -type f -iname "*" -exec file -N '{}' >> junk.txt \;
dell@DELL-E6440:~$ grep "JPEG" junk.txt
./150120-ssc-proxy~20190508-061623.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 150x150, segment length 16, baseline, precision 8, 1018x1426, frames 3
./avoid-powered-overfight~20190508-061623.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, comment: "Intel(R) JPEG Library, version [2.0.16.48]", baseline, precision 8, 1048x659, frames 3
dell@DELL-E6440:~$
but the point is I want to do it on one bash line, and >> doesn't properly flush the file between runs anyhow.
grep
because none of the files you rungrep
on contains the stringJPEG
. But thu command is definitely running. – Kusalananda Nov 08 '19 at 07:19junk.txt
file contains the stringJPEG
, because it contains the output of thefile
command. The image files do not contain the wordJPEG
themselves (except possibly by coincidence as part of the binary image data, but not in your case). Yourfind
commands runsfile
on the files, and thengrep
on the same files. Thefile
command generates the output you redirect intojunk.txt
while thegrep
command thatfind
executes does not generate any output. – Kusalananda Nov 09 '19 at 14:02