On Sat, Nov 28, 2009 at 12:09 AM, Peng Yu <[email protected]> wrote:
> On Fri, Nov 27, 2009 at 2:54 PM, Eric Blake <[email protected]> wrote:
>> find -name '*.py' -print -o -name '*.sh' \
>>  -exec sh -c 'test ! -f "${1%.sh}.py"' sh {} \; -print
>
> I don't quite understand how the command after -exec works. Would you
> please let me know how "-exec sh -c 'test ! -f "${1%.sh}.py"' sh {} \;
> -print" works?

If find is considering the file /tmp/a/foo.sh, it runs this shell command...

test ! -f "${1%.sh}.py"'

with $1 set to /tmp/a/foo.sh.    To see that work, type this:

set /tmp/a/foo.sh
test ! -f "${1%.sh}.py"'
echo $?

To understand in more detail, please read the documentation for the
shell (it's a feature of the GNU/Linux system, it's not built into
find).

The -exec primary succeeds if the shell command returns 0, that is, if
the .py file exists.    If the -exec primary succeeds, then the -print
primary will be executed.

Eric's answer combines find with other Unix tools.    Understanding
Unix shell programming more generally is essential to making good use
of find, especially in the case of more complex tasks.

James.


Reply via email to