Re: basename is not working as expected if invoked in `` inside a find subcommand

[email protected] (Paul Jarc)
Newsgroups gmane.comp.gnu.utils
Organization What did you have in mind? A short, blunt, human pyramid?
Message-ID <[email protected]>
"Andreas R." <[email protected]> wrote:
>> find dir1 -exec diff -q "{}" dir2/`basename {}` \;

Here, the command substitution is expanded by the shell before find
runs.  basename sees the literal argument {}, and so it outputs {},
and find sees dir2/{}.

>> find dir1 -exec sh -c "diff -q {} dir2/`basename {}`" \;

Here, agin, the command substitution is expanded before find runs.
Double quotes don't prevent an inner command substitution from being
expanded.  Single quotes will, though.  This will do what you want:

find dir1 -exec sh -c 'diff -q {} dir2/`basename {}`' \;

But if the filename contains any whitespace or shell metacharacters,
it'll cause trouble.  You can protect against that like this:

find dir1 -exec sh -c 'diff -q "$0" dir2/"`basename "$0"`"' {} \;


paul
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.