An esay way to empty a config file in Linux?

5

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

Thanks.

superuser

Posted 2012-02-11T04:52:17.313

Reputation: 3 297

Answers

8

cat /dev/null > file.txt 

Executing this command will make the file blank.

Jim G.

Posted 2012-02-11T04:52:17.313

Reputation: 2 794

12Or just > file.txt. Shorter, but also will empty the file. – lik – 2012-02-11T05:56:49.493

0

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

Marco

Posted 2012-02-11T04:52:17.313

Reputation: 4 015

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 – 2013-10-10T19:07:43.073