Sometimes, you get an O/S that puts spaces in the names, and that's messy when you're using commands that aren't all that happy with it (like xargs).

So, try something like this...

use File::Find; finddepth sub { # finddepth so directories are done AFTER their co +ntents my $old = $_; tr/ /_/ or return; # don't do anything if you don't have spaces -e and return; # don't rename over an existing file! rename $old, $_ or warn "cannot rename $old to $_: $!"; }, "/top/dir/here";

Replies are listed 'Best First'.
RE: rename directories recursively to remove spaces from names
by AltBlue (Chaplain) on Aug 17, 2000 at 18:11 UTC
    hehe... very good example for File::Find usage...
    i come to suggest a oneliner shell version of it that i often use in my everyday work for verious purposes:
    find2perl . -depth -eval 'my $o=$_; tr/_/ /; -e or rename $o,$_ or warn "cannot rename $o to $_: $!"' | perl

    it's quick to use/modify action(s) (the strong area) without snooping into misc source code for any even beginner into regex craft ;-P

    {
    i like 'spaced' filenames :) ... also it's more difficult sometimes to parse those files =\ ... you'll always find a solution: *dumb example* 'ls -b | xargs ls -l' :))
    }

    --
    AltBlue.

Re: rename directories recursively to remove spaces from names
by fundflow (Chaplain) on May 12, 2003 at 05:25 UTC
    Just to mention, `xargs` has a -0 option that lets it deal with spaces in file names. The (GNU) Unix `find` has a matching switch that makes it produce null terminated lists