[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: |
Mon, 09 Apr 2007 11:40:56 -0400 |
User-agent: |
Gnus/5.110003 (No Gnus v0.3) Emacs/21.4 (gnu/linux) |
"Andreas R." <newsgroups2005@geekmail.de> 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