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 08-22-2012, 09:05 AM   #1
biki_happy
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: Disabled
Smile rename multiple files at a time


Hi,
I have number of files named 11.log,12.log,...........,20.log. I want to rename/move them as 10.log,11.log,.........,19.log respectively. Please suggest a script.

Thanks,
 
Old 08-22-2012, 10:39 AM   #2
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
Do you see the box at the bottom titled "Similar Threads"? Start by looking there. Or use the LQ search feature. If you had done that before you posted you would've found some of the hundreds of previous threads asking the exact same thing.
 
Old 08-22-2012, 12:14 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Code:
for filename in $(ls); do
     newfilename = <insert code to determine new file name>
     mv $filename $newfilename
done
Depending on where the files are, you might need "ls -R" instead of ls, and you might need to test for directories in the ls listing
 
Old 08-22-2012, 01:20 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Make use of mv(1)'s -i option when testing / building this sort of script -- or learn the hard way.

My 2 cents.
 
Old 08-22-2012, 09:49 PM   #5
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 90
@ pixellany: Would this work?

Code:
#!/bin/bash
x=10
for i in * 
do 
   j=$(printf "%.2d_$i\n" $x)
   mv $i $j
   x=$((x+1))
done
unset x
exit 0
I'm thinking since he wants to start the numbering of files with 10. My 2 cents worth also

Last edited by SharpyWarpy; 08-22-2012 at 09:51 PM. Reason: Targeting pixellany!
 
Old 08-22-2012, 10:27 PM   #6
sKaar
Member
 
Registered: Jun 2006
Location: dartmouth, nova scotia
Distribution: slackware 12.1
Posts: 74

Rep: Reputation: 2
hmm, on slackware there's a script already built, called, magically enough, rename, i'm sure it's in other distros.
 
Old 08-22-2012, 10:43 PM   #7
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 90
Quote:
Originally Posted by sKaar View Post
hmm, on slackware there's a script already built, called, magically enough, rename, i'm sure it's in other distros.
Yes, rename is very common. Unfortunatly it might be confusing to use this way. At least for me. That's because I have the evil handicap of misunderstanding things that SHOULD be easy.
 
Old 08-23-2012, 05:22 AM   #8
sKaar
Member
 
Registered: Jun 2006
Location: dartmouth, nova scotia
Distribution: slackware 12.1
Posts: 74

Rep: Reputation: 2
mmm, yah, i still have to look up the way it's used when ever i actually have to use it, man rename helps for a refresher, but, dummy files to test may help.
 
Old 08-23-2012, 09:19 AM   #9
biki_happy
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Smile rename multiple files at a time

Thanks to all for the replies. However, I have successfully done it using the script


x=1
for i in `seq 11 20 `
do
z=$(($i - $x))
mv $i.log $z.log
done
exit 0

Thanks to my friends Sudhir and Sooraj, with whom I have discussed and finally Sudhir came up with success.

Thanks.......
 
  


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 multiple files L1nuxn00b703 Linux - Newbie 4 04-11-2012 03:30 AM
rename multiple files tjay1983 Programming 3 08-10-2010 01:43 AM
How to rename multiple files with the same extension rogueeve Linux - Newbie 3 05-18-2007 02:54 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 08:17 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