Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Thanks.

share|improve this question

2 Answers 2

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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