A script to remove spaces from a file name

Posted on August 1, 2006. Filed under: compigiri, Computer, script |

Removing spaces from file names

I face a lot of problem while dealing with files with spaces in their names on the Unix platform. I wrote a script yesterday to change the spaces in a file name to undescores.

It is very useful for changing the names of all files in a directory along with directory names.

Save the script to your home directory or to /usr/bin or some such directory,

Go to the directory in which you want to effect the name change and run
# find . -depth -exec ~/rename.sh {} \;

This will rename all files in the directory so that the spaces in filenames are changed to underscores.

The script is as follows:

# Writen by Mayuresh Phadke (mayuresh at gmail.com)# To change the names of all files in a directory including directory names
# run the command
#  find . -depth -exec ~/rename.sh {} ;
# This command is pretty useful if you have a collection of songs or pictures transferred
# from your windows machine and you are finding it difficult to handle the
# spaces in the filenames on UNIX
#set -x

progname=`basename $0`

if [ $# != 1 ]
        echo "Usage: $progname \"file name with spaces\""
        echo "This utility is useful for renaming files with spaces in the filename. Spaces in the filename are replaced with _"
        echo "\"file name with spaces\" will be renamed to \"file_name_with_spaces\""
        exit 1

dir=`dirname "$1"`
file=`basename "$1"`

new_file=`echo $file|sed "s/ /_/g"`

if [ "$old_name" != "$new_name" ]
        mv "$old_name" "$new_name"

exit 0

Please let me know if you find this useful/useless or if you have any other comments about it.

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

23 Responses to “A script to remove spaces from a file name”

RSS Feed for Abstract-n-Concrete Comments RSS Feed

Changing ‘if [ $# != 2 ]’ to ‘if [ $# != 1 ]’ will let it actually run. Thank you, it was a help today.

Thanks Rixter for pointing this out. It’s fixed now.

I use a simple trick to do the same.. it is a single command you can type anytime in bash shell… You can set an alias or shell function to do the same in your bashrc..

find . -type f | while read i;do [ “$i” != “${i// /_}” ] && mv “$i” “${i// /_}” ;done


Does ur script rename directories as well?

Yes, depends on what u pass in find. if you say ‘-type d’ then it will rename only directories. If you remove -type itself, it will rename both files and directories.

for a in `find . -name ‘* *’`;do mv -i “$a” ${a// /_};done

To rename just the files:
for a in `find . -type f -name ‘* *’`;do mv -i “$a” ${a// /_};done

To rename just the dirs:
for a in `find . -type d -name ‘* *’`;do mv -i “$a” ${a// /_};done

To rename just the files in the current dir:
for a in `find . -span -type f -name ‘* *’`;do mv -i “$a” ${a// /_};done

To only rename everything in the current folder, you don’t need find:
for a in *\ *;do mv -i “$a” ${a// /_};done

And so on.

If you use the example in the script, change the line

find . -depth -exec ~/rename.sh {} ;


find . -depth -exec ~/rename.sh {} \;

in the comment. The “;” is missing there.

The “\” was missing, not the “;”

Thanks! This was useful!

thanx guys, this was a cool post..I could solve one of my problems.

Not sure but there might be a small typing error. In your blog you list:

find . -depth -exec ~/rename.sh {} \;

Now in the comments section of the script you have:

# find . -depth -exec ~/rename.sh {} ;

It appears that there is a backslash missing from the find command.

PS: Keep up the good work.

A long time ago I put backslashes and other offenders in my file names.
I am currently backing up and cannot copy these files,
Can you help with this or recommend a script that will find and change these offenders / * ^ #
so I can back up lod files.

Thanks Mayuresh, this was a big help. We used it to rename a bunch of photo filenames that had spaces and parenthesis. To get it to remove other characters (like a parenthesis) just add a new line after new_file=`echo …`, for example:
new_file=’echo $file|sed “s/(/_/g”‘ got rid of my parenthesis

Thank you very much. This worked wonderfully for a couple of old windows directories.

Works great! Thanks.

Thanks for the nice little script.. I was just going to whip up a script for myself and figured somebody else probably already created one. Very handy and great job.

[…] first one I found was this one which worked really […]

thanks you, this worked nicly, A+

works like a charm

Thank you, just what I was looking for

I was in a similar situation so I used this with success:

for i in *; do j=`echo $i | tr ‘ ‘ _`; mv “$i” $j; done;

Sorry to ask a dumb question here, but I don’t seem to be having much luck.

I’ve copy pasted the script above into gedit, save it as rename. Then ran chmod 777 rename. Then ./rename The script seems to run printing “the filename are replaced with _ “file name with spaces” will be renamed to file_name_with_spaces””
But none of my files in that directory are actually renamed. They all still have their spaces!
Please tell me what I am doing wrong. Thanks.

works perfect. mayuresh thanks for sharing the script.

Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: