LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 05-13-2009, 07:21 PM   #1
Trap
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:

5206.txt
5712.txt
6302.txt
7408.txt

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

1952-06.txt
1957-12.txt
1963-02.txt
1974-08.txt

... 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
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,318

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

Rep: Reputation: 241Reputation: 241Reputation: 241
if you have Python
Code:
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
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
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
Trap
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!!!
 
  


Reply


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 03:29 AM.

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