[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: basename is not working as expected if invoked in `` inside a find s
From: |
Paul Jarc |
Subject: |
Re: basename is not working as expected if invoked in `` inside a find subcommand |
Date: |
Tue, 10 Apr 2007 15:44:16 -0400 |
User-agent: |
Gnus/5.110003 (No Gnus v0.3) Emacs/21.4 (gnu/linux) |
"Andreas R." <newsgroups2005@geekmail.de> wrote:
> find dir1 -type f -exec sh -c 'echo "{}" dir2/"`basename "{}"`"' \;
> it works, but I still don't understand completely why it works.
Actually, it still won't work in some cases. If a filename contains a
double quote, like 'foo" "bar', the shell will see this command for
-c:
echo "foo" "bar" dir2/"`basename "foo" "bar"`"
which isn't what you want. This will handle any flename properly:
find dir1 -exec sh -c 'diff -q "$0" dir2/"`basename "$0"`"' {} \;
The difference here is that {} is substituted by find, before sh is
invoked, so if {} appears within the -c command, sh will interpret any
special characters in the filename as shell syntax, not as part of the
filename. Keeping {} as a separate argument to sh, and referring to
it as a variable from within the command, prevents any such problems.
> And where do you learn such tricks from? :)
Experience, mostly.
paul