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 :

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

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? Nov 8, 2017 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? Nov 8, 2017 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. Nov 8, 2017 at 18:36

1 Answer 1


Create a file with the following contents:

echo "<html>"
echo "   <body>"
echo "    <h1>Table of Contents</h1>"
echo "    <p style=\"text-indent:0pt\">"
for fil
    echo "       <a href=\"$fil\">${fil%.html}</a><br/>"
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 Nov 10, 2017 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.