How to empty (truncate) Log files in Linux

(: October 2, 2018)

At some point in Sysadmin life cycle, you may need to empty a log file to save on system disk space or for any other reason. There are various ways you can empty a file in a Linux system.

Empty log file using truncate command

The safest method to empty a log file in Linux is by using the truncate command. Truncate command is used to shrink or extend the size of each FILE to the specified size.

truncate -s 0 logfile

Where -s is used to set or adjust the file size by SIZE bytes. The file can be relative to the current directory or an absolute path to the file provided.

.td_uid_2_5d9064642e412_rand.td-a-rec-img{text-align:left}.td_uid_2_5d9064642e412_rand.td-a-rec-img img{margin:0 auto 0 0}

For complete truncate command options, use the option --help

Usage: truncate OPTION... FILE...
Shrink or extend the size of each FILE to the specified size

A FILE argument that does not exist is created.

If a FILE is larger than the specified size, the extra data is lost.
If a FILE is shorter, it is extended and the extended part (hole)
reads as zero bytes.

Mandatory arguments to long options are mandatory for short options too.
  -c, --no-create        do not create any files
  -o, --io-blocks        treat SIZE as number of IO blocks instead of bytes
  -r, --reference=RFILE  base size on RFILE
  -s, --size=SIZE        set or adjust the file size by SIZE bytes
      --help     display this help and exit
      --version  output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

SIZE may also be prefixed by one of the following modifying characters:
'+' extend by, '-' reduce by, '<' at most, '>' at least,
'/' round down to multiple of, '%' round up to multiple of.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/truncate>
or available locally via: info '(coreutils) truncate invocation'

Empty log file using :> or true >

You can also use :> to clear file content. The syntax is

:> logfile

This is equivalent to

true > logfile

See example below

Empty log file using echo command

If you echo nothing to a file, it will clear the content to empty it.

echo "" > logfile

This is the same as

echo  > testfile

Empty log file using the dd command

The syntax for using dd command is

dd if=/dev/null of=logfile

or

dd if=/dev/null > logfile

See examples below

$ ls -l testfile 
-rw-r--r-- 1 jmutai jmutai 1338 Oct  2 23:07 testfile

$ [[email protected] tmp]$ ls -l testfile 
-rw-r--r-- 1 jmutai jmutai 1338 Oct  2 23:07 testfile

[[email protected] tmp]$ dd if=/dev/null of=testfile 
0+0 records in
0+0 records out
0 bytes copied, 0.000322652 s, 0.0 kB/s

[[email protected] tmp]$ ls -l testfile 
-rw-r--r-- 1 jmutai jmutai 0 Oct  2 23:33 testfile

For multiple files, a simple loop in bash should suffice.

for file in logfile1 logfile2 logfile2 ... ; do
    truncate -s 0 $file 
    or
    dd if=/dev/null of=$file
    or
    :>$file
done

Use any of the method to empty your large log files.

.td_uid_4_5d9064642e62d_rand.td-a-rec-img{text-align:left}.td_uid_4_5d9064642e62d_rand.td-a-rec-img img{margin:0 auto 0 0}

Related Posts