[Linux-disciples] Troubles with 'rename'

Stephen R Laniel steve at laniels.org
Tue Jun 21 16:03:46 EDT 2005


On Tue, Jun 21, 2005 at 03:57:45PM -0400, Adam Rosi-Kessel wrote:
> Um, it seems like what you want to do was answered in the script I
> posted that day:
> 
> http://lists.bostoncoop.net/pipermail/linux-disciples/2005-April/001222.html

I know that you responded with a script, but it bugs me that
we'd have to write a script to do recursive directory
traversal -- that's what 'find' does. We shouldn't have to
write that again.

> One problem with your script is it gets sticky with depth vs. breadth
> ... the directory list in the for loop is generated first, and a parent
> directory in your path may already be renamed by the time you get to it.

That's what the -depth option to find does:

       -depth Process each directorys contents before the directory itself.

...which is part of what's bugged me about why I can't get
find to do this: it has all the tools already built in to do
what we need. It shouldn't be necessary do any scripting
whatever for something like this.

Actually, I believe the -execdir option to find would even
obviate the need to do the 'cd' step that I included:

       -execdir command ;

       -execdir command {} +
	      Like -exec, but the specified command is run from the  subdirec
	      tory  containing	the  matched  file,  which is not normally the
	      directory in which you started find.  This a  much  more	secure
	      method  for invoking commands, as it avoids race conditions dur
	      ing resolution of the paths to the matched files.  As  with  the
	      -exec option, the + form of -execdir will build a command line
	      to process more than one matched file, but any given  invocation
	      of command will only list files that exist in the same subdirec
	      tory.  If you use this option, you must ensure that  your  $PATH
	      environment  variable  does not reference the current directory;
	      otherwise, an attacker can run any commands they like by leaving
	      an appropriately-named file in a directory in which you will run
	      -execdir.

Because, again, it seems like the problem with the rename
command is that it won't allow you to do 's/_/ /g' if the
file's parent directory contains spaces in its name. So the
trick should be to cd into the directory first, rename the
file, then move up to the parent directory. So this should
do the job:

find . -depth -iname '*\ *' -execdir rename 's/ /_/g' '{}' \;

but it doesn't, quite.

-- 
Stephen R. Laniel
steve at laniels.org
+(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20050621/e0856869/attachment.pgp


More information about the Linux-disciples mailing list