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, 2018 at 1:40

5 Answers 5


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


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.

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

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

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



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

  • Could you provide a reference to an explanation of the meaning of >foo.txt syntax, please?
    – Alexey
    Oct 8, 2022 at 13:42
  • 1
    @Alexey Bash (or the documentation of other shells, but bash tend to be the most readable), POSIX Oct 8, 2022 at 19:21
  • Thanks. I know about redirection, but have hard time finding documentation for the line >foo.txt, where there is nothing to redirect (no command before >).
    – Alexey
    Oct 9, 2022 at 14:34
  • @Alexey It's an empty command. An empty command does nothing, but its redirections are performed, which can have visible effects: an error if the file can't be opened, creating the file if it didn't exist for > and >>, truncating the file if it exists for <, processing the here-document for <<. Oct 9, 2022 at 16:46
  • I thought the empty command had to be evoked explicitly as :. I would like to understand the rules that allow $ >foo.txt, but disallow $ |cat.
    – Alexey
    Oct 9, 2022 at 17:15
touch ~/Desktop/something.txt

This will create an empty txt file.


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


All it takes to create an empty file is:

> my.txt
  • 3
    In some shells (zsh, tcsh, ...) at least :> my.txt is needed. : is an empty command.
    – jofel
    Oct 6, 2014 at 22:11
  • { echo 'First line.' echo 'Second line.' echo 'Third line.' } >foo.txt won't work Aug 12, 2015 at 22:24

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'.
# 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.


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

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

Not the answer you're looking for? Browse other questions tagged .