LinuxQuestions.org
Register a domain and help support LQ
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 11-07-2006, 05:25 AM   #1
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
File Renaming Query


Hi.

I have a directory full of data files, each of which is named something like data_12345_xxz, the number string running from 00000 to 15000 say.

I need to be able to rename each of these data files as data_12345_bhz, the 'xx' being replaced by a 'bh' - without having to do each individually!

Would appreciate any help!
John Paul O'Donnell
 
Old 11-07-2006, 06:09 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You can do this fairly easily using the rename program which comes with perl. The syntax is a little mysterious if you don't understand perl regular expressions, so it might be a good idea to read about them before using it.

This command just shows what will be renamed to what without actually doing it - good to check that it's the right thing...
Code:
rename -n 's/data_(\d+)_xxz/data_$1_bhz/' data_*
...and this command actually does the rename:
Code:
rename 's/data_(\d+)_xxz/data_$1_bhz/' data_*
 
Old 11-07-2006, 10:25 AM   #3
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
The rename command is part of util-linux, not perl, at least on Slackware.

Also, you don't need to match the whole thing, only the part you want to change, e.g.

Code:
rename xx bh data*
should suffice.
 
Old 11-07-2006, 10:39 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by ioerror
The rename command is part of util-linux, not perl, at least on Slackware.

Also, you don't need to match the whole thing, only the part you want to change, e.g.

Code:
rename xx bh data*
should suffice.
Excellent, didn't know that one. Thanks. It's author of rename is Larry Wall. I had assumed it came with perl. In Ubuntu Edgy, dpkg claims not to know which package owns this file, which is a little annoying. Ho hum.
 
Old 11-07-2006, 10:56 AM   #5
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
Thanks very much. That's worked.
 
Old 11-07-2006, 11:08 AM   #6
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
Quote:
Originally Posted by matthewg42
Excellent, didn't know that one. Thanks. It's author of rename is Larry Wall. I had assumed it came with perl. In Ubuntu Edgy, dpkg claims not to know which package owns this file, which is a little annoying. Ho hum.
Ah, I didn't know LW wrote rename. The Slack man page is pretty minimal. It's possible it was supplied with perl originally. util-linux has sucked up loads of handy utilities into one package, just as core-utils unified sh-utils and a few other packages I can't remember the name of. I did a quick google for a history of rename but couldn't find anything of much interest.

And, yeah, rename only changes the pattern specified, the rest of the filename is unchanged. It took me a while to get my head around this, since it's completely different to every other "rename" command on various OSes and to sed etc. Dead handy command, though, and very easy to use once you grok it.

Last edited by ioerror; 11-07-2006 at 11:29 AM.
 
  


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
File renaming amushtaq Linux - Software 2 01-13-2006 06:40 AM
Complex file renaming Imyrryr Linux - General 3 04-29-2005 09:27 AM
renaming a file?? U-Toast Linux - Newbie 2 08-29-2004 02:07 AM
Bulk File Renaming nevereverend Linux - Newbie 11 07-03-2004 06:28 PM
renaming or converting a text file to a dat file... tangaz Linux - Software 1 10-24-2003 07:57 AM


All times are GMT -5. The time now is 06:36 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration