Sometimes, we need to clear the contents of a log file or text file which is continuously increasing in size to save the space on the hard disk. In Linux, we can easily do it and there are many commands to clear the content of the file to make the file empty. The most used command in Linux systems to clear the content of a file is cat /dev/null > filename.log
If we want to clear the content of log file /var/log/messages then we have to use the below command
# cat /dev/null > /var/log/messages
Clear the Contents of a File in Windows
In Window, we can clear the content of a text file or log file using the cat command and it really works
- Search the Command Prompt and Run as administrator.
- First, check the size of the file named logFile.log, here it is around 15 MB.
C:\windows\system32>dir F:\logFile.log Volume in drive F is New Volume Volume Serial Number is 3446-545B Directory of F:\ 16-07-2018 14:52 15,803,465 logFile.log 1 File(s) 15,803,465 bytes 0 Dir(s) 71,806,472,192 bytes free</pre?
- Run the command cat /dev/null > logFile.log to clear the contents of the file. It will give you an error but the contents of the file will be removed which we will verify in the next step.
C:\windows\system32>cat /dev/null > F:\logFile.log 'cat' is not recognized as an internal or external command, operable program or batch file.
- Verify the size of the logFile.log file after clearing the contents, it becomes 0 bytes.
C:\windows\system32>dir F:\logFile.log Volume in drive F is New Volume Volume Serial Number is 3446-545B Directory of F:\ 12-08-2020 16:57 0 logFile.log 1 File(s) 0 bytes 0 Dir(s) 71,822,176,256 bytes free
Note: I have done this activity on Microsoft Windows 10.
Last Updated: August 19, 2020
Solaire
type nul > file_here
worked for me without erro
JohnA
LOL…Surprisingly this works.
Sergei
Thanks for the article, it is usefull. It also works if you type the same without cat:
/dev/null > logFile.log
And as Solaire said, it also works with:
nul > logFile.log
Despite it appears an Internet Security Configuration warning in my case, the file gets finally cleared.
Regards!