240

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

6 Answers 6

283

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.

Example:

touch newfile
5
  • 18
    You can use touch newfile.txt or some other extension, too (if you need to specify the extension).
    – gotqn
    Apr 26, 2014 at 8:18
  • @gotqn for python touch newfile.py and for ruby newfile.rb ?
    – kouty
    Aug 30, 2016 at 12:56
  • 3
    But if newfile already exists and isn't empty, then touch newfile will leave you with a nonempty file. Maybe not what you wanted. Sep 26, 2017 at 14:47
  • 1
    @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. Mar 13, 2018 at 11:20
  • You don't have to think that; it's true! But it's not what the question asked. Mar 13, 2018 at 16:21
93
> 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.


If you want to create as root

: | sudo tee thefile

To not truncate existing file:

: | sudo tee -a thefile
5
  • 1
    Now that's new. Is it bash-specific?
    – tshepang
    Jan 15, 2011 at 6:40
  • 1
    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, 2011 at 10:23
  • Wow now I think this is called cheating the system
    – banarun
    Jun 23, 2014 at 12:00
  • I understand that : is a null command, but could you explain why : > file is faster than > file Mar 8, 2021 at 3:59
  • @GypsyCosmonaut because you don't have to press Ctrl+D
    – balki
    Jan 11, 2023 at 22:28
16
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*.*
1
  • 11
    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, 2011 at 8:00
10

Using vim editor you can also create an empty file.

vim filename

Then save

:wq
2
  • Well, in this case not any text editor but vim.
    – Nephente
    Oct 4, 2015 at 12:15
  • This is not from the command line. Perhaps if you post a vim one-liner.
    – Asim Jalis
    Jul 12, 2020 at 20:45
3

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
1
  • printf gave me a permission error even as sudo
    – Dawoodjee
    Oct 15, 2021 at 18:33
3

In general, creating any regular1 file on Linux involves open(2) ,openat(2), and creat(2) system calls (and specifically with O_CREAT flags). That means if you call any command-line utility that does these system calls, you can create a new empty file.

Most commonly new filename is created with something like this:

  • touch /tmp/new_file
  • : > /tmp/new_file
  • true > /tmp/new_file
  • > /tmp/new_file ( bash shell only )

touch is a standalone utility. Its original purpose is to update the access and modification time of a file, however if the file does not exist - it will be created. Note also that a filename - is treated specially, so if you do want to create a file that is named literally -, you'll have to enclose that into single or double quotes.

By contrast, > is a shell redirection operator for stdout stream. The > operator specifically calls the openat() system call with O_WRONLY|O_CREAT|O_TRUNC flags. That means, if the filename does not exist - it will be created, and if it does - the data will be truncated (and therefore gone, so > should be used with care). In most shells nowadays true or : is a built-in , so doing : > /tmp/new_file is going to be more efficient, although marginally compared to touch /tmp/new_file.

But of course it does not stop there. As mentioned, anything that can perform open(), openat() and, create() syscalls will create a file. Hence, we can do:

  • true|dd of=/tmp/newfile
  • truncate --size 0 /tmp/new_filename.txt
  • cp /dev/null /tmp/null_file
  • true|tee /tmp/some_other_file
  • mktemp or tempfile ( for creating temporary files that do not need to exist between reboots !)

Of course, all the above mentioned utilities do not exclusively create files. But they perform the necessary syscall and that allows us to adapt the commands.


Of course, at the level of programming or scripting we may want to create a file as well, especially for efficiency purposes (because calling external commands such as touch from a Perl script or Python will require additional resources).

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

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 

And in Perl:

$ perl -e 'open(my $fh,">","/tmp/perlfile")'

1 Other types of files such as hard/soft links,character or block special devices, directory,named pipes, or sockets require entirely different syscalls.

You must log in to answer this question.

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