" I found this command several years ago and have used it since then to clear the file. But how does it work? :> used as :>...">

Explain ":>"

I found this command several years ago and have used it since then to clear the file.

But how does it work?

:>

used as

:> /server/logs/access_log

+4
source share
4 answers

> - redirection operator. : is a built-in shell command and is equivalent to true , it is used mainly as a placeholder in scripts.

:> filename will make your file empty.

+7
source

In bash, help : you get:

 :: : No effect; the command does nothing. A zero exit code is returned. 

It is worth noting that, as mentioned earlier : usually replaces the truth, as in

 while : do date sleep 60 done 

But, strictly speaking,: it is not needed in your team, and you can just write

 > /server/logs/access_log 

To avoid trimming important files unintentionally, it is best to set the noclobber parameter

 set -o noclobber 

and if you try to do it, he will spit

 -bash: /server/logs/access_log: cannot overwrite existing file 

if you want to force him

 >| /server/logs/access_log 
+7
source

Being : a do-nothing statement in bash (equivalent to true , as WebMonster points out), this command actually creates and truncates the size 0 of the next file.

Note that you can usually use touch to create an empty file that does not exist, but if the file exists and contains data, you should empty it. This is a way to do both things at once.

+3
source

I assume that you are using a shell on the unixoide operating system. Typically, the ">" command sends the output of the command to a file.

I'm not quite sure what β€œ:” does, but it seems that this part is β€œnothing” that is sent to your file.

0
source

All Articles