For web-development practice I'm using a LAMP stack, on Ubuntu 17.10. For this I'm creating directories within /var/www
, each directory is named after a project.
To create a subdirectory I use the terminal and:
david@Ed:/var/www/html$ mkdir projectName
david@Ed:/var/www/html$ cd projectName
david@Ed:/var/www/html/projectName$ mkdir css img js
david@Ed:/var/www/html/projectName$ touch index.html
david@Ed:/var/www/html/projectName$ ls
css img index.html js
This works. And, in all honesty, is far from arduous. But, is there a means by which I can simplify this?
I'm aware that:
mkdir -p projectName/css
Will create both the css
, and the parent projectName
, directory (if that parent doesn't already exist), but it still leaves the requirement of creating the other two directories and the index.html
file (again, this is not particularly arduous but feels like it should be unnecessary).
Ideally I'd like that the command mkdir <projectName>
when run in the /var/www
directory would create the <projectName>
directory with the css
, img
, js
directories and the index.html
document (that document ideally containing the skeleton of a valid html document*). However I accept that abusing the mkdir
command would likely lead to unforeseen/predictable edge-cases and consequences, so probably the best compromise is a script of some kind.
With the above in mind, I created the following (naive/simple) script:
#!/bin/bash
mkdir $@
cd $@
mkdir css img js
touch index.html
I call the above script as follows:
./createDir.sh projectName
This works but is less than ideal; the problems:
- the created
index.html
has a type ofplain text document (text/plain)
, rather than the expectedHTML document (text/html)
, and of course - has none of the structure of an HTML document (as outlined in the single footnote), because I didn't add any content (and haven't yet found a way of creating said content in a script).
Also, somewhat predictably, both the css
and js
directories would also – ideally – be created with documents (project.css
and project.js
) intact.
So, is there a means by which I can create – with the use of a script, or otherwise – a directory with its subdirectories and documents?
*. As a rudimentary example:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="js/project.js"></script>
<link href="css/project.css" rel="stylesheet" />
</head>
<body></body>
</html>
You'll need some template for your
index.html
to copy from. But you can create such a template on the fly and then copy it to the desired destination. Theinstall
command may be of help:You probably need to run this script via
sudo
because/var/...
isn't world-writable.I would suggest you to do something like adduser command is doing with skeleton (https://unix.stackexchange.com/questions/285708/skeleton-directory-how-to-add-my-own-directories).
Make a template/skeleton somewhere, where all the initial files and directories for the project are organized as they would be in the project. After you make a directory for a new project you just copy the skeleton to the destination with a simple copy:
The parameters in cp command are as follows:
I would point out here an archive parameter, which allows you to set the correct files and directories permissions and ownerships in the template. They will be preserved when copied and in that way you have files in the project with correct permissions for web server to access them. If you want to have different ownerships of files for each project, you will have to do that manually, again with simple commands (
chmod -R <permissions> <destination directory>
andchown -R <ownership> <destination directory
).While this is an imperfect answer, and is mostly produced via some further searches after I posted the question, I've updated my script and solved most of the the problems.
My current script (equally naive and simple though it certainly is) is:
This feels a little dirty, though it does work. The main issue with this, current, approach is that final cat call:
This to produce a document that is correctly identified by its
type
property asapplication/javascript
, whereas using:Simply creates yet another document – with the correct filetype extension – whose type attribute is
plain text document (text/plain)
.