|
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? 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 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 pattern="$1" for filename in $(ls -tr Screen* | sed 's/ /__/g') 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 That's it. This should work just fine for your requirements too, and good luck to you!
Categorized:
Mac OS X Help
,
Shell Script Programming
,
Unix and Linux Help
(Article 9471,
Written by Dave Taylor)
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? Subscribe!
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 Posted by: L Duperval at June 18, 2010 12:07 PM"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). Posted by: Tim Watson at July 26, 2010 1:27 AMis it possible to have the renaming sequence to be 0x for single digit numbers? you get I have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
Recommended
Recent Entries
Search
I Need Help!
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and RSS Feeds Building Web Site Traffic Business and Management CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above Facebook Help Google Plus Help HTML and CSS Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Mac OS X Help Pay Per Click (PPC) Advertising Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help WordPress Help |