As part of some user interface testing I’m involved with on an Ubuntu Linux system, I find myself frequently having to rename groups of files in a sequential manner. That is, I’ll have files like “output mm-dd-yyyy at hh:mm:ss” and need to rename them to “testrun-xx-file-yy”. Is there any way to automate this so I don’t have to go crazy typing?
You’re asking about a fairly straightforward shell script programming task, actually, which I think can be easily solved by recognizing that there are three basic steps:
1. create an ordered list of the files to rename
2. create a transformation formula that’ll turn old name –> new name
3. rename the old file appropriately
Agreed? Turns out your problem is remarkably similar to one I solved recently as part of how I manage the screen shot process when I’m writing articles here on Ask Dave Taylor. Until the latest dual-graphics-processor MacBook Pro arrived in the office, I was a long-time aficionado of Ambrosia Software’s Snapz Pro X, but on the new i5 and i7 core computers, the program forces the system to run the more powerful (read way more battery intensive) graphics subsystem. Long story short, I’m not using it any more and instead just use Cmd-Shift-3 to get full-screen captures that I later trim and edit.
The editing is no big deal, but the files are all named with cheery names like Screen shot 2010-06-13 at 5.24.58 PM, names that are non-descriptive and Web unfriendly. Instead, my image file names are more typically “reset-windows-live-password-1.png”, “reset-windows-live-password-2.png”, and so on. Useful, friendly, but not something I want to type over and over!
Since the Mac has a command line interface that is Posix compliant – no surprise given it’s running a variant of Linux under the hood – it turns out that the command line solution for my Mac will work for your Ubuntu Linux box too.
The first script step is to identify the matching files within a loop, so that each can be processed sequentially. In this case, it’s done with:
Simple enough, but we can’t always guarantee that alphabetical sorting is going to get oldest to newest files. Instead, a better solution is to use “ls -tr Screen*”, which returns the filenames in oldest-to-newest order. Sort of:
> do
> echo filename = $filename
> done
filename = Screen
filename = shot
filename = 2010-06-13
filename = at
filename = 5.24.58
filename = PM.png
filename = Screen
filename = shot
filename = 2010-06-13
filename = at
filename = 5.28.36
filename = PM.png
See what’s happened? Since we’re using the “ls” to order by creation time, the for loop just sees a lot of words separated by spaces.
One way to fix this is to actually change the file separator (shell variable FS), but I tend to like a different solution:
This replaces each space with a double underscore, ensuring that filenames survive the for loop intact. It’s trivial to reverse the substitution later in the script, as needed.
That’s the first piece nailed, and if you dont’ have any spaces in your original file names, of course, this can be simplified.
How about the second part, the pattern? To make my script as flexible as possible, I have the new pattern specified on the command line. To rename all the “Screen shot” files to ‘reset-windows-live-password-XX’, I simply specify the base name, knowing that the script appends an incrementing value to each filename:
count=$(( $count + 1 ))
The “count” variable will need to have an initial numeric value (I suggest “1”, but you could use whatever you want) but once “pattern” is set, that’s all I need.
Finally, the third step is the rename itself, which requires that I decode the filename:
mv “$filename” “$newname”
Notice the use of the quotes: otherwise the “mv” command will fail when it sees filenames with spaces in them (yes, spaces really do make it a pain to work with filenames in shell scripts).
Let me put it all together so you can see the entire script now:
# Sequential rename – rename screen shot images sequentially
if [ $# -ne 1 ] ; then
echo “Usage: $(basename $0) replacement-pattern-”
exit 1
fi
pattern=”$1″
count=”1″
for filename in $(ls -tr Screen* | sed ‘s/ /__/g’)
do
newname=”$pattern$count.png”
filename=”$(echo $filename | sed ‘s/__/ /g’)”
echo “renaming \”$filename\” to $newname”
mv “$filename” “$newname”
count=$(( $count + 1 ))
done
exit 0
Now you can see all the pieces and how they work together. A quick demonstration:
renaming “Screen shot 2010-06-13 at 5.24.58 PM.png” to sequential-file-rename-1.png
renaming “Screen shot 2010-06-13 at 5.28.36 PM.png” to sequential-file-rename-2.png
renaming “Screen shot 2010-06-13 at 5.40.54 PM.png” to sequential-file-rename-3.png
That’s it. This should work just fine for your requirements too, and good luck to you!
is it possible to have the renaming sequence to be 0x for single digit numbers?
So instead of getting the output of *_1.png, *_2.png, etc…..
you get
*_01.png, *_02.png, etc….
“rename” renames multiple files according to a Perl expression renaming rule. For example (from the man page), to rename all files ending in “.bak” to strip the extension, you would use the command:
rename ‘s/\.bak$//’ *.bak
“man rename” in a terminal will show you how to use the command if it is installed as part of your linux distribution (it is installed by default on Ubuntu 10.04).
Another interesting one is to modify the base name of a file. For example, changing your “sequential-file-rename-” to “sfr-“. A similar approach can be used.
What would be really handy (not that this isn’t, mind you) is a generic way to do that with one script, where you specify an input pattern and an output pattern on the command line and it does the rest automagically.
L