---
title: "Generate index.html for a given directory"
description: "#!/bin/bash #Author : Hemanth.HM #Email : hemanth.hm@gmail.com #Purpose : To genrate index.html for a give directory. #Usage."
url: "https://h3manth.com/scribe/generate-indexhtml-given-directory/"
canonical_url: "https://h3manth.com/scribe/generate-indexhtml-given-directory/"
markdown_url: "https://h3manth.com/scribe/blog/generate-indexhtml-given-directory.md"
publishDate: "2010-02-12T00:00:00.000Z"
author: "Hemanth HM"
category: "linux"
tags: ["bash"]
word_count: 127
reading_time_minutes: 1
x_markdown_tokens: 313
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Generate index.html for a given directory

> #!/bin/bash #Author : Hemanth.HM #Email : hemanth.hm@gmail.com #Purpose : To genrate index.html for a give directory. #Usage.

#!/bin/bash
#Author  : Hemanth.HM
#Email   : hemanth.hm@gmail.com
#Purpose : To genrate index.html for a give directory.
#Usage  : bash index.sh 
 
dir=$1
cwd=$PWD
 
#Check for input 
[ -z ${dir} ] && echo "Usage : `basename $0` " && exit 1 || cd ${dir}
 
## In case the user gives the input as "."
[ "${dir}" == "." ] && dir=`pwd`
 
 
## Collect each file and add them to href's
INDEX=$(for f in *; 
do printf '
- [%s]()\n' "$f" "$f"; 
done)

## Create an index.html file
cat > index.html << EOI

     
        
            body { font-family: serif; }
            p { font-size: 13px; }
            h2 { font-size: 24px; 
                 font-style: italic;
               }
            ul { font-weight: bolder; }
 
            <title>Index of ${dir}</title>
        
    
    
        Index of ${dir}
        
            $INDEX
        
    

EOI
echo "Genrated index.html in ${dir}!"
cd $cwd
