$dir = $ARGV[0]; # get dir from arg list opendir(D, $dir) or die $!; for(readdir(D)) { $file = $_; tr/ /_/; # or tr/ \t\r\n/_/; for tabs and spaces # or s/\s+/_/g; to make multiple whitespace chars into one _ rename("$dir/$file", "$dir/$_"); } closedir(D);