Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 05-13-2009, 07:21 PM   #1
LQ Newbie
Registered: Aug 2004
Posts: 13

Rep: Reputation: 0
Insert additional characters into filenames to rename them.

First off, I apologize if this question has been asked before. I've searched but came up with no answers with the search words I used.

I have a folder with a large number of files. They are named with the following pattern:


... etc.,etc.,etc.

The first 2 digits are the year and the last 2 are the month. They are all relating to the 1900's.

I am wanting to change all the filenames via a script so they end up in this format.


... etc.,etc.,etc.

I don't seem to be good enough with bash, regex, sed, etc. to figure out how to do this. Any ideas?
Old 05-13-2009, 08:15 PM   #2
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,481

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Any number of ways I would think - if all the files in the current dir are of that form, for bash try something like
for i in * ; do mv $i 19${i:0:2}-${i:2} ; done
Old 05-13-2009, 09:21 PM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 242Reputation: 242Reputation: 242
if you have Python
import glob,os
for files in glob.glob("*.txt"):
    os.rename(files, "19"+files[:2]+"-"+files[2:])
Old 05-13-2009, 10:28 PM   #4
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734Reputation: 734
for name in *; do newname=$(echo $name|sed -e 's/^/19/' -e 's/\(^....\)/\1-/'); mv $name $newname; done
(Just proving it can be done with SED.... Someone will surely have a more elegant SED solution, but probably NOT as elegant as the string substitution given by sygOO
Old 05-14-2009, 06:25 AM   #5
LQ Newbie
Registered: Aug 2004
Posts: 13

Original Poster
Rep: Reputation: 0
Thank you all for your suggestions. They all work. I do learn from such input. I dissect the code to get an understanding of what's actually taking place and why.

I appreciate your help. Thanks again!!!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Filenames with accented characters and XFS MQMan Linux - Software 2 07-31-2008 05:28 PM
Removing special characters from filenames PlymWS Linux - Software 1 08-09-2007 05:11 AM
script to insert filenames into files magnim Linux - General 4 03-22-2007 01:05 PM
Special characters in filenames gmartin Linux - General 2 01-05-2006 08:22 PM
bash and filenames with special characters CoolAJ86 Programming 2 03-09-2005 02:50 PM

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration