How can I create an empty file from the command line?


Use the touch command:

The touch utility sets the modification and access times of files to the
current time of day. If the file doesn't exist, it is created with
default permissions.


touch newfile
  • 14
    You can use touch newfile.txt or some other extension, too (if you need to specify the extension). – gotqn Apr 26 '14 at 8:18
  • @gotqn for python touch newfile.py and for ruby newfile.rb ? – kouty Aug 30 '16 at 12:56
  • 2
    But if newfile already exists and isn't empty, then touch newfile will leave you with a nonempty file. Maybe not what you wanted. – Camille Goudeseune Sep 26 '17 at 14:47
  • @CamilleGoudeseune I think If newfile already exists, touch command will just update the timestamp of file (which is what command exactly for) without editing the contents of file. – C0deDaedalus Mar 13 '18 at 11:20
  • You don't have to think that; it's true! But it's not what the question asked. – Camille Goudeseune Mar 13 '18 at 16:21
> newfile

Will also create an empty file. If the file does already exist, it will be truncated (emptied). To keep the file contents, use >> for appending as in:

>> file

Even if the file exists, the contents will be untouched.

Edit: If you don't have any content to type, this one is faster:

user@host$ :> newfile
user@host$ :>> new_or_existing_file

Note. : is the command here. It is not part of the prompt.

  • Now that's new. Is it bash-specific? – Tshepang Jan 15 '11 at 6:40
  • I don't think so. Any shell which allows redirection of output stream to a file should support this. This will truncate the file if it already exists. touch is safe to use if you don't want to empty it. – balki Jan 15 '11 at 10:23
  • Wow now I think this is called cheating the system – banarun Jun 23 '14 at 12:00
cat /dev/null > file1.ext 

the exact way there is also another way

echo "" > file2.ext 

The difference is file1.ext will be zero bytes and file2.ext would be one byte. You can check this by

ls -l file*.*
  • 10
    No, 'echo "" >' does not create an empty file, it creates a file containing a newline. If you for some reason want to use echo to create an empty file you will have to use 'echo -n "" >', or simply 'echo -n >' – andol Jan 15 '11 at 8:00

Using vim editor you can also create an empty file.

vim filename

Then save

  • Well, in this case not any text editor but vim. – Nephente Oct 4 '15 at 12:15

The command

echo -n > file

creates an empty file, if your version of echo supports the -n switch.

Or you could use printf

printf '' > file

Python one-liner:

$ python -c 'import sys,os;f=sys.argv[1];os.utime(f,None) if os.path.exists(f) else open(f,"a").close' myfile.txt

Basically,python implementation of touch.

We can make it shorter with this:

$ python -c 'import sys,os;f=sys.argv[1];'$'\n''with open(f,"a"): os.utime(f,None)' mysecondfile.txt 

protected by Sergiy Kolodyazhnyy Jul 10 '17 at 12:00

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.