Batch rename Files with Unix

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 ‘$‘)

test file naming change with terminal

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:

[prettify]cat rename.txt[/prettify]

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.)

change file names with the Terminal

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.

7 Replies to “Batch rename Files with Unix”

  1. If you are on windows you can use the Cygwin Terminal and do the same thing! One of the things I hate about this post is I don’t have the code that you can copy and paste. That’s because I’m using Google prettify and for some reason I can’t figure out (I already wasted over an hour trying to figure it out) it boggles up the code. I think it’s an character entity issue so if someone know how to fix it.. please, by all means enlighten me. So I’ll just paste the code here to save you time:

    for filename in *_*; do echo mv \”$filename\” \”${filename//_/-}\”; done > rename.txt

  2. this is cool for renaming a file in unix (mac)
    let’s say you want to batch rename all the files in the current directory that have the term ‘wilshire’ to ‘wilshire-labrea’
    use this:
    ls *wilshire* | awk ‘{print(“mv “$1” “$1)}’ | sed ‘s/wilshire/wilshire-labrea/2’ | /bin/sh

Leave a Reply

Your email address will not be published. Required fields are marked *