How To: Empty a text file from the command line

h@ker

Active member
Joined
Jun 18, 2007
Messages
11,661
Reaction score
0
Points
36
Terminal-Screenshot.png
Web site Hackaddict.net demonstrates how to remove the contents of a plain text file on Windows, Linux, and Mac without manually deleting the file and recreating it. This can be done by copying the contents of an empty system file into the file to be emptied:
This will create a new file named t whose contents is the word "test":
$ echo "test" > t
more will display the contents of the file:
$ more t
cat reads the contents of /dev/null (which contains nothing) and > writes this into t$ cat /dev/null > t
The next time you open your file its contents will be empty. Windows users take notice -- when you run the command "cat /dev/null" Windows will throw an error. However, it will still successfully empty the contents of your file.
Delete the Contents of a File without Removing and Recreating It [hackaddict.net]
</img>


More...
 
Back
Top