Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


Can I sequentially rename files in Linux?

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?


Dave's Answer:

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:

for filename in Screen*

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:

$ for filename in $(ls -rt Screen*)
> 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:

for filename in $(ls -rt Screen*|sed 's/ /__/g')

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:

newname="$pattern$count.png"
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:

filename="$(echo $filename | sed 's/__/ /g')"
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:

#!/bin/sh

# 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:

$ seqrename sequential-file-rename-

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!


More Useful Shell Script Programming Articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  




Categorized: Mac OS X Help , Shell Script Programming , Unix and Linux Help   (Article 9471, Written by )
Tagged: bash, bourne shell, file management, linux, shell script programming
Previous: Can I limit songs played on my iPod / iPhone to non-explicit?
Next: How do I reset my Windows Live password?




Reader Comments To Date: 3

L Duperval said, on June 18, 2010 12:07 PM:

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

Tim Watson said, on July 26, 2010 1:27 AM:

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

Jake said, on December 13, 2010 1:24 PM:

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

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.