LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Batch rename html files line by line from text file (https://www.linuxquestions.org/questions/linux-newbie-8/batch-rename-html-files-line-by-line-from-text-file-4175447659/)

saturnianalien 01-29-2013 04:57 AM

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

pan64 01-29-2013 06:44 AM

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

theNbomr 01-29-2013 11:38 AM

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.

saturnianalien 01-30-2013 11:01 AM

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.


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