Unix – Renaming (Removing Spaces in Filenames)

Stephane Harnois :

foreach f (“`ls -1`”)
set file2 = echo “$f” | nawk ‘{print $1$2$3$4$5$6}’
mv “$f” $file2
end

Kevin Tengan :

Tested with perl 5.004 … It will change all the spaces in a filename to a “_”.

#!/usr/local/bin/perl5 -w
use strict;
my $dir = $ARGV[0] || die “usage: $0 <directory>\n”;
opendir DIR, $dir or die “Unable to open $dir: $!\n”;
foreach my $file (grep !/^\.\.?$/, readdir DIR) {
if ($file =~ /\s+/) {
(my $file2 = $file) =~ s/\s/_/g;
print “$dir/$file -> $dir/$file2\n”;
rename “$dir/$file”,”$dir/$file2″ or warn “Unable to rename: $!\n”;
}
}
closedir DIR;
__END__

Jean-Francois Panisset :

foreach filename (*)
mv -f “$filename” `echo $filename | tr ‘ ‘ ‘_’`
end

The tr command can be used to remap single characters or character ranges.
It can also be used to delete specific characters.

Although it can make your life miserable, UNIX allows you to use pretty much anything as a file name, with the following exceptions:

a file called “.” is the current directory
a file called “..” is the parent directory
/ is used to delimite path components

You don’t have any choice but to rename the files containing a “/” on the Mac before transfering them to your SGI system.
There were also several posts suggesting software to rename the files:

Macintosh:

GraphicConverter – http://www.lemkesoft.de/
Reads and writes almost every file format imaginable, it also does renaming. Inexpensive, fast!

Drop*Renamehttp://www.chaoticsoftware.com

File Buddyhttp://www.skytag.com/

PC:

NameWizhttp://www.windowz.com/

 

Submitter: Stephane Harnois, Kevin Tengan, Jean-Francois Panisset