Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

Redirection or pipe inside `find -exec`

Writer 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 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?

1

2 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?

2

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy