Thursday, April 26, 2012

Truncate Large Text File in UNIX / Linux

How do I truncate or shrink large text file under UNIX / Linux operating systems?

There are various tools to truncate large text files under UNIX / Linux operating systems.

Options #1: Shell Output Redirction

Your shell can truncate text file and make the size to zero using redirection:
> {filename}
ls -l largefile.txt
> largefile.txt
ls -l largefile.txt
Please note that largefile.txt file is created if it doesn't exist. And largefile.txt file is overwritten if it exits.

Option #2: truncate Command

Use the truncate command to shrink or extend the size of each FILE to the specified size:
truncate -s 0 {filename.txt}
ls -lh filename.txt
truncate -s 0 filename.txt
ls -lh filename.txt
The -s option is used to set SIZE to zero. See truncate command man page for more details:
man truncate

Option #3: logrotate Utility

logrotate command is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large. See how to use logrotate command to rotates, compresses, and mails system logs stored in /var/log and other locations under UNIX / Linux oses.

No comments:

Post a Comment