532

What's the easiest way to create a file in Linux terminal?

juzraai
  • 5,693
  • 8
  • 33
  • 47
raffian
  • 31,267
  • 26
  • 103
  • 174

17 Answers17

670

Depending on what you want the file to contain:

  • touch /path/to/file for an empty file
  • somecommand > /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 or vi /path/to/file (or any 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

Loves Probability
  • 929
  • 1
  • 9
  • 15
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • 61
    also, `printf "some long message\nwith newlines\n" > file` . Good luck to all. – shellter Feb 21 '12 at 16:50
  • 1
    UNIX 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
  • 14
    Additionally, 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
    @EugenRieck What if it says permission denied – 123 Sep 30 '13 at 10:57
  • 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
  • 1
    If 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
155

Use touch

touch filename
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
129

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

raffian
  • 31,267
  • 26
  • 103
  • 174
  • 1
    I prefer using cat since it offers an easy and convenient way to edit the file immediately. – Delali Sep 26 '17 at 19:52
  • You can also type nothing. – Bing Zhao Feb 15 '19 at 08:04
  • 1
    When 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
53

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.

Community
  • 1
  • 1
sorpigal
  • 25,504
  • 8
  • 57
  • 75
27

Also, create an empty file:

touch myfile.txt
andreapier
  • 2,958
  • 2
  • 38
  • 50
23

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

Community
  • 1
  • 1
user3753202
  • 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
19

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
Chris Charabaruk
  • 4,367
  • 2
  • 30
  • 57
DATA SCIENCE
  • 191
  • 1
  • 2
17

haha! it's easy! try this:

$ touch filename
Roman Newaza
  • 11,405
  • 11
  • 58
  • 89
16

This will create an empty file with the current timestamp

touch filename
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Christy George
  • 309
  • 1
  • 5
  • 14
16

1st method

echo -n > filename.txt

2nd method

> filename.txt

3rd method

touch filename.txt

To view the file contents

vi filename.txt
Tomik
  • 23,857
  • 8
  • 121
  • 100
shashwat gupta
  • 161
  • 1
  • 2
  • 2 method is type-> "> filename.txt" – shashwat gupta Aug 29 '16 at 06:50
  • 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
16
touch filename

for permission denied error use sudo command as:

sudo touch filename
Pradeep Kumar
  • 4,065
  • 2
  • 33
  • 40
11

Simple as that :

> filename

IvanM
  • 2,913
  • 2
  • 30
  • 30
8

You can use the touch command to create a new empty file.

http://linux.about.com/library/cmd/blcmdl_touch.htm

Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
6

I like the nano command-line editor (text):

nano filename
Sam Perry
  • 2,554
  • 3
  • 28
  • 29
4

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.

Sebastian
  • 1,593
  • 4
  • 26
  • 41
2

To create a blank file with ownership and permissions using install.

sudo install -v -oUSER -gGROUP -m640 /dev/null newFile.txt
LinuxGuru
  • 341
  • 2
  • 11
2

One of the easiest way and quick

$ vim filename