Linux – Split command equivalent/replacement that gives total count of pieces/parts

linuxunix

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).

Thanks!

Wodow

Best Answer

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
414