Issue is, all the below listed commands make the file size 0 (Zero) but for some time.
When the new logs are generated then log file size is = old file size(before truncate) + new log messages size.
This means that truncate does not free the disk space occupied by the file.
File size should be whatever the size of new log messages those are coming after truncate command.
I have tried below options before asking this question.
- $ > sysout.log
- $ : > sysout.log
- $ cat /dev/null > sysout.log
- $ cp /dev/null sysout.log
- $ dd if=/dev/null of=sysout.log
- $ echo "" > sysout.log
- $ echo -n "" > sysout.log
- $ truncate -s 0 sysout.log
At first, I checked the file size (contains 10 lines of log messages)
[root@mylaptop ~]# ls -lh sysout.log
-rw-r--r-- 1 root root 6.0K Dec 2 11:30 sysout.log
Then ran the truncate command
[root@mylaptop ~]# truncate -s0 sysout.log
[root@mylaptop ~]# ls -lh sysout.log
-rw-r--r-- 1 root root 0 Dec 2 11:31 sysout.log
After few seconds 2 lines of log messages printed into the file. But file size is
[root@mylaptop ~]# ls -lh sysout.log
-rw-r--r-- 1 root root 6.3K Dec 2 11:31 sysout.log
As you can see, it adds the file size.
How to free the disk space? Or is there any another approach?
ls > out.txt; truncate -s 0 out.txt; touch out.txt; echo abc >>out.txt, the file out.txt contains only abc, nothing else.