Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I just wrote a function in my ~/.bashrc that will let me create a folder for a new website with one command. The function looks like this:

function newsite() {
  mkcd "$*"  # mkdir and cd into it
  mkdir "js"
  mkdir "imgs"
  touch "index.html"
  touch "main.css"
  vim "index.html"
}

Woo hoo super complicated function... Okay in all seriousness I understand this is really basic, it's the first function I've made without copy and pasting from online though so bear with me.

Now what I would like to do is, instead of just touching index.html and main.css I'd like to create basic template files for index.html and main.css problem is I have absolutely no idea how to do that. I don't know much about writing to files using bash commands. Typically I'd just open the files in vim and go to town but I'd like to have something already started when I get into vim...

share|improve this question
1  
It'd probably be easier to save your templates in some directory site_template/, and just do a recursive copy from the template folder to the new site folder, followed by whatever cd and vim commands you want. –  jw013 Feb 10 '12 at 4:15
    
@jw013 AH Yes super simple! Thank you very much :D Make that an answer and I'll accept it. –  CaldwellYSR Feb 10 '12 at 4:22
    
Looks like @HaiVu posted an answer already, so need for me to make another :) –  jw013 Feb 10 '12 at 4:38
    
You're better off using a build tool like scons to do this. –  Faheem Mitha Feb 10 '12 at 5:25

2 Answers 2

up vote 2 down vote accepted

jw013's idea and HaiVu's answer are both correct. However for the sake of completeness for anyone who comes upon this question wanting the answer, here it is;

function newsite() {
  mkcd "$*"  # mkdir and cd into it
  mkdir "js"
  mkdir "imgs"
  cat > index.html <<'EOI'
<html>
<head>
</head>
<body>
</body>
</html>
EOI
  cat > main.css <<'EOI'
body {
 font-family: Arial;
}
EOI
  vim "index.html"
}

The <<'EOI' thing is called a heredoc, most scripting languages have them.

share|improve this answer

I like jw013's idea:

mkdir -p ~/site_template/{js,imgs}
# Creates all the files in this directory: index.html, main.css, ...

Now, when it's time to create a new site:

cp -r ~/site_template ~/my_site

That would be much easier. Plus, you can edit your site template files any way you like.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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