#!/usr/bin/perl -w use strict; use constant BLOCKSIZE => 128 * 1024; die "Usage: $0 numbytes file" unless @ARGV > 1; my $remove_bytes = shift @ARGV; my $file = shift @ARGV; open(my $fh, "+<", $file) or die "Couldn't open $file: $!\n"; if(-s $fh > $remove_bytes ) { seek $fh, $remove_bytes, 0; my $total_in = 0; while(my $bytes_in = read $fh, my $buf, BLOCKSIZE) { seek $fh, $total_in, 0; print $fh $buf; last if $bytes_in < BLOCKSIZE; $total_in += $bytes_in; seek $fh, $remove_bytes + $total_in, 0; } } truncate $fh, tell $fh;