LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 04-10-2012, 11:28 PM   #1
L1nuxn00b703
Member
 
Registered: Sep 2009
Posts: 117

Rep: Reputation: 15
Rename multiple files


How would I rename a bunch of files using only the one command or simplest way

from:

file.1
file.2
file.3
....
file.10

To

file.1.old
file.2.old
file.3.old
...
file.10.old

for loop? mv?
 
Old 04-10-2012, 11:35 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Code:
for i in file.*; do mv $i $i.old; done
Like that?
 
Old 04-10-2012, 11:37 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
A for loop would be most straight forward.

for file in file.[[:digit:]]*; do mv $file ${file}.old; done

If you wanted "old." prepended, you could use "rename"
rename '' old. file.[[:digit:]]*

On some distros, the rename command uses a perl styled expression. So read the rename manpage before using it.
 
Old 04-11-2012, 01:53 AM   #4
L1nuxn00b703
Member
 
Registered: Sep 2009
Posts: 117

Original Poster
Rep: Reputation: 15
ahhh that works... thanks guyz. Also, what is I wanted to change the files back:

file.1.old
file.2.old
file.3.old

To:

file.1
file.2
file.3
 
Old 04-11-2012, 02:30 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
For the record, if you had bothered to do a quick search or two of the forum or the net before posting, you likely would have found at least one of the many, many, many other discussions that have come up before on this topic.

Just take a look at the "similar threads" box at the bottom of this page for some examples.

PS: Don't forget to quote all of your variable expansions, everyone!

Last edited by David the H.; 04-11-2012 at 02:33 AM.
 
  


Reply



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
Another script to rename multiple files Becklan Programming 9 08-10-2010 09:18 AM
rename multiple files tjay1983 Programming 3 08-10-2010 12:43 AM
Rename multiple files in Bash damianpfister Programming 5 11-15-2009 08:30 AM
Rename multiple files in folders??? adds2one Linux - Software 19 10-05-2006 12:22 AM
How to rename multiple files? Rostfrei Linux - Newbie 3 07-11-2006 06:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:48 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
Open Source Consulting | Domain Registration