Truncate File

trunc.pl is a simple Perl script to truncate files, for example logs and other data files.  Many Pikt scripts invoke trunc.pl.  trunc.pl sources from the trunc_programs.cfg file.

#!/usr/bin/perl

# trunc.pl -- truncate a file

die "Usage:  trunc.pl <#lines> <file>\n" unless $#ARGV == 1 ;

$LINES = $ARGV[0] ;
$FILE = $ARGV[1] ;

$BAK = "$FILE.bak.gz" ;
$TMP = "$FILE.tmp" ;

die "no $FILE file!\n" if (! -e $FILE) ;

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,
 $mtime,$ctime,$blksize,$blocks) = stat($FILE) ;

unlink($BAK) ;
system("/usr/bin/gzip -c $FILE > $BAK") ;
chown($uid, $gid, $BAK) ;
chmod($mode, $BAK) ;

system("/usr/bin/tail -$LINES $FILE > $TMP") ;
chown($uid, $gid, $TMP) ;
chmod($mode, $TMP) ;
rename($TMP, $FILE) ;

For more examples, see Samples.

Home | FAQ | News | Intro | Samples | Tutorial | Reference | Software
Developer's Notes | Licensing | Authors | Pikt-Users | Pikt-Workers | Related Projects | Site Index | Privacy Policy | Contact Us
Page best viewed at 1024x768 or greater.   Page last updated 2019-01-12.   This site is PIKT® powered.
Copyright © 1998-2019 Robert Osterlund. All rights reserved.
Home FAQ News Intro Samples Tutorial Reference Software
PIKT Logo
PIKT Page Title
Learn about
partial
configurations