How to empty a file on Linux and Mac

less than 1 minute read

Quite recently, I had to clear up some space on my machine and was going through the clean-up procedure. A software took some time which I had some tea and then the software came up with the list of folders arranged properly showing which folder contained what amount of data.

Surprisingly, my Apache logs were too big. One of the logs was about 2 GB. Since this is a development machine, I was little surprised. Then I remembered how a lot of PHP errors related to accessing file systems dumped a whole lot of data into the log file. No problem, I now had enough reason to not wonder and go about cleaning it. So how do you clean it? There were two thoughts:

  • Delete the file and recreate it.
  • Do something so that the file size is set to zero while the file still exists. Kind of ‘Truncate’; but for a file, not a database table.

The second one was interesting. So here is what I did: Emptying a file

cat /dev/null > path/to/file

All done. Simple and easy. No need to recreate file, change permissions and so on. Hope that helps you some day!

Updated:

Leave a comment