Open Laboratory for Technocrats

Grab the developer role, learn concepts & prepare with senior software engineers to get solutions for problems in a software job. Coding and Programming Solutions crafted for you.

Ubuntu - Delete content of file not file itself


Sometimes we usually keep the log of everything that is coming to out console output or what ever we are performing on the server.

Like cron job handling and processing data at several time intervals for which the problem occurs with the size of data which is being stored on the file if you are using file log storage.

Once you have taken the backup of the log you might need to clean the file from the system.
But for that either you need to delete the file and regenerate the file with all the permissions that are required to keep the write access up on the log file or we can keep the same file up and delete the content of the file.

Second option seems easy which eliminates few extra steps involved in the first step.

Lets see how we can achieve this by following different commands on Ubuntu

  • > filename
    • If above command fails then use sudo as: sudo bash -c "> filename"
  • truncate -s 0 filename
  • echo -n > filename
  • echo "" > foo_file



Top #3 Articles