Truncate: UNIX Binary Log File

How do I truncate a UNIX binary log file using command line options?

You can use perl as follows to truncates the file opened on FILEHANDLE, or named by EXPR, to the specified length. You can also use truncate command as discussed below.


perl -e'truncate "filename", LENGTH'
perl -e'truncate "foo", 122455'

truncate UNIX command

Various version of UNIX and Linux like operating system comes with the truncate command to shrink or extend the size of a file to the specified size:

truncate -s SIZE fileName
truncate -s 2000KB fileName
truncate -s 2000MB fileName
truncate -s 20GB fileName

The SIZE may also be prefixed by one of the following to adjust the size of each FILE based on their current size:

          +  => extend by
          -  => reduce by
          <  => at most
          >  => at least
          /  => round down to multiple of
          %  => round up to multiple of

The above syntax is based on GNU truncate command. See local man page for more info:
$ man truncate

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.