Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Linux Help
  • Can I sequentially rename files in Linux?

Can I sequentially rename files in Linux?

June 18, 2010 / Dave Taylor / Linux Help, Linux Shell Script Programming, Mac & MacOS Help / 3 Comments

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:

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!

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
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!

3 comments on “Can I sequentially rename files in Linux?”

  1. Jake says:
    December 13, 2010 at 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….

    Reply
  2. Tim Watson says:
    July 26, 2010 at 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).

    Reply
  3. L Duperval says:
    June 18, 2010 at 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

    Reply

Leave a Reply Cancel reply

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

Search

Recent Posts

  • How Do You Lower the Volume on Apple Watch Alerts?
  • How do I Schedule a Google Meet call with Colleagues?
  • How Can I Add My Pronouns to my Instagram Account Profile?
  • Possible to Pair Bluetooth Headphones with my Vizio TV?
  • Possible to Schedule a Windows 11 System Update & Restart?

On Our YouTube Channel

ONFORU Outdoor Bluetooth LED Light-Up Lantern Speakers -- REVIEW

Google Pixel 6a Budget Android Smartphone -- DEMO & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help, (161)
  • Android Help (201)
  • Apple iPad Help (145)
  • Apple Watch Help (53)
  • Articles, Tutorials, and Reviews (344)
  • Auto Tech Help (11)
  • Business Advice (199)
  • Chrome OS Help (25)
  • Computer & Internet Basics (764)
  • d) None of the Above (165)
  • Facebook Help (383)
  • Google, Chrome & Gmail Help (180)
  • HTML & Web Page Design (245)
  • Instagram Help (48)
  • iPhone & iOS Help (607)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (93)
  • LinkedIn Help (85)
  • Linux Help (166)
  • Linux Shell Script Programming (87)
  • Mac & MacOS Help (894)
  • Most Popular (16)
  • Outlook & Office 365 Help (26)
  • PayPal Help (69)
  • Pinterest Help (53)
  • Reddit Help (18)
  • SEO & Marketing (81)
  • Spam, Scams & Security (92)
  • Trade Show News & Updates (23)
  • Twitter Help (217)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (921)
  • Wordpress Help (204)
  • Writing and Publishing (72)
  • YouTube Help (46)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (57)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


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 site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2022 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy