Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

There is a standard command for file splitting - split.

For example, if I want to split a words file in several chunks of 10000 lines, I can use:

split -dl 10000 words wrd

and it would generate several files of the form wrd.01, wrd.02 and so on.

But I want to have a specific extension for those files - for example, I want to get wtd.01.txt, wrd.02.txt files.

Is there a way to do it?

share|improve this question

4 Answers 4

up vote 4 down vote accepted

Not with split, but you can easily rename them afterwards, or you can do it in awk:

awk '{filename = "wrd." int((NR-1)/10000) ".txt"; print >> filename}' inputfile
share|improve this answer
Looks good - but does not work. In your form, complains about "expression for `>>' redirection has null string value", and if "file" is "changed" to "filename", outputs files of the form wrd.{file number}.{line number}.txt (quite a lot of them :) –  Rogach Feb 25 '12 at 5:15
@Rogach Sorry, I hadn't tested it, so I forgot awk doesn't do integer division. I've tested this one. –  Kevin Feb 25 '12 at 6:07

This wasn't available back then but with more recent versions (≥ 8.16) of gnu split one can use the --additional-suffix switch to have control over the resulting extension. From man split:

              append an additional SUFFIX to file names.

so when using that option:

split -dl 10000 --additional-suffix=.txt words wrd

the resulting pieces will automatically end in .txt:

share|improve this answer
Not working on mac –  ericgu Feb 10 at 4:51
@swiftshokunin - well, that's quite an enlightening comment... What is not working on mac ? Are you using gnu split or another flavor of split on your mac ? Which version ? How do you invoke it ? –  don_crissti Feb 10 at 13:08
I love your sarcasm. I am a unix n00b from the Apple world. I am using OS X Yosemite and I just didn't want others to crash and burn like I did. I tested and reviewed at the docs and we don't have this parameter. I might have missed something. developer.apple.com/library/mac/documentation/Darwin/Reference/… –  ericgu Feb 10 at 15:46
@swiftshokunin - my answer pertains to gnu split, part of gnu coreutils. It is also available on OSX if you install coreutils via homebrew but note that by default, on OSX, the gnu utilities have a g prepended to their name (e.g. gstat instead of stat) so you invoke it as gsplit (or alter the PATH as per the guide here if you want to use it as split over the OSX split). HTH. –  don_crissti Feb 10 at 16:50
helpful thanks don –  ericgu Feb 10 at 16:56

Such tasks are best managed with the shell. Use split and then write a simple loop to rename the files. E.g.

for file in wrd.*
    mv "$file" "$file.txt"

would rename your wrd.01, wrd.02, etc. files so they all have a .txt extension.

share|improve this answer
That's quite obvious, but it would break the conciseness of bash script. –  Rogach Feb 25 '12 at 5:08
The Unix philosophy is to provide you with a set of simple tools that you then combine to do a job. The "conciseness of the bash script" was not a stated requirement in your question. –  Kyle Jones Feb 25 '12 at 5:19
I agree, but there are different combinations of simple tools out there, and some do job better. I just hoped that there was a good combination that could do this job better that a 5-line long split with file renaming. –  Rogach Feb 25 '12 at 5:20
Just a personal obversation: When you actually get both versions to do the same thing with similar var names... awk 79 chars, split+mv 78 chars - - - - awk '{f="wrd." sprintf("%03d",int((NR-1)/10000)) ".txt"; print >> f}' inputfile - - - - split -dl10000 -a3 inputfile wrd.; for f in wrd.???; do mv "$f" "$f".txt; done –  Peter.O Feb 25 '12 at 10:01
PS: the split+mv combo is more that 6 times faster than awk (approx 3s vs 18s) for a 10 million line input file (75 MB)... the text in each line was its own line-number... Thanks for re-stating the "obvious" :) –  Peter.O Feb 25 '12 at 10:36

The --additional-suffix option of split was added in GNU coreutils 8.16.

split now accepts the --additional-suffix option, to append an additional static suffix to output file names.


share|improve this answer

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.