[tachyon@www cgi-bin]# cat ./test.pl #!/usr/bin/perl my $file = '/tmp/test.txt'; open FILE, ">$file" or die $!; print FILE "foo\nbar\nfoo\nbar\nfoo\nbar\nfoo\nbar\nfoo\nbar\nfoo\nbar\n\n\r\n\n\r"; close FILE; # grab the last 20 bytes for analysis open FILE, $file or die $!; { local $/ = undef; seek FILE, -20, 2; $EOF = ; } close FILE; my $file_length = -s $file; (my $newlines) = $EOF =~ m/([\015\012]+)\z/; my $num_newlines = length $newlines; print "File length $file_length bytes, number of newlines $num_newlines\n"; truncate $file, ($file_length - $num_newlines) or die $!; print "Now file is ", -s $file, "bytes\n\n"; [tachyon@www cgi-bin]# ./test.pl File length 53 bytes, number of newlines 6 Now file is 47 bytes [tachyon@www cgi-bin]#