Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

The standard split command on Unix/Linux allows you to split files into fixed size pieces in terms of bytes or lines, with the output file names suffixed with letters or numbers (e.g. PREFIXaa, PREFIXab, ... or PREFIX1, PREFIX2, ...)

However, it's not clear from the output names alone how many pieces there should be.

Is there a standard tool that will do this, i.e. outputting to e.g. PREFIX1of4, PREFIX2of4... or similar? Or is this a job for Perl (i.e. I find a Perl script).



share|improve this question

2 Answers 2

up vote 2 down vote accepted

if you need to fully identify the "piece number" from the generated "piece files" names you can use "-d" and have the suffix argument to be a directory name where the "piece files" should be generated. Using "-a" you can specify the number of digits to be used to generate the suffix names.

example: split the file "cfg.pm", 3 lines per output file; use 3 digits for the generated suffixes (making sure the name space isn't exhausted); use number-only suffixes; trick split to generate digit-only file names by specifying a directory as a prefix

mkdir ./s
split -a 3 -l 3 -d cfg.pm s/

after that,

ls s | tail -1

will implicitly give you the number of generated files

ls s|tail -1
share|improve this answer
errata: "ls s | tail -1" will give you the number of generated files minus 1 because the suffix generator starts with zero –  sysfault Nov 23 '10 at 12:34
Thanks - short and sweet! –  wodow Nov 29 '10 at 10:30

Picking up from adirau's answer:

If your system has the Perl script called rename (and it's a version that will support doing this) you can rename your files so that each number is incremented by one and have a prefix and a suffix:

count=$(($(ls s | tail -n 1) + 1))
rename "my \$n=sprintf(\"%03d\", \$_+1); my \$c=sprintf(\"%03d\", $count); s/.*/prefix\${n}of\${c}/" *

This would rename the files from:




This is the bulk of that Perl script, by the way:

use strict;

use Getopt::Long;

my ($verbose, $no_act, $force, $op);

die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n"
    unless GetOptions(
        'v|verbose' => \$verbose,
        'n|no-act'  => \$no_act,
        'f|force'   => \$force,
    ) and $op = shift;

$verbose++ if $no_act;

if (!@ARGV) {
    print "reading filenames from STDIN\n" if $verbose;
    @ARGV = <STDIN>;
for (@ARGV) {
    my $was = $_;
    eval $op;
    die $@ if $@;
    next if $was eq $_; # ignore quietly
    if (-e $_ and !$force)
        warn  "$was not renamed: $_ already exists\n";
    elsif ($no_act or rename $was, $_)
        print "$was renamed as $_\n" if $verbose;
        warn  "Can't rename $was $_: $!\n";
share|improve this answer
Thanks also - a good coda. –  wodow Nov 29 '10 at 10:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.