Redirection or pipe inside `find -exec`
Andrew Henderson
Any "find -exec" guru's around.
I have a folder of files I need to send to sendmail (they appear in there when something bad has happened).
The command
find . -type f -exec sendmail -t < {} \;gives me
-bash: {}: No such file or directoryIt doesn't seem to like the
<.And this
find . type -f -exec cat {} |sendmail -t \;gives me
find: missing argument to `-exec'It doesn't seem to like the
|.
What is wrong?
12 Answers
It looks like you'd like this redirection (<) or pipe (|) to belong to the inside of the -exec … ; statement. This doesn't work because they are handled by your shell before find even runs.
To make them work you need another shell inside -exec … ;. This other shell will handle < or |. Respectively:
find . -type f -exec sh -c 'sendmail -t < "$1"' sh {} \;
find . -type f -exec sh -c 'cat "$1" | sendmail -t' sh {} \;Note: find . -type f -exec sh -c 'sendmail -t < "{}"' \; is less complicated but wrong. This is explained here: Is it possible to use find -exec sh -c safely?
In both cases your redirection is parsed by the shell, not by find, so you need to escape it:
find . -type f -exec sendmail -f \< {} \;works as expected.
2