How to clear the contents of a txt file in Linux?

To clear the content of a txt file, you can use the following command:

  1. Redirect a null character to a file using the “>” symbol.
> filename.txt
  1. Redirect the file content to the null device using “/dev/null”.
cat /dev/null > filename.txt
  1. Output an empty string to a file using “echo -n”.
echo -n "" > filename.txt

All of the above commands will empty the contents of the file, with “filename.txt” being the name of the file to be cleared.

bannerAds