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


cat /dev/null > file.txt 

Executing this command will make the file blank.

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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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