3

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.

Zanna
  • 70,465
Caleb Wagner
  • 47
  • 1
  • 3

3 Answers3

12

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
Zanna
  • 70,465
  • 1
    It might be useful to mention that redirection can (and I think does, by default?) overwrite the target file if it already exists. That could definitely give some people unhappy surprises. – David Z Sep 11 '20 at 18:17
  • @DavidZ yes it does do that - it truncates the file to zero bytes before writing anything (so one may empty a file (or create an empty file) by doing just > file). I will mention it just in case. – Zanna Sep 11 '20 at 18:26
  • This answer could be improved by mentioning printf as a safer, more portable and more powerful alternative to echo. Also, set -C (with a capital C) or set -o noclobber will prevent > from overwriting an existing file.  (These are specified in POSIX and they work in bash.) Also, since the user is “very new”, an example of a relative pathname beginning with ../ might be especially helpful. – G-Man Says 'Reinstate Monica' Sep 11 '20 at 20:01
  • @G-ManSays'ReinstateMonica' feel free to write an answer. I don't want this one to be longer and more complicated. – Zanna Sep 12 '20 at 02:00
3

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.

0

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