The rename command…

Smoot Carl-Mitchell smoot at tic.com
Wed Aug 27 20:36:45 UTC 2008


On Wed, 2008-08-27 at 22:14 +0200, Johnny Rosenberg wrote:
> 2008/8/27 Ulf Rompe <Ulf.Rompe at icem.com>
>         On Mi, 2008-08-27 at 17:44 +0200, Ulf Rompe wrote:
>         > You may well do it with mv instead of rename:
>         >
>         > find . -name "Link to *" | while read name; do mv "$name"
>         "`echo $name | sed -e "s/Link to //"`"; done
>         
>         
>         ...at least if you replace the inner pair of double quotes
>         with single
>         quotes...
>         
>         [x] ulf
> 
> No, it worked the way you did.I tried both and it worked in both
> cases.
> Does ` have a special meaning? I tried to replace it with something
> else, such as ', but got errors. It looks unnecessary to have this
> construction ( <blah blah> "`<more blah blah>`" <even more stuff here>
> - it looks like either " or ` can be omitted, but it can't; I've
> tried…)

The backtic (`) encloses a shell pipeline and sends its output back into
the command stream instead of writing it to standard output.  What

mv "$name" "`echo $name | sed -e 's/Link to //'`"

does is echo the filename ($name) and pipe it as input to sed which
strips the "Link to " string in the filename and then inserts the output
from sed as the 2nd argument to the "mv" command.  The outer double
quotes insure the resulting filename will be parsed as a single string
even if it has whitespace in the name.

To be more exact the $name argument to echo should also be quoted so all
whitespace in $name is included in the output. Without the quoting
multiple blanks are interpreted as a single blank.

As you can see quoting can get pretty convoluted with complex pipelines
which include quoted strings and backtics and you have to be careful
about when to use single or double quotes, since double quotes allow you
to interpolate variables and single quotes do not.

-- 
Smoot Carl-Mitchell
System/Network Architect
smoot at tic.com
+1 480 922 7313
cell: +1 602 421 9005




More information about the ubuntu-users mailing list