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 04-11-2012, 12:28 AM   #1
L1nuxn00b703
Member
 
Registered: Sep 2009
Posts: 113

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-11-2012, 12:35 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,259

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

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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, 02:53 AM   #4
L1nuxn00b703
Member
 
Registered: Sep 2009
Posts: 113

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, 03:30 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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


All times are GMT -5. The time now is 02:14 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