How to create an empty file from command line

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


Solution 1:

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

Solution 2:

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

Solution 3:

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*.*

Solution 4:

Using vim editor you can also create an empty file.

vim filename

Then save

:wq

Solution 5:

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