As we know nowadays data is present everywhere, especially in our computer systems. Sometimes we have to play around with data like creating data, removing data, changing the location of data, etc

Linux provides some methods to remove the contents inside the file without removing the file completely. 

This blogpost will determine how to clear the contents of a file in Linux from the command line.

How to clear the contents of a file from the command line?

To clear the contents of a file from the linux command line, the following methods are used

  • Using cat command
  • Using echo command
  • Using cp command
  • Using truncate command

Let’s talk about them one by one.

How to clear the contents of a file from the command line Using cat Command

The cat command in Linux serves multiple purposes. It is used to create files as well as to clear the contents of a file from the command line.

The syntax is demonstrated below:

$ cat /dev/null > path_to_file

Where /dev/null is a placeholder used to write content to the file as well as to erase it.

For instance, let’s clear the content inside the Motivational_quotes file shown below:

It can be seen that after the execution of the above command, the content inside the Motivational_quotes.txt file has been deleted successfully.

How to clear the contents of a file from the command line Using Echo Command

The echo command is frequently used to write data to text files but it can also be used to delete the content inside a file.

The syntax is shown below

$ echo " " > path_to_file

It can be seen that after the execution of the above command, the content inside the file2.txt file has been deleted successfully.

How to clear the contents of a file from the command line Using cp Command

Copy or cp enables you to make copies of larger files and replace them with empty ones.

The syntax of the cp command is listed below:

$ cp old_path new_path

It can be seen that the content.txt file is replaced with an empty file.

How to clear the contents of a file from the command line Using Truncate Command

Linux’s truncate command allows users to shrink files to a specific size. To completely clear the content of a file use the truncate command with size 0.

The syntax of the truncate command is demonstrated below:

$ truncate -s 0 path_to_file

For instance, let’s delete the content inside the main.txt file as shown below:

It can be seen that after the execution of the above command, the content inside the main.txt file has been deleted successfully.

That’s all about clearing the contents of a file using the command line.

Conclusion

There are four methods to clear the contents of a file from the command line. We can use the cat command with the cat /dev/null/ path_to_file syntax to remove the content inside the file. Similarly, we can use the echo command with the echo “” > path_to_file syntax to remove the file’s content. The cp command with cp old_path new_path syntax also removes the file’s content and the truncate command with truncate -s 0 path_to_file syntax removes all the contents of the file.