Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question

4 Answers 4

up vote 70 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
share|improve this answer
1  
You can use touch newfile.txt or some other extension, too (if you need to specify the extension). –  gotqn Apr 26 at 8:18
> 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.

share|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
    
Wow now I think this is called cheating the system –  banarun Jun 23 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*.*
share|improve this answer
5  
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 Any Text editor you can also create an empty file.

vim filename

Then save

:wq
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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