5
Is there a way to empty a configuration file in Linux without deleting it so that it won't lose the permissions?
Thanks.
5
Is there a way to empty a configuration file in Linux without deleting it so that it won't lose the permissions?
Thanks.
8
cat /dev/null > file.txt
Executing this command will make the file blank.
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).
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
12Or just
> file.txt
. Shorter, but also will empty the file. – lik – 2012-02-11T05:56:49.493