Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to remove spaces from filenames UNIX
Perhaps you're like me, and have several years worth of JPEG files with names like "Aunt Sue's Birthday.jpg" These are a pain to deal with when putting together a web page, because most browsers don't handle spaces in file names well - you have to escape them with %20 or similar. I finally wrote a shell script that squeezes the spaces out of file names

Read the rest of the article for the script and a method for using it from the Finder instead of the Terminal...

The script:
#!/bin/sh
# this shell script removes the spaces out of the name of every file argument
# passed to it (handles an arbitrary number of files on the command line).
# example:
# sqzspaces.sh "/Users/Kurt/Name With Spaces"
# results in changing the filename to "/Users/Kurt/NameWithSpaces"

for OriginalFile
do

Location=`dirname "$OriginalFile"`
FileName=`basename "$OriginalFile"`

ShortName=`echo $FileName | sed 's/ //g'`

if [ $ShortName != "$FileName" ]
then
cd "$Location"
mv "$FileName" "$ShortName"
fi

done
[Editor's note: Copy and paste the text into a text editor, save the script, and then make it executable (chmod 755 sqzspaces.sh in the Terminal). To make it run from anywhere, make sure it's in a folder on your path -- I use ~/bin for my scripts, for example.]

The script assumes that the full pathname of the file is being given on the command line - which is what happens if you drag and drop a filename from Finder to the Terminal.

The real beauty of a script like this is that you can open it with ScriptGUI, a shareware application, and then Save As a scriptlet. Then all you have to do to perform this type of renaming is drag and drop the file(s) you want changed onto the scriptlet, and it will magically remove the spaces from the filenames. No need to open the Terminal!
    •    
  • Currently 2.75 / 5
  You rated: 4 / 5 (4 votes cast)
 
[30,184 views]  

A script to remove spaces from filenames | 12 comments | Create New Account
Click here to return to the 'A script to remove spaces from filenames' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Here's how to do it in Applescript
Authored by: Robert M on Jun 12, '02 01:03:33AM

on open something
tell application "Finder"
activate
set TheFile to a reference to item (something as string)
set TheName to (name of TheFile) as string
set the_Text to TheName
set Replace_string to ""
set OldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set newText to text items of the_Text
set AppleScript's text item delimiters to Replace_string
set finalText to newText as text
display dialog finalText
set the (name of TheFile) to finalText
end tell
end open

I got major help from http://discussions.info.apple.com
but that's cool , too, how to do it from the Terminal!

--Robert M



[ Reply to This | # ]
Here's how to do it in Applescript
Authored by: osxpounder on May 20, '03 05:02:34PM

That's really cool. It would be even cooler if we could figure out how to make an AppleScript that you could drop multiple files upon, which would change each file's name to one without spaces. Apparently this one won't; it gets confused if I drop a handful of files on it.

---
--
osxpounder



[ Reply to This | # ]
Here's how to do it in Applescript
Authored by: ericgorr on Sep 14, '06 06:53:04AM

If one is just looking to remove spaces, this might be a bit easier:


set newText to oldText's words as string



[ Reply to This | # ]
There's a Bug
Authored by: thinkyhead on Jun 12, '02 04:17:13AM
If the new name for a file / folder matches the name of an existing file / folder something very bad will happen to that file / folder. Put this before the "if" statement to avoid the problem:
x=1 BaseName=$ShortName while [ -e "$ShortName" ]; do ShortName="$BaseName$x" let x=x+1 done


[ Reply to This | # ]
And a bug in my bug-fix
Authored by: thinkyhead on Jun 12, '02 04:20:25AM
Use this in place of the original "if" statement I provided:
while [ -e "$Location/$ShortName" ]; do


[ Reply to This | # ]
Alternate Method
Authored by: THEM on Jun 12, '02 12:01:44PM
Here's an alternate script that gets rid of space in file names. cd to the folder with files you want to change then run this script. If you'd prefer to make spaces an underscore, swap the comment lines.
#! /bin/sh

for n in *
do
OldName=$n
NewName=`echo $n | tr -d " "`
#NewName=`echo $n | tr -s " " "_"`
echo $NewName
mv "$OldName" "$NewName"
done


[ Reply to This | # ]
Run this script anywhere?
Authored by: osxpounder on May 20, '03 02:36:14PM

I'd love to use this, but first I must clear up a more basic mystery [to me; I bet most of you know this]: how can I make it so that my script will run even if I'm not the folder that has the script? There is a way to define a "path" or two in *nix, as in DOS, but I don't know how it's done in *nix or OSX. Can some one point me to a hint for that?

And would that path issue explain why I can't get man pages? I always get "man page not found".

---
--
osxpounder



[ Reply to This | # ]
What does the first line mean?
Authored by: osxpounder on May 20, '03 02:38:40PM

By the way, what does the line beginning with "#! ..." mean?

It is a comment, right? I.e., it doesn't do anything, right? If that's the case, why the "!" ... what is this line supposed to be telling us?

---
--
osxpounder



[ Reply to This | # ]
Another alternative
Authored by: lindman on Jun 13, '02 12:00:42AM

If you're working with images use QPict. This app has a batch processor that lets you do just about anything with the filename.

http://www.qpict.net



[ Reply to This | # ]
A script to remove spaces from filenames
Authored by: hellesoe on Mar 10, '08 03:08:06PM

the original script works fine, but would it be possible to make it work on all underlying files and directories?

I need to run a shell script to remove spaces from files and file names in a certain folder every day. can this be done??



[ Reply to This | # ]
A script to remove spaces from filenames
Authored by: maintain1 on Mar 10, '08 05:15:10PM

#!/usr/bin/perl -w

# mvSpace.pl
# WARNING
## This will overwrite files if they have the same name
## So say we have file 1, f ile1, file 1
# After the script we will have one folder called folder1
#
# Created by mar8 on 3/10/08.
#
# the chdir command should change to a users home directory
chdir;
# you could enter the full path name to the folder
$workingDir = "\./Documents/spacefolder/";
opendir DH, "$workingDir" or die ;
my @workingFiles;
my $workingFiles_r = \@workingFiles;
while ($_ = readdir(DH)) {

next if $_ eq "." or $_ eq ".." ;
push @{workingFiles_r}, $_;
}
closedir DH;
foreach (@{workingFiles_r}) {


my $input = "$workingDir$_";
#print "my input is equal to ", $input,"\n";
s/\ //g;
my $output = "$workingDir$_";
#print "my output is equal to ", $output,"\n";

if ("$input" ne "$output") {
rename "$input", "$output" or warn "Can't move file: $!";
}
}



[ Reply to This | # ]
A script to remove spaces from filenames
Authored by: degiy on Aug 13, '10 03:06:00AM

>out;ls -1 | grep ' ' | tr ' ' '!' | while read a ; do b=`echo $a | tr '!' '_'`; echo "mv ,$a, $b" ;done | tr , \' | tr ! ' ' > out ; . ./out; \rm -f out



[ Reply to This | # ]