bug-findutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to conditionally search?


From: James Youngman
Subject: Re: How to conditionally search?
Date: Sat, 28 Nov 2009 10:12:06 +0000

On Sat, Nov 28, 2009 at 12:09 AM, Peng Yu <address@hidden> wrote:
> On Fri, Nov 27, 2009 at 2:54 PM, Eric Blake <address@hidden> 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

[Prev in Thread] Current Thread [Next in Thread]