use File::Find; finddepth sub { # finddepth so directories are done AFTER their contents 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";