Redirection or pipe inside `find -exec`
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 directory
It 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?
Solution 1:
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?