Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way to delete the contents of a file in BASH?

Tags:

bash

I need to delete all lines in a file, but leave essentially an empty file with no content and no lines. As best I know, one can do this:

rm file.txt 2> /dev/null
touch file.txt

However, is there a simpler, more canonical solution in BASH?

like image 415
Village Avatar asked Mar 14 '14 10:03

Village


1 Answers

This is a pretty fast way I always use:

> file.txt

It completely empties the file and updates the modification time.

like image 197
fedorqui 'SO stop harming' Avatar answered Oct 13 '22 11:10

fedorqui 'SO stop harming'