killspc - Remove Spaces In File Names Shell Script


If you need an automated script to remove spaces and replace them with the underscore in file names, then you may find the script shown here useful.

Why would you want this? Any reason will do fine....

Web browsers need spaces in file names or locations changed to a cryptic %20 if you find such files and pages on the Internet (best to change them first).

Some scripts misbehave, or act unpredictably when you give them file names that contain spaces (some scripts use a protective wrapper around values, some do not, and therefore may act unpredictably when file names with spaces appear).

If you have many years worth of files like "Aunt Helen's Birthday.jpg" or "Cici's Marathon.jpg" you may want an automated way to change the file names containing spaces to another character (like the underline or underscore character).

You may be scouting the Internet looking for any easy to understand script examples so that you can learn a little bit more about script programming for your own educational benefit.

Any reason will do fine....   ...the script you want, is shown below.


GET THE SCRIPT

This script was posted in one of the advocacy newsgroups many years ago and I think it is worth sharing. If you find it useful for your own purposes - Great!

You can download the script "killspc" or you can copy and paste the script shown below to a location where you keep handy utility programs:

#!/bin/sh
#
# (Hopefully) replace all occurences of spaces in filenames with an underscore character
#
# Expected Usage: killspc [regexp]
# regexp represents location and types of files to rename i.e $HOME/downloads/*
#

TOTAL_RENAMED=0
for NXT_FILE in "$@" ; do
	case "$NXT_FILE" in *' '*)
	OLD_NAME="$NXT_FILE"
	NEW_NAME=`echo "$OLD_NAME" | tr ' ' '_'`
	echo -e "Renaming file `basename "$OLD_NAME"` to `basename $NEW_NAME`...\c"
	OLDIFS="$IFS"
	IFS=:
	mv "$OLD_NAME" "$NEW_NAME"
	IFS="$OLDIFS"
	if [ $? -eq 0 ]; then
		echo "ok"
		TOTAL_RENAMED=$(( $TOTAL_RENAMED +1 ))
	else
		echo "failed"
	fi
	;;
	esac
done

After you download and save the script, use the chmod a+x killspc command to make the script executable.


RUN THE SCRIPT

This script is run from the Terminal command-line and expects to be fed filenames that can be converted. If you are unfamiliar with the command line, then continue here.

You can feed it one filename at a time, or give it a directory list all at once and let it go through the entire list all in one go.

Here is an example of giving it a few individual file names at a time:
1.
2.


3.


4.


5.
 
[you@genesis ~]$ cd Pictures
[you@genesis Pictures]$ ls -l
-rw-r--r-- 1 you you 8000 2011-01-01 00:01 Cats and Dogs.jpg
-rw-r--r-- 1 you you 9000 2011-01-01 00:01 Dogs and Cats.jpg
[you@genesis Pictures]$ ~/killspc Cats\ and\ Dogs.jpg "Dogs and Cats.jpg"       
Renaming file Cats and Dogs.jpg to Cats_and_Dogs.jpg...ok
Renaming file Dogs and Cats.jpg to Dogs_and_Cats.jpg...ok
[you@genesis Pictures]$ ls -l
-rw-r--r-- 1 you you 8000 2011-01-01 00:01 Cats_and_Dogs.jpg
-rw-r--r-- 1 you you 9000 2011-01-01 00:01 Dogs_and_Cats.jpg
[you@genesis Pictures]$ exit

If you are not familiar with the command-line, here is an explanation of the commands shown above:

  1. cd Pictures - Change to the directory where you have files that you want to rename (For this example, we assume you want directory "Pictures").

  2. ls -l - List the files in that directory (Good if you want to confirm spelling or if you have files that you want to change).

  3. ~/killspc Cats\ and\ Dogs.jpg "Dogs and Cats.jpg" - Run the script killspc (in this example, we assume you saved the script as file "killspc" in your "home" directory, which is directory "~"). If you specify individual files, then, file names with spaces in them need to have each space preceeded with the reverse slash character (See example "Cats\ and\ Dogs.jpg") or the entire file name needs to be enclosed in quotes (") without the reverse slash character (see example "Dogs and Cats.jpg")

  4. ls -l - List the files in that directory. Check to see if you changed them all.

  5. exit - You are done. Exit the command-line console.

Here is an example of using Wildcards to change a lot of file names at one time:
1.






2.




3.



4.
 
[you@genesis ~]$ ./killspc ~/Pictures/*Cats* ~/Music/*
Renaming file Birds and Cats.jpg to Birds_and_Cats.jpg...ok
Renaming file Cats and Dogs.jpg to Cats_and_Dogs.jpg...ok
Renaming file Dogs and Cats.jpg to Dogs_and_Cats.jpg...ok
Renaming file Bird Chirps.wav to Bird_Chirps.wav...ok
Renaming file Cat Calls.mp3 to Cat_Calls.wav...ok
Renaming file Dog Barks.wav to Dog_Barks.wav...ok
[you@genesis ~]$ ls -l -R Pictures
-rw-r--r-- 1 you you 6000 2011-01-01 00:01 Birds_and_Cats.jpg                   
-rw-r--r-- 1 you you 7000 2011-01-01 00:01 Birds and Dogs.jpg
-rw-r--r-- 1 you you 8000 2011-01-01 00:01 Cats_and_Dogs.jpg
-rw-r--r-- 1 you you 9000 2011-01-01 00:01 Dogs_and_Cats.jpg
[you@genesis ~]$ ls -l -R Music
-rw-r--r-- 1 you you 1111 2011-01-01 00:01 Bird_Chirps.wav
-rw-r--r-- 1 you you 2222 2011-01-01 00:01 Cat Calls.wav
-rw-r--r-- 1 you you 3333 2011-01-01 00:01 Dog_Barks.wav
[you@genesis ~]$ exit

  1. ./killspc ~/Pictures/*Cats* ~/Music/* - Run the script killspc (in this example, we assume you are in the same directory as file "killspc" so you need to preceed the script with "./" to make it run). In this example we want to change all files in directory "Pictures" and contain "Cats" in the file name. We also want to change all files in directory "Music")

  2. ls -l -R Pictures - List all files in directory "Pictures" and all sub-directories. Check to see if you changed them all. In this example, all files were changed except for one which did not have "Cats" as part of the file name.

  3. ls -l -R Music - List all files in directory "Music" and all sub-directories. Check to see if you changed them all. In this example, all files that had spaces in the file name were changed.

  4. exit - You are done. Exit the command-line console.

Other Pages On This Web Site
Home Page Backup With Tar Disk Image Backup BOINC On Linux
android.rules For adb Server Remove Duplicate Files Change MAC Address Java On Linux
Win At Roulette Ship Arcade PicDis Disassembler


Copyright© 2000..2023 Joe's Cat Website