Is it possible to create an html file with some text already in it, (I'll just use my name as an example) in a directory separate to the one you're in, all in one command? I'm very new to Ubuntu and any and all help would be appreciated.
3 Answers
You can use redirection, which creates a file if the given target does not exist (or overwrites the file if it does, so do be careful). Instead of the output of a command being written to the terminal, it gets written to the target file.
echo "something" > target
So, replace target with the path to the file you want to create, and "something" with what you want to put into the file...
echo "zanna s" > /home/zanna/playground/new-file
The directory has to exist already, and you must have permission to write there. If you do not, sudo will not help you, because redirection is done by the shell itself, and the shell is not being run as root. There are ways around that, though. One of them is tee, which is another option for you (although perhaps this makes it two commands). The tee command writes to both standard output (the terminal) and the specified file, so it is a little more informative than using redirection:
echo "various things" | tee /path/to/file
This | thing is a "pipe". It turns the output of the command to the left of it into the input of the command to the right of it. So the output of echo (various things) becomes the input of tee.
One can make echo give multiline output using the -e option, although it is pretty awkward:
zanna@peach:~$ echo -e "things\non\nseveral\nlines\n" | tee playground/new-file
things
on
several
lines
In the above command I used a relative path to the file (a path that depends on the current location). playground is a subdirectory of the current directory, so this works fine. If you use the absolute path (for example /home/zanna/playground/file or ~/playground/file or $HOME/playground/file) the same command will work no matter what the current working directory is.
I don't know what you mean by a html file. Perhaps it should just have an extension:
echo "$USER" | tee ~/subdirectory/file.html
- 72,312
To bypass echo being awkward at multiline literals one can use another bash feature:
cat > /path/to/my_file.html << EOF
<!DOCTYPE html>
<html>
<head>
<title>Simple example</title>
</head>
<body>
<p>You just wrote `echo HTML` file with $SHELL as user ${USER}!</p>
</body>
</html>
EOF
Note that EOF can be any string as long as it does not appear on separate line in your input. Also note that some of bash constructs like backticks and variable substitution are still in effect.
If you are doing this interactively as opposed to using in script, editor like nano might be better suited for this job.
You want to copy that file in another directory?
cp <filename> <path/to/new/directory>
or if you want to create a new file into another directory...
nano file.html /path/to/new/directory
you write whatever you want to write...
then to edit or write something new in that file
nano /path/to/new/directory/file.html
- 221