Empty a file without deleting it

At times, you might want to just empty the file contents without deleting it. Here is one option

server:/var/log$ ls -lt debug.log
-rw-rw-rw- 1 www-data www-data 76718817 Jan 20 08:09 debug.log
server$ cat /dev/null > debug.log
server:/var/log$ ls -lt debug.log
-rw-rw-rw- 1 www-data www-data 0  Apr 30 17:07 debug.log

Notice the size of debug.log changed to "0" bytes.

/dev/null is a special file called the null device in Unix/Linux based systems. It is also referred to as bit-bucket or the blackhole (yes, similar to the blackholes in space) because it immediately discards anything written to it and only returns an end-of-file EOF when read.

So, we are utilizing the null device and redirection mechanism to redirect the output of /dev/null to the file that needs to be emptied. The cat command is used to output the contents of /dev/null which is nothing but an empty file.