99

I'm just trying to review basic terminal commands. Having said that, how do I create a text file using the terminal only?

  • Although the question is really too broad, a quick tutorial (vim, editors, shell command redirection and so on) could be useful for the future, particularly together with the view count and the voting scores of the post and the answers. Maybe it would be most useful as a community wiki post. – peterh May 20 '18 at 01:40

5 Answers5

132

You can't use a terminal to create a file. You can use an application running in a terminal. Just invoke any non-GUI editor (emacs -nw, joe, nano, vi, vim, …).

If you meant using the command line, then you are asking how to create a file using the shell. See What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'?

The basic way to create a file with the shell is with output redirection. For example, the following command creates a file called foo.txt containing the line Hello, world.

echo 'Hello, world.' >foo.txt

If you want to write multiple lines, here are a few possibilities. You can use printf.

printf '%s\n' 'First line.' 'Second line.' 'Third line.' >foo.txt

You can use a string literal containing newlines.

echo 'First line.
Second line.
Third line.' >foo.txt

or

echo $'First line.\nSecond line.\nThird line.' >foo.txt

Another possibility is to group commands.

{
  echo 'First line.'
  echo 'Second line.'
  echo 'Third line.'
} >foo.txt

On the command line, you can do this more directly with cat. Redirect its output to the file and type the input line by line on cat's standard input. Press Ctrl+D at the beginning of the line to indicate the end of the input.

$ cat >foo.txt
First line.
Second line.
Third line.
Ctrl+D

In a script you would use a here document to achieve the same effect:

cat <<EOF >foo.txt
First line.
Second line.
Third line.
EOF

If you just want to create an empty file, you can use the touch command: it creates the file if it doesn't exist, and just updates its last-modified date if it exists.

touch foo.txt

Equivalently:

>>foo.txt

i.e. open foo.txt for appending, but write 0 bytes to it — this creates the file but doesn't modify it. Unlike touch, this doesn't update the file's last-modified date if it already existed.

To create an empty file, and remove the file's content if the file already existed, you can use

>foo.txt
14
touch ~/Desktop/something.txt

This will create an empty txt file.

Or

echo "Hello" > ~/Desktop/somethingelse.txt

This will create a txt file saying "Hello".

nano ~/Desktop/anotherfile.txt

This will open ~/Desktop/anotherfile.txt in nano, or if it doesn't exist, it will create it and open it in nano.

The same can be done by simply replacing nano with emacs or vim and it will use emacs or vim instead of nano

DisplayName
  • 11,688
8

All it takes to create an empty file is:

> my.txt
SAM
  • 259
5

Something helpful when you're stuck and must create a multiline textfile on the command line is something like this:

# cat <<'EOF' > textfile
> This "should"
> prove useful, 'maybe'.
> EOF
# cat textfile 
This "should"
prove useful, 'maybe'.

You can substitute cat for another command, for instance grep and a pattern, and have grep do its job on your input and this would output to file in the same fashion.

2

You can use either nano, vi or emacs editing program to create file on terminal level. If you're using x windows system you'll need to tell your system not to use the program in the GUI; but anyways, I'll use emacs as an example.

From the bash; type:

 emacs filename.txt (press enter) - it will create an empty file with the filename specified "filename.txt". It will save "filename.txt" from which ever 
directory type in "emacs filename.txt" command.

keep in mind in all UNIX system VI comes with it; emacs doesn't, you'll need to install emacs. Nano on the other hand is more popular; just in case if that doesn't respond it means when you "nano filename.ext" and nothing happens you'll need to install it.

once you issue vi filename.txt most likely you'll end up creating a new file. You should look up vi, emacs or nano how to use on google before you get started. How to write a file, editing it, using its tools to search and replace text is different in those editing programs. I like emacs out of all the choices but you'll find developers that are obsessed with or favor vi and nano

unixmiah
  • 358
  • for no graphical editor option in emacs is "emacs filename.txt -nw" where "-nw" means no graphical user window, no window – unixmiah Oct 06 '14 at 19:41