randomfox (randomfox) wrote,
randomfox
randomfox

Split a file

Perl script that splits a file into 100 million-byte pieces. It system()s out to the dd utility, so it could be rewritten in pure Perl.

Usage example: split.pl largefile lf
will produce lf0, lf1, lf2, lf3... until there are no more pieces.

#!perl -w
use strict;

@ARGV >= 2 or die "Usage: $0 file prefix\n";

my $fname = shift;
my $prefix = shift;

my $fsize;
defined($fsize = -s $fname) or die "Error stating file $fname: $!\n";

my $splitcount = int($fsize / 1e8);

for my $i (0 .. $splitcount) {
    print "Split $i of $splitcount...\n";
    system "dd if=$fname of=$prefix$i bs=100MD count=1 skip=$i";
}

__END__

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments