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

link|improve this question

feedback

4 Answers

up vote 45 down vote accepted

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
link|improve this answer
feedback
> 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.

link|improve this answer
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
That's good info. Can you add it to the answer itself (warning: this will overwrite...). – Tshepang Jan 15 '11 at 15:23
@Tshepang added. – balki Jan 15 '11 at 17:11
feedback
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*.*
link|improve this answer
4  
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
feedback

Using Any Text editor you can also create an empty file.

vim filename

Then save

:wq
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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