Wednesday, April 16, 2008

A script to remove spaces from a file name

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:

#!/bin/bash
#
# 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 ]
then
echo "Usage: $progname \"file name with spaces\""
echo
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\""
echo
exit 1
fi

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

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

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

exit 0

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

No comments: