Perl Script to remove Spaces from file/dir names 14

Posted by Tom Willett Mon, 18 Sep 2006 16:53:00 GMT

The other day I moved a bunch of files from a windows server to a linux server. The files and directories on the windows server had spaces in them. Linux does not really like spaces in directory and file names.

I went looking around the net and could not find exactly what I wanted, so I took what I could find and modified it. At this point I cannot remember where I got the pieces, but the main brains for this script is not mine. Anyway here it is:

#!/usr/bin/perl -w
# nospace /this/dir /that/dir /those/too

use File::Find;
use strict;
die "usage: nospace dir[s]\n" unless @ARGV;

my %ext;

find(\&remspaces, @ARGV);

sub remspaces {
return if ($_ eq '.');
return if ($_ eq '..');
(my $new = $_) =~ tr/a-zA-Z0-9_.-/_/c;
my $duplicate = ($new ne $_ and -e $new);
my $try = $new;

$ext{"$File::Find::dir/$try"}++ if $duplicate;

while (my $count = $ext{"$File::Find::dir/$new"}++) {
(my $with_num = $new) =~ s/(?=\.|$)/_$count/;
$new = $with_num, last if not -e $with_num;
}

$ext{"$File::Find::dir/$try"}-- if $duplicate;

rename $_ => $new
or warn "can't rename $_ to $new: $!";
}

Copy above into a new file, save it as nospace, make it executable and run it by giving it a directory to work on. Thus if your files were in a directory called music call it with:

nospace music

Worked for me.

Tom
Comments

Leave a comment

  1. Azimov 10 days later:

    Hey nice script, i hade one problem, i won´t work for files within a directory with spaces in it. e.g. I have a file calle ¨my photo.jpg¨ within the folder ¨/home/me/photos/my pictures¨ the script can´t move into the folder if i just used the command ¨nospace photos¨.

    Is this just me using it wrong, the script is great apart from that tho.

  2. Tom 10 days later:

    To be honest, I never checked that particular issue. I just did and found that I had to run it twice, since it couldn’t change into the directory with a space after it had renamed it. But running it the second time succeded then went into the directory and renamed the files. Someday, I might look into it.

  3. Joe 17 days later:

    Great script, thank you very much saved me a lot of time and frustration going through rename script etc. as I’m a perl noob.

    Things like this make the world a better place. :-)

  4. TJ about 1 month later:

    cool - that works superb. THX

  5. Russell 3 months later:

    Thanks for the script. Out of the 10 scripts I found online to do this function only yours worked.

  6. dkl 5 months later:

    This worked great on my Mac!

    Thanks!

  7. AjD 5 months later:

    Thanks a lot!

  8. Any way to use UTF-8 characters? 9 months later:

    I have a bunch of files from Windows that are in Japanese. The filenames are on the lines of - 041507. I have a script that uses mencoder to re-encode the files so they’ll shrink. But I have to manually remove the spaces since the title of the show is in Japanese.

  9. Tom 9 months later:

    To be honest I don’t know. I would assume that if you had the character set set correctly on the host computer then it would. Put the console in utf-8 mode and try it on a couple of test files. Let us know if it works.

  10. Darley 10 months later:

    Perfect! Thank you!

  11. Sam 11 months later:

    Thanks for the help.

  12. toms about 1 year later:

    Thanks for the script. Just saved me a ton of time.

  13. donpicoro about 1 year later:

    Ufff nice job!!!! Thanks for posting the code and make it so easy to use.

  14. willie about 1 year later:

    brilliant! Thanks!

Comments