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 Answers
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

- 829,060
-
Could you provide a reference to an explanation of the meaning of
>foo.txt
syntax, please? – Alexey Oct 08 '22 at 13:42 -
1@Alexey Bash (or the documentation of other shells, but bash tend to be the most readable), POSIX – Gilles 'SO- stop being evil' Oct 08 '22 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 09 '22 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<<
. – Gilles 'SO- stop being evil' Oct 09 '22 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 09 '22 at 17:15 -
@Alexey To use POSIX terminology, a simple command can't be empty, but it's enough for it to have any one of an
io_redirect
, anASSIGNMENT_WORD
, or acmd_name
. – Gilles 'SO- stop being evil' Oct 09 '22 at 17:52
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

- 11,688
-
-
Let's not reinvent the wheel.
<<
and>>
are discussed in What are the shell's control and redirection operators? – G-Man Says 'Reinstate Monica' Oct 13 '14 at 18:53
All it takes to create an empty file is:
> my.txt

- 259
-
3In some shells (zsh, tcsh, ...) at least
:> my.txt
is needed.:
is an empty command. – jofel Oct 06 '14 at 22:11 -
{ echo 'First line.' echo 'Second line.' echo 'Third line.' } >foo.txt won't work – Waterfr Villa Aug 12 '15 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'.
> 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.
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

- 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