ozbsla
Last Updated: February 25, 2016
·
860
· mlafeldt
2190d7a468f51fa3be5eabfc9397a28b

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

Say Thanks
Respond

5 Responses
Add your response

3447
$ 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 ·
3449
Mich400x400

@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 ·
3473
2190d7a468f51fa3be5eabfc9397a28b

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 ·
3486
2190d7a468f51fa3be5eabfc9397a28b

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

over 1 year ago ·
4589
70e1167dbde5397557b0ec6fabb3d90a

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 ·
Filed Under