Some Notes on Find Exec Output Redirect

Page content

Discovered an interesting thing today playing around find linux command.

The idea was to process several files in a directory using some utility that directs output to stdout. In order to keep tracking it’s useful to have a status file with a list of already processed files.

My first thought was to use something like this:

$ find . -name 'foo*' -exec echo "Processing file {}" >> status_file.txt \; -exec utility {} \;

but the problem is that all commands pulled using exec share same output channel. It means utility output is redirected to status_file.txt as well instead of stdout that is certainly not what I wanted.

Simle command chaining using ‘;’ or ‘&&’ does not work for find exec directly so I’ve decided to wrap commands with sh/bash:

$ find . -name 'foo*' -exec sh -c "echo 'Processing file {}' >> status.txt; utility {}" \;

In this case output redirect works as expected. Probably more elegant solution exists but who cares.