Linux/Unix – truncate a large log file without deleting it

Sometimes we need to truncate (make it size 0) a large opened file by a running process (e.h. php error log, apache error logs, custom log files, etc.) Deleting the file may not be a good idea as application has opened a file handle on it. It can be done using command truncate on Linux/Unix/Mac.

## -s 0 sets new size to 0
$ truncate -s 0 filename

Few points to note

  • On mac truncate can be installed using
    $ brew install truncate
  • Truncate does not change inode number of the file
    ## Check inode number of file1.txt
    $ ls -i file1.txt 
    412882 file1.txt
    $ truncate -s 0 file1.txt 
    $ ls -i file1.txt 
    412882 file1.txt
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments