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:
- Redirect a null character to a file using the “>” symbol.
> filename.txt
- Redirect the file content to the null device using “/dev/null”.
cat /dev/null > filename.txt
- 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.