How to use pipe within -exec in find
In order to be able to use a pipe, you need to execute a shell command, i.e. the command with the pipeline has to be a single command for -exec
.
find /path/to/dir -type f -print -exec sh -c "cat {} | head -1 | grep yourstring" \;
Note that the above is a Useless Use of Cat, that could be written as:
find /path/to/dir -type f -print -exec sh -c "head -1 {} | grep yourstring" \;
Another way to achieve what you want would be to say:
find /path/to/dir -type f -print -exec awk 'NR==1 && /yourstring/' {} \;