LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-27-2009, 08:53 PM   #1
Mark007
Member
 
Registered: Apr 2004
Location: Sussex, UK
Distribution: Centos, Cisco IOS, Win<n>X64
Posts: 34

Rep: Reputation: 15
rename a load of files.... (Change one character!)


Hi all,

I need to rename a load of files, in fact only one character needs to be changed.

i.e.


abc-def.more.files.day.month.year.ext

to

abc.def.more.files.day.month.year.ext



As you can see, I only want to change the fourth charter in from a dash to a period.... there's about 500 files to do, so a "batch" job would be better..

What's the best away to do this, I don't speak regexp yet. so looking for some ideas/guidance of the easiest way.


TIA

Mark
 
Old 03-27-2009, 09:16 PM   #2
number-g
LQ Newbie
 
Registered: Mar 2009
Distribution: Debian
Posts: 16

Rep: Reputation: 2
if you have tcl installed you could write this small script to do it:

Code:
#!/usr/bin/tclsh

set path "/path/to/your/files"
set files [glob -directory $path *]


foreach originalname $files {
regsub -all \\\- $originalname . newname
exec mv $originalname $newname
}
then chmod the file to 755 and execute it

Last edited by number-g; 03-27-2009 at 09:19 PM. Reason: neatened
 
Old 03-27-2009, 09:19 PM   #3
Udi
Member
 
Registered: Jan 2009
Posts: 165

Rep: Reputation: 44
I'm sure there is a better way to do it, but this is what I would do:

for f in `ls abc-def*`; do mv $f ${f/-/.}; done

Explanation:
1) We loop over the files that look like abc-def*
2) We do mv (to rename) from $f to ${f/-/.} - which replaces the first dash it finds with a dot

Hope it helps.
 
Old 03-27-2009, 09:27 PM   #4
jman82s
Member
 
Registered: Jul 2007
Location: Central Coast, California
Distribution: "distro-hopper"
Posts: 66

Rep: Reputation: 15
Alternately, there are some pretty good graphical apps to do this: Gprename for Gnome, and KRename for KDE.
 
Old 03-27-2009, 09:28 PM   #5
number-g
LQ Newbie
 
Registered: Mar 2009
Distribution: Debian
Posts: 16

Rep: Reputation: 2
lovely
 
Old 03-27-2009, 09:39 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Udi View Post
Explanation:
1) We loop over the files that look like abc-def*
no need "ls".
Code:
for f in abc*
 
Old 03-27-2009, 10:02 PM   #7
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 6,870

Rep: Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249Reputation: 1249
Code:
rename abc- abc. abc*
 
Old 03-27-2009, 10:23 PM   #8
stratotak
Member
 
Registered: Sep 2005
Distribution: Today Debian . Tomorrow ..??
Posts: 386

Rep: Reputation: 39
I tried to something like that about 2-3 weeks ago or so.I had to change file extension on alot of files withing a bunch of subfolders..ended up using PyRenamer.It pretty much will cover anything that you need do.
http://www.linuxquestions.org/questi...-in-it-710065/

Last edited by stratotak; 03-27-2009 at 10:28 PM.
 
Old 03-29-2009, 02:00 PM   #9
Mark007
Member
 
Registered: Apr 2004
Location: Sussex, UK
Distribution: Centos, Cisco IOS, Win<n>X64
Posts: 34

Original Poster
Rep: Reputation: 15
Resolved.

Guys,

Many thanks for your suggestions..

The rename abc def ghi did the trick, something nice and simple.


but thanks for your answers, as always with linux, there's always six zillion ways to do something, some simple, some contrived.. whatever your milage etc. and how an individual wants to do it.

I'm trying to keep away from Guis atm, simply because you learn more (plus the linux box in now tucked away in a different room (loft!)

MarkA
Sussex.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename or change an ethernet interface gw1500se Mandriva 3 11-29-2008 05:57 PM
bash script to rename files removing one character Byenary Linux - Newbie 2 04-08-2008 11:12 AM
rename last character in file/folder name BoraX Programming 9 10-26-2006 11:01 AM
rename script won't change uppercase to lowercase on fat32 pwc101 Programming 4 09-27-2006 06:16 PM
Character Device driver: How to load it automatically ? shendeswanand Linux - Software 0 02-09-2006 10:59 PM


All times are GMT -5. The time now is 08:40 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration