Perl Script to remove Spaces from file/dir names 14
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
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.
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.
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. :-)
cool - that works superb. THX
Thanks for the script. Out of the 10 scripts I found online to do this function only yours worked.
This worked great on my Mac!
Thanks!
Thanks a lot!
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.
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.
Perfect! Thank you!
Thanks for the help.
Thanks for the script. Just saved me a ton of time.
Ufff nice job!!!! Thanks for posting the code and make it so easy to use.
brilliant! Thanks!