1

This is not specifically a Linux question. But I was wondering if I can get this done in BASH. I have multiple HTML files in a folder and I want to generate a HTML table of content. So for file1.html and file2.html, I want to generate a file like this :

<html>
   <body>
    <h1>Table of Contents</h1>
    <p style="text-indent:0pt">
       <a href="file1.html">file1</a><br/>
        <a href="file2.html">file2</a><br/>
     </p>
   </body>
 </html>

The process is simple but can get tedious and confusing when you have more than ten files with long names. I use the table of content file to convert the HTML files to an ebook format using Calibre. If it can be done automatically it'll save a lot of trouble.

Thank you for your time. Any help would be appreciated.

  • what is the condition for filling such table of contents? how it should look if there are 10 html files in a folder? – RomanPerekhrest Nov 8 '17 at 16:53
  • This seems like a very simple scriptable task (if you don’t have to worry about files with weird non-alphanumeric characters in their names). What have you tried? – G-Man Nov 8 '17 at 18:24
  • With more file added it should just add lines like this <a href="file1.html">file1</a><br/> <a href="file2.html">file2</a><br/> <a href="file3.html">file3</a><br/> . . I don't know much about scripting, since I am a Linux newbie. I was just wondering if there's something simple that could be done. I will learn scripting later on, but I don't want to learn it just to do this. – Joenikushimi Nov 8 '17 at 18:36
0

Create a file with the following contents:

#!/bin/sh
echo "<html>"
echo "   <body>"
echo "    <h1>Table of Contents</h1>"
echo "    <p style=\"text-indent:0pt\">"
for fil
do
    echo "       <a href=\"$fil\">${fil%.html}</a><br/>"
done
echo "    </p>"
echo "  </body>"
echo "</html>"

Give it a name such as toc.  Type chmod +x toc, and then run ./toc *.html in the directory where the files are.  It will write the HTML Table of Contents to the standard output, which you can, of course, redirect to a file.

  • Thank you! This worked like a charm. It'll save me so much trouble. Much appreciated – Joenikushimi Nov 10 '17 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.