LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fuduntu
User Name
Password
Fuduntu This forum is for the discussion of Fuduntu Linux.

Notices


Reply
  Search this Thread
Old 06-06-2012, 08:12 AM   #1
carlosestensser
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Rep: Reputation: Disabled
rename files using text list


I have more than 400 files of books (coyright expired) that are named as follows:
L001.pdf
L002.pdf

etc.

I also have a list with the name of each book:
Quote:
L001 - Apollonius Rhodius -- Apollonius Rhodius. Argonautica
L002 - Appian -- Roman History I: Books 1-8.1
L003 - Appian -- Roman History II: Books 8.2-12
L004 - Appian -- Roman History III: The Civil Wars, Books 1-3.26
L005 - Appian -- Roman History IV: The Civil Wars, Books 3.27-5
L007N - Cicero -- Letters to Atticus I
L008N - Cicero -- Letters to Atticus II
L009 - Euripides -- Euripides I: Iphigenia at Aulis. Rhesus. Hecuba. The Daughters of Troy. Helen
L010N - Euripides -- Euripides II: Electra. Orestes. Iphigeneia in Taurica. Andromache. Cyclops
L011N - Euripides -- Euripides III: Bacchanals. Madness of Hercules. Children of Hercules. Phoenician Maidens. Suppliants
L012 - Euripides -- Euripides IV: Ion. Hippolytus. Medea. Alcestis
etc.

I want to rename each file with its corresponding name.
Can anyone point me to some command(s) or ideas for a bash script that might get me where I want to go?

Last edited by carlosestensser; 06-06-2012 at 10:09 AM.
 
Old 06-06-2012, 08:29 AM   #2
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
One way would be like this:

Code:
sed -r 's/^([[:alnum:]]+).*/mv \1.pdf "\0.pdf"/' booklist.txt | xargs -d'\n' -I{} bash -c {}
where booklist.txt is the list you provide above.

P.S. Back the files up before you do this. It should work but just to be sure

Last edited by ruario; 06-06-2012 at 08:35 AM. Reason: added warning
 
Old 06-06-2012, 09:08 AM   #3
carlosestensser
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ruario View Post
One way would be like this:

Code:
sed -r 's/^([[:alnum:]]+).*/mv \1.pdf "\0.pdf"/' booklist.txt | xargs -d'\n' -I{} bash -c {}
where booklist.txt is the list you provide above.

P.S. Back the files up before you do this. It should work but just to be sure
Before I even tried to start to begin to understand each piece of the string, I tried it and got the following:

Code:
[darwin...loeb_test] sed -r 's/^([[:alnum:]]+).*/mv \1.pdf "\0.pdf"/' /home/darwin/New/list.txt | xargs -d'\n' -I{} bash -c {}
bash: $'\357\273\277L001': orden no encontrada
 
Old 06-06-2012, 09:48 AM   #4
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
Did any of the files rename?

Also try the following:

Code:
sed -r 's/^([[:alnum:]]+).*/mv \1.pdf "\0.pdf"/' /home/darwin/New/list.txt > renamelist.txt
Open the file renamelist.txt and scroll through it. Any of the commands look wierd? Perhaps post a snippet if the file here is so.

If it looks ok (a bunch of mv commands to do the rename) you could then try:

Code:
sh renamelist.txt
 
1 members found this post helpful.
Old 06-06-2012, 09:58 AM   #5
carlosestensser
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ruario View Post
Did any of the files rename?
NO.

Quote:
Originally Posted by ruario View Post
Also try the following:

Code:
sed -r 's/^([[:alnum:]]+).*/mv \1.pdf "\0.pdf"/' /home/darwin/New/list.txt > renamelist.txt
Open the file renamelist.txt and scroll through it. Any of the commands look wierd? Perhaps post a snippet if the file here is so.

If it looks ok (a bunch of mv commands to do the rename) you could then try:

Code:
sh renamelist.txt
An extract of renamelist.txt:
Code:
L001 - Apollonius Rhodius -- Apollonius Rhodius. Argonautica  
mv L002.pdf "L002 - Appian -- Roman History I: Books 1-8.1  .pdf"
mv L003.pdf "L003 - Appian -- Roman History II: Books 8.2-12  .pdf"
 L004 - Appian -- Roman History III: The Civil Wars, Books 1-3.26  
 L005 - Appian -- Roman History IV: The Civil Wars, Books 3.27-5  
 L007N - Cicero -- Letters to Atticus I
I removed the trailing whitespaces in the name's list abd now I get a correct list:
Code:
mv L002.pdf "L002 - Appian -- Roman History I: Books 1-8.1  .pdf"
mv L003.pdf "L003 - Appian -- Roman History II: Books 8.2-12  .pdf"
mv L004.pdf "L004 - Appian -- Roman History III: The Civil Wars, Books 1-3.26  .pdf"
mv L005.pdf "L005 - Appian -- Roman History IV: The Civil Wars, Books 3.27-5  .pdf"
mv L007N.pdf "L007N - Cicero -- Letters to Atticus I  .pdf"
mv L008N.pdf "L008N - Cicero -- Letters to Atticus II  .pdf"
mv L009.pdf "L009 - Euripides -- Euripides I: Iphigenia at Aulis. Rhesus. Hecuba. The Daughters of Troy. Helen  .pdf"
mv L010N.pdf "L010N - Euripides -- Euripides II: Electra. Orestes. Iphigeneia in Taurica. Andromache. Cyclops  .pdf"
mv L011N.pdf "L011N - Euripides -- Euripides III: Bacchanals. Madness of Hercules. Children of Hercules. Phoenician Maidens. Suppliants  .pdf"
mv L012.pdf "L012 - Euripides -- Euripides IV: Ion. Hippolytus. Medea. Alcestis  .pdf"
mv L013.pdf "L013 - Julian -- Julian I: Orations 1-5  .pdf"
mv L014.pdf "L014 - Lucian -- Lucian I: Phalaris. Hippias or The Bath. Dionysus. Heracles. Amber or The Swans. The Fly. Nigrinus. Demonax. The Hall. My Native Land. Octogenarians. A True Story. Slander. The Consonants at Law. The Carousal (Symposium) or The Lapiths  .pdf"
mv L015.pdf "L015 - Petronius -- Satyricon. Apocolocyntosis  .pdf"

Last edited by carlosestensser; 06-06-2012 at 10:04 AM.
 
Old 06-06-2012, 10:09 AM   #6
carlosestensser
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Success ! ! !

I get a few errors such as:

Code:
mv: accediendo a «L014 - Lucian -- Lucian I: Phalaris. Hippias or The Bath. Dionysus. Heracles. Amber or The Swans. The Fly. Nigrinus. Demonax. The Hall. My Native Land. Octogenarians. A True Story. Slander. The Consonants at Law. The Carousal (Symposium) or The Lapiths  .pdf»: Nombre de archivo demasiado largo
but that's understandable.
A million thanks.

Now I have to go do my homework and try to understand what I have just done.
 
Old 06-06-2012, 12:33 PM   #7
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
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Try something like this:

Code:
while read -r filename xx title ; do

	mv "$filename.pdf" "$title.pdf"

done <file
It reads the string into 3 variables, with the second one (xx) being discarded, as it only captures the hyphen between the two desired strings. Then mv is run using them.

This is just a skeleton though. You should really add some code to clean up the title names a bit before mv'ing to them, such as replacing spaces with underscores and removing shell-reserved characters.

It will also only run in the current working directory at this point. You'd have to add extra path strings if you wanted to run it on an arbitrary location.


How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001

How do I do string manipulations in bash?
http://mywiki.wooledge.org/BashFAQ/100

Last edited by David the H.; 06-06-2012 at 12:34 PM. Reason: added links
 
  


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
[SOLVED] rename and arrange files based on a list babak1112 Linux - General 2 01-29-2012 08:47 AM
Rename list of files from command promp!!! Drigo Linux - Newbie 6 02-18-2010 03:26 PM
Rename files matching a list Qu3ry Programming 34 07-22-2009 06:59 PM
Rename 100+ files from a text file AGazzaz Linux - Software 9 09-26-2008 12:41 AM
awk cli to rename a list of files... pld Linux - General 4 02-15-2005 10:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fuduntu

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