Gnuru.org
Productive Linux


Subscribe

 Subscribe via Feedburner in a reader

Enter your email address:

Delivered by FeedBurner


Login
Login:
Password:



Don't have an account?
Sign up to Gnuru.org
Forgot your password?

Recursively renaming files in a directory tree
3 August 2004 @ 16:08 BST
by Paul

This is a perl-ish way of renaming all files in a directory tree. This removes all non-word characters:

#!/usr/bin/perl

use File::Find;
use File::Copy;
use warnings;
use strict;

find ( sub  {
	 return unless -f ;
	 my $foo;
	 move($_, $foo) if  ($foo = $_) =~ s/\W//g;
       },
       '.' );

Here's a version that maintains extensions:
#!/usr/bin/perl

use File::Find;
use File::Copy;
use warnings;
use strict;

find ( sub  {
	 return unless -f ;
	 my $new;
	 move($_, $new ) if ($new = $_) =~ s/[^\w\.]|\.(?!\w+$)//g;
       },
       '.' );




Leave a comment:

Are you human?