LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 02-16-2009, 01:18 PM   #1
yanik
Member
 
Registered: Oct 2003
Location: Montreal Beach
Distribution: Debian Unstable
Posts: 368

Rep: Reputation: 30
Mass renaming?


Hi everyone,

I have a text file that looks like this:

ydoucet@vplbna:~$ head zipNames.txt
2954505.zip 3332826.zip
2954506.zip 3320224.zip
2954507.zip 3518673.zip
2954508.zip 3320218.zip
2954509.zip 3320358.zip
2954510.zip 3320328.zip
2954512.zip 3332849.zip
2954513.zip 3332713.zip
2954514.zip 3333181.zip
2954515.zip 3320261.zip


the first column is the names of about 2000 files under the same directory. The second column is the new names. I'm not really good at scripting, can anyone help me start? I guess I should go with a "for" loop, but how do I tell it to look in the text file?

thx
 
Old 02-16-2009, 01:25 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
http://tldp.org/LDP/abs/html/ should be useful to you. Edit: specifically, this should help. To get the filenames into variables, I'd use awk. Let's say you've read a line of the file into the variable "line". Then, you can use the following to get the old and new filenames into variables "oldname" and "newname" respectively:

oldname=`echo $line | awk '{print $1}'`
newname=`echo $line | awk '{print $2}'`

You can then just use these variables with mv to rename the files, e.g.

mv $oldname $newname.

HTH.

Last edited by Nylex; 02-16-2009 at 01:26 PM.
 
Old 02-16-2009, 01:25 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Code:
sed 's/^/mv /' zipNames.txt >newZipNames.txt
Now you have a script that will rename the files.
 
Old 02-16-2009, 01:34 PM   #4
yanik
Member
 
Registered: Oct 2003
Location: Montreal Beach
Distribution: Debian Unstable
Posts: 368

Original Poster
Rep: Reputation: 30
Thank you Nylex for the ABS link, and thank you too jschiwal, so simple lol!
 
Old 02-16-2009, 03:21 PM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
My god, I really need to learn sed!
 
Old 02-16-2009, 04:12 PM   #6
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
You could also do:

Code:
while read line; do mv $(echo $line | awk '{ print $1 }') $(echo $line | awk '{ print $2 }'); done <zipNames.txt
 
Old 02-16-2009, 04:25 PM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Following suggestion by H_TeXMeX_H you can also read two variables per line:
Code:
while read oldname newname
do
  mv $oldname $newname
done < zipNames.txt
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mass renaming script frieza Programming 25 12-10-2007 04:56 AM
mass renaming icons for use with XFE? Mr_Shameless Linux - Software 2 06-02-2007 06:55 AM
Bash - Mass file renaming problem smudge|lala Linux - Software 2 02-14-2007 07:02 PM
mass mp3 directory renaming schbond Linux - General 4 01-05-2007 12:18 PM
a question about renaming files in mass! zeltak Linux - Software 1 05-24-2006 07:12 AM


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