Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to empty a configuration file in Linux without deleting it so that it won't lose the permissions?


share|improve this question
up vote 8 down vote accepted
cat /dev/null > file.txt 

Executing this command will make the file blank.

share|improve this answer
Thanks. Just what I was looking for. – superuser Feb 11 '12 at 5:07
Or just > file.txt. Shorter, but also will empty the file. – lik Feb 11 '12 at 5:56

Another solution is

printf '' > file

The >file trick has a slightly different behaviour depending on the used shell. In zsh for instance, it's required to terminate the redirection with ctrl-d if NULLCMD is set to cat (which is the default).

share|improve this answer
To make > file work as in bash you simply need to set NULLCMD=:. The default is NULLCMD=cat, so > file is identical to cat > file which needs to be terminated by EOF, i.e. CTRL+D. – mpy Oct 10 '13 at 19:07
@mpy Thanks, I added that to my answer. – Marco Oct 10 '13 at 19:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .