[Techtalk] bash/perl script to rename files...

Sam Watkins sam at myinternet.com.au
Sun Oct 28 01:12:30 EST 2001

Walt <pippin at fred.net> wrote:
>I need a perl script that replaces spaces
>in a filename with underscores...
On Fri, Oct 26, 2001 at 02:34:56PM -0700, Sean McAfee wrote:
> The Perl Cookbook has a pretty nifty "rename" script:

That 'rename' script comes standard with debian's perl distro, at least.
If you type:

find -type f -print0 | xargs -0 rename 'tr/ /_/'

it will do what Walt wanted.  If you have directories with spaces, it's
a bit uglier - but Hey, that's what Perl's all about:

find -depth -print0 | xargs -0 rename '1 while s: ([^/]*)$:_$1:'

(yes, it DID take me a while to get that right!)

You might apply this to your whole filesystem if you're feeling really
dark on spaced-out filenames.

More information about the Techtalk mailing list