Recently I wrote a blog about proper file naming. This article taught me that file names should not have underscores to separate multiple word file names (i.e. this_is_a_long_file_name_with_underscores). Instead, they should have dashes (i.e. this-is-a-long-file-name-with-dashes).
Why? Because if the file is a link, the underline from the link will clash with the underscore… therefore dashes are supreme. You are making your site more accessible by using dashes instead of underscores.
I am a teacher by profession so I let all my students know of my recent file naming philosophical change. Most didn’t think it was a big deal and some thought I was making something out of nothing. But I hope they see that this field of web development is alive and constantly changing. Remaining static is a recipe for extinction. Ask MySpace if you don’t believe me.
Now I have created a huge problem. All of my sites and files are using underscores in the file names. I could decide to switch to the new naming convention now and forget the old stuff but I thought this would be a great time to show the power of Unix and the Terminal. How can I batch rename files? Manually changing underscores to dashes would be a nightmare. Not with Unix, it’s not.
Open the Terminal and browse to the current directory where you want to batch rename files. (use this article if you don’t know how to do this).
Type this command in the Terminal (after the ‘$‘)
That line finds all the underscores in the current directory and replaces them with dashes. It then creates a file called rename.txt and writes what the change will look like in that file. To see that this file was actually created type the following in the Terminal:
Now to see what is actually inside that file type the following inside the Terminal:
Why did we create rename.txt? To test to see if what we are trying to do, we actually do. If you are satisfied with the results, type the following inside the Terminal (if you are not satisfied, keep tweaking the line until it does what you want.)
Now the changes have been made to the file names directly.
To verify this is true type the following into the Terminal:
If this did not make sense or work for you. Read this fantastic article which gave me the info I needed to make this work.