Quick tip for everyone who is using Mac OS X, Unix, or anything with bash. If you want to mass rename a bunch of files, there isn’t a simple way of doing it. Either you would have to do it manually1 or you could use this simple line of code.

ls ryan.* | awk '{print("mv "$1" "$1)}' | sed 's/ryan/steve/2' | /bin/sh

Let me explain it quickly. Let’s say you have a whole folder of files named ryan.*2 and you want to rename them to steve.*.

The first part displays a list of files that match the wildcard of ryan.* then it is piped to the second part ( awk '{print("mv "$1" "$1)}' ). This part prints a list of commands such as mv ryan.txt ryan.txt.

Well, that’s close but you want to move ryan.txt to steve.txt, not itself. This is where the third part comes in. What sed does in this situation is replace any instance of ryan with steve. The last part runs the whole thing as a list of shell commands.

  1. mv ryan.txt steve.txt []
  2. Such as ryan.txt, ryan.gif, ryan.php, ryan.asp, etc []

One Response to “Quickly, and efficiently, mass rename files in bash”

  1. This isn’t even simple and efficient. Would you like to type this on a daily basis?

    for i in ryan.*; do mv $i $(echo $i|sed ’s/ryan/steve/g’); done

    You should really take a look into zsh or zmv or mmv, they all can do it better than you did.

Leave a Reply