LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-29-2013, 05:57 AM   #1
saturnianalien
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Rep: Reputation: Disabled
Batch rename html files line by line from text file


Say I have a bunch of html files that are named sequentially as well as a separate text file containing a bunch of strings separated by new lines. The number of lines in the text file match up exactly with the number of files. How would I batch rename all those html files according to each line? That is, 000.html would be 1stline.html and 001 would 2ndline.html etc (1stline and 2ndline actually being whatever is on those lines in the text file).
 
Old 01-29-2013, 07:44 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,475

Rep: Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424
you would need to read the two arrays (let we say a and b) and create a loop to mv a[i].html to b[i].html.
You can use perl, awk or whatever language you prefer
 
1 members found this post helpful.
Old 01-29-2013, 12:38 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This should be close.

Code:
#
#  Get array of HTML files (assumes natural sort order)
#
htmlFiles=([0-9][0-9][0-9]*.html)

# Loop counter to index $htmlFiles array
i=0

#  Loop over lines from text file, tracking sorted array
#  with incrementing index.
#
while read destFile; do
    sourceFile=${htmlFiles[$i]}
    echo mv "$sourceFile" "$destFile"
    i=$(( $i + 1 ))
done < textFile.dat
--- rod.

Last edited by theNbomr; 01-29-2013 at 12:45 PM.
 
1 members found this post helpful.
Old 01-30-2013, 12:01 PM   #4
saturnianalien
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
That worked perfectly theNbomr, thanks! Just had to change echo mv "$sourceFile" "$destFile" to mv "$sourceFile" "$destFile".html, moved the wildcard in the htmlFiles variable to the beginning since my files had prefixes, and of course textFile.dat was changed to the name of my text file.
 
  


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
[SOLVED] open two text files , read them line by line and update parameters of the 3rd file rastin_nz Programming 17 10-20-2010 08:10 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 09:22 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 10:28 AM


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