Last Updated: February 25, 2016
· mlafeldt

Easiest way to create or empty a file in Shell

The shell redirection operator > allows you to create a new empty file:

$ > path/to/file

The same command can be used to empty an existing file (use with caution).

5 Responses
Add your response

$ touch path/to/file

Also works, but won't empty an existing file. May be safer to not accidently empty an existing file.

over 1 year ago ·

@kornnflake touch also updates the accessed/modified date/time for existing files. You can even add the option --no-create to it so it only touches files that already exist, not creating any new files.
(just as a side note)

over 1 year ago ·

Thanks for your comments. I know about touch and the dangers of > file. But the latter is faster to type and less people might know that redirections can be used this way. Perhaps a better title would have been "Easiest way to create or empty a file in Shell".

over 1 year ago ·

FYI, I've updated the protip (title and contents) in response to your comments. Thanks again.

over 1 year ago ·

this is great thanks bro!

I just use

touch path/to/file

as well but will definitely start to use this a lot.

over 1 year ago ·