What's the easiest way to create a file in Linux terminal?
17 Answers
Depending on what you want the file to contain:
touch /path/to/file
for an empty filesomecommand > /path/to/file
for a file containing the output of some command.eg: grep --help > randomtext.txt echo "This is some text" > randomtext.txt
nano /path/to/file
orvi /path/to/file
(orany other editor emacs,gedit etc
)
It either opens the existing one for editing or creates & opens the empty file to enter, if it doesn't exist

- 929
- 1
- 9
- 15

- 64,175
- 10
- 70
- 92
-
61also, `printf "some long message\nwith newlines\n" > file` . Good luck to all. – shellter Feb 21 '12 at 16:50
-
1UNIX is not a command line environment, but a family of (very different) OSes. That said: Yes, this should work on most Unices – Eugen Rieck Feb 21 '12 at 16:58
-
5`touch` will work in UNIX, because it's a standard tool. The `somecommand` example will work because it uses standard syntax. The `nano` sample may not work because an editor named `nano` may not be installed (nano is not standardized). The standard editor is `ed` and could be used in place of `nano`, or you could use `$EDITOR` to use your user- or system-configured default text editor, if there is one. – sorpigal Feb 21 '12 at 17:16
-
14Additionally, you could simply say `>/path/to/file` to create an empty file, even if you don't have `touch`. – sorpigal Feb 21 '12 at 17:17
-
2
-
1"permission denied" means just that - you do not have the permission to create the specified file. This is typically a good thing. – Eugen Rieck Sep 30 '13 at 18:39
-
Also keep in mind, the /dir/ needs to exist for touch/vi(m)/nano to work...at least in certain distros such as CentOS. – dhaupin Aug 26 '15 at 17:22
-
It is also worth noting that the tilde (~) shorthand for the home directory will not be interpreted by a bash script. This tripped me up for a few minutes. – Shadoninja Aug 16 '16 at 17:55
-
1If you see a 'permission denied' error try: sudo bash -c 'echo "Text" > file' – ammills01 Nov 04 '16 at 18:09
-
expanding on @ammills01's comment: If you see a 'permission denied' error try any of the commands listed in the question and the above comments with a leading `sudo `, e.g. `sudo touch /path/to/file`. You will then have to type the password to then be given permission (root access) to create this new file – hello_there_andy Jan 12 '17 at 00:28
-
@EugenRieck: nano seems doesn't work. I tried on Fedora 27 terminal. Got error bash: nano: command not found... – vinsinraw Feb 20 '18 at 14:36
-
@vinsinraw That just means, that `nano` is not installed on this system. Most likely `vi` is, so `vi /path/to/file` should work (or just do `yum install nano`) – Eugen Rieck Feb 20 '18 at 15:29
-
Which command does not *`touch`* any file? `mkdir` refuses to create a new directory, so I imagined a command such as `mkfile`. – neverMind9 Apr 28 '18 at 16:35
-
@neverMind9 `touch` can be used as `mkfile` - so there must be something else going on. What's the error message to `mkdir` ? – Eugen Rieck Apr 29 '18 at 12:28
Create the file using cat
$ cat > myfile.txt
Now, just type whatever you want in the file:
Hello World!
CTRL-D to save and exit

- 31,267
- 26
- 103
- 174
-
1I prefer using cat since it offers an easy and convenient way to edit the file immediately. – Delali Sep 26 '17 at 19:52
-
-
1When I tried `cat /etc/systemd/system/sample.service`, it said "no such file or directory" rather than creating a new sample.service file. – TylerH Aug 18 '19 at 20:57
-
@TylerH `cat /etc/systemd/system/sample.service` prints the file to the console `cat > /etc/systemd/system/sample.service` redirects standard input to the file (which is why you need to close standard input by pressing control-d. – Jerry Jeremiah Jan 29 '20 at 22:52
There are several possible solutions:
Create an empty file
touch file
>file
echo -n > file
printf '' > file
The echo
version will work only if your version of echo
supports the -n
switch to suppress newlines. This is a non-standard addition. The other examples will all work in a POSIX shell.
Create a file containing a newline and nothing else
echo '' > file
printf '\n' > file
This is a valid "text file" because it ends in a newline.
Write text into a file
"$EDITOR" file
echo 'text' > file
cat > file <<END \
text
END
printf 'text\n' > file
These are equivalent. The $EDITOR
command assumes that you have an interactive text editor defined in the EDITOR environment variable and that you interactively enter equivalent text. The cat
version presumes a literal newline after the \
and after each other line. Other than that these will all work in a POSIX shell.
Of course there are many other methods of writing and creating files, too.
You can use touch
command, as the others said:
touch filename
To write on file on command line, you can use echo
or printf
:
echo "Foo" > filename
printf "Foo" > filename
Maybe you can have problems with permissions. If you are getting the following error: bash: filename: Permission denied
, you need to use sudo bash -c 'echo "Foo" > filename'
, as described here:
https://askubuntu.com/questions/103643/cannot-echo-hello-x-txt-even-with-sudo

- 1
- 1

- 517
- 7
- 14
-
Or `sudo !!` If you get that error. Using `!!` Will let you rerun the command that failed prepending ‘ `sudo` to the command. Just a tip I find to be very useful. Happy coding =) – 0xe1λ7r Mar 29 '22 at 23:27
How to create a text file on Linux:
- Using
touch
to create a text file:$ touch NewFile.txt
- Using
cat
to create a new file:$ cat NewFile.txt
The file is created, but it's empty and still waiting for the input from the user. You can type any text into the terminal, and once done CTRL-D will close it, or CTRL-C will escape you out. - Simply using
>
to create a text file:$ > NewFile.txt
- Lastly, we can use any text editor name and then create the file, such as:
nano MyNewFile vi MyNewFile NameOfTheEditor NewFileName

- 4,367
- 2
- 30
- 57

- 191
- 1
- 2
haha! it's easy! try this:
$ touch filename

- 11,405
- 11
- 58
- 89
-
1This worked with sudo, so I was able to create new file in system folder. – Patricia Apr 23 '15 at 20:43
This will create an empty file with the current timestamp
touch filename

- 30,615
- 24
- 120
- 162

- 309
- 1
- 5
- 14
1st method
echo -n > filename.txt
2nd method
> filename.txt
3rd method
touch filename.txt
To view the file contents
vi filename.txt

- 23,857
- 8
- 121
- 100

- 161
- 1
- 2
-
-
The question is not on-topic for Stack Overflow as defined in the [help]. Please don't answer such questions; instead, you should flag them for attention and they will be closed or migrated appropriately. – Toby Speight Aug 29 '16 at 08:45
touch filename
for permission denied
error use sudo
command as:
sudo touch filename

- 4,065
- 2
- 33
- 40
In case you guys are trying to create a new file, but it says: 'File does not exist'
, it's simply because you are also accessing a directory, which does not exist yet. You have to create all non existent directories first, using the mkdir /path/to/dir
command.

- 1,593
- 4
- 26
- 41
To create a blank file with ownership and permissions using install.
sudo install -v -oUSER -gGROUP -m640 /dev/null newFile.txt

- 341
- 2
- 11
-
The `sudo` is useful in many scenarios, but not directly part of the answer to the OP's question. – tripleee Apr 04 '19 at 16:03
-
It is needed if you are not the user name that you need the file owned by or not a member of the group. – LinuxGuru Apr 04 '19 at 16:10
-
Yes, there are scenarios like that which are however strictly speaking not what the OP asked. – tripleee Apr 04 '19 at 16:11
-
One of the easiest way and quick
$ vim filename