Generate index.html for a given directory

13 Feb 2010

by hemanth

  1. #!/bin/bash
  2. #Author : Hemanth.HM
  3. #Email : hemanth.hm@gmail.com
  4. #Purpose : To genrate index.html for a give directory.
  5. #Usage : bash index.sh <dir>
  6.  
  7. dir=$1
  8. cwd=$PWD
  9.  
  10. #Check for input
  11. [ -z ${dir} ] && echo "Usage : `basename $0` <dir>" && exit 1 || cd ${dir}
  12.  
  13. # In case the user gives the input as "."
  14. [ "${dir}" == "." ] && dir=`pwd`
  15.  
  16.  
  17. # Collect each file and add them to href's
  18. INDEX=$(for f in *;
  19. do printf '<li><a href="%s" target="_blank">%s</a></li>\n' "$f" "$f";
  20. done)
  21.  
  22. # Create an index.html file
  23. cat > index.html << EOI
  24. <html>
  25.   <head>
  26.   <style type="text/css">
  27.   body { font-family: serif; }
  28.   p { font-size: 13px; }
  29.   h2 { font-size: 24px;
  30.   font-style: italic;
  31.   }
  32.   ul { font-weight: bolder; }
  33.  
  34.   <title>Index of ${dir}</title>
  35.   </style>
  36.   </head>
  37.   <body>
  38.   <h2>Index of ${dir}</h2>
  39.   <hr>
  40.   <ui>$INDEX</ui>
  41.   <hr>
  42.   </body>
  43. </html>
  44. EOI
  45. echo "Genrated index.html in ${dir}!"
  46. cd $cwd
Share this