Split command to create file with a number as a filename

filenamessplit

I am using the split command to split a 40GB file. I want the resulting split files to be named using incrementing numbers starting from 1 like 1, 2, 3 . . .

Is this possible ?

I am currently using the command split --numeric-suffixes=1 -l 2 t5 and getting filenames as follows:

x01  x02  x03  x04  x05  x06  x07  x08  x09  x10  x11  x12  x13  x14  x15  x16  x17

Best Answer

Using GNU split, yes:

split --numeric-suffixes=1

will use numeric suffixes, starting from 1. (You still need to specify a prefix if you don’t want the default x.)

To obtain filenames which are only numbers, you can specify an empty prefix:

split --numeric-suffixes=1 -l 2 t5 ""

split always uses suffixes of the same length, so the default produces 01, 02 etc. If you want to avoid leading zeroes, you need to post-process the result (and live with the sub-optimal sorting behaviour):

rename 's/^0+//' 0*

You also need to ensure that split’s suffix length provides enough room for all the files you’ll need; the default, two digits, allows for 99 files if you start from 1. You can specify more digits using -a, e.g. -a 3, -a 4, etc.

(split stops when it runs out of suffixes, with an error. If you stick to its defaults it will automatically increase the suffix length as necessary, but giving it a start suffix disables this.)

Related Question