Every once in a while, you may find yourself in a situation where you need to rename a whole batch of files on your Web server. In my case, I find this especially useful when someone provides me with a folder full of friendly-named files (files that contain spaces, special characters, etc.) and I want to make them a little more Web-friendly. On Linux and Unix-based computers, it’s really simple to do this from the command line. To do so, simply use a command similar to the following:
for file in *;
do mv "$file" `echo "$file" | sed 's/[^A-Za-z0-9_.]/_/g'`;
That series of commands will search for all files in your current directory. Then, the sed command will use a Perl-style regular expression to search all of the file names and replace characters where appropriate. The mv (move) command actually handles the renaming, based on the sed command’s output.
For the small subset of users that use Linux as their primary OS and maintain an active presence on MySpace (very small subset of users, I suspect), you may have discovered that MySpace’s photo upload feature is incapable of finding photographs when they end with an uppercase JPG extension. You can use this set of commands to change the JPG extension to jpg, allowing MySpace to locate your photos. That command would look something like:
for file in *;
do mv "$file" `echo "$file" | sed 's/\(.*\.\)JPG/\1jpg/g'`;
Unfortunately, the command above only seems to work with filenames that do not include spaces. I’m sure there’s a simple way to adjust it so that it works properly regardless of the way the filename is constructed, but I haven’t found it, yet.
I hope this quick lesson helps some people work more efficiently and effectively when dealing with large groups of files.