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 03-27-2011, 04:24 AM   #1
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, XUbuntu 12.04
Posts: 498

Rep: Reputation: 37
Quick shell question: rename


I'm trying to get the syntax correct for a spot of bulk renaming. Should not need a script, just the right line in a shell!

Say I have lots of files that are named, for example:

This is an example file with spaces 401.txt

All the files will have this name, except the end number will vary: 401, 402, 403 etc.

I want to rename them:

FILE401.txt, FILE402.txt etc.

With my very basic and flawed knowledge of regexps, I did try:

rename -v *.(\d\d\d).txt FILE\1.txt

... which didn't work and...

sed *.(\d\d\d).txt FILE\1.txt

Actually this sed documentation is doing my head in. I suspect it's even more complex that regexps. And that's saying something.

Is this a task that could be done with rename, or do I have to resort to sed? Could I ask a kind soul reading this thread to tell me the exact command I should use for this specific task? Thank you.
 
Old 03-27-2011, 04:51 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Maybe something like:
Code:
rename -n 's/[^\d]*/FILE/' <file>
Take away '-n' to make it happen.
 
Old 03-27-2011, 05:21 AM   #3
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, XUbuntu 12.04
Posts: 498

Original Poster
Rep: Reputation: 37
I am getting the error bash: syntax error near unexpected token 'newline'. Is your solution meant to be typed in literally? I'm having trouble seeing how it works...
 
Old 03-27-2011, 06:28 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Apart from the '<file>' part, yes it should work as is ... at least it did for me with your example.
 
Old 03-28-2011, 02:36 AM   #5
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, XUbuntu 12.04
Posts: 498

Original Poster
Rep: Reputation: 37
Yes, but what do I put in place of <file>? I cannot put a specific piece of text, since the filename of the renamed file will vary, from one renamed file to another. I should clarify (which was not explicit in my original posting) that I was hoping to save time by running the rename command once on all the files in a folder, and it would rename all relevant files in one go...
 
Old 03-28-2011, 03:46 AM   #6
spazticclown
Member
 
Registered: Sep 2010
Distribution: Fedora, Android, CentOS
Posts: 91
Blog Entries: 2

Rep: Reputation: 21
The manual page has examples that may cover what you are doing, just remember to put your file names in quotes.

Hope this helps you out.
 
Old 03-28-2011, 04:47 AM   #7
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 45
Ruby(1.9+)

Code:
$ ruby -e 'Dir["*"].each {|x| File.rename(x , x.scan(/\d+\.txt/)[0] ) if x[/\d+\.txt/] }'
 
Old 03-28-2011, 04:59 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Well assuming you are in the directory where the file you listed is, you can just do:
Code:
rename -n 's/[^\d]*/FILE/' *
Again, this will not make the change until you remove the '-n' switch, but it will tell you the from and to name.
 
Old 03-28-2011, 12:18 PM   #9
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, XUbuntu 12.04
Posts: 498

Original Poster
Rep: Reputation: 37
Okay, thank you very much.
 
  


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
[SOLVED] Quick, simple, shell command question JoshConsulting Linux - Newbie 8 06-03-2010 07:13 PM
quick variable question in a shell script yargk Linux - Newbie 4 02-23-2010 03:09 PM
quick shell question kebabbert Solaris / OpenSolaris 10 10-27-2009 07:31 PM
Quick shell scripting question Komelore Programming 3 03-12-2007 09:39 AM
Quick Shell Question youssefe2k Linux - Software 2 04-10-2005 10:47 PM


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

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