LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Fuduntu (http://www.linuxquestions.org/questions/fuduntu-98/)
-   -   rename files using text list (http://www.linuxquestions.org/questions/fuduntu-98/rename-files-using-text-list-948811/)

carlosestensser 06-06-2012 09:12 AM

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?

ruario 06-06-2012 09:29 AM

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 ;)

carlosestensser 06-06-2012 10:08 AM

Quote:

Originally Posted by ruario (Post 4696830)
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


ruario 06-06-2012 10:48 AM

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

carlosestensser 06-06-2012 10:58 AM

Quote:

Originally Posted by ruario (Post 4696880)
Did any of the files rename?

NO.:cry:

Quote:

Originally Posted by ruario (Post 4696880)
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"


carlosestensser 06-06-2012 11:09 AM

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. :D

David the H. 06-06-2012 01:33 PM

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


All times are GMT -5. The time now is 07:22 PM.