How to split single file into multiple files based on rows

Updated on April 3, 2018

Here’s a question from one of our regular reader Anu. She likes to know how to split single file into multiple files based on number of lines or rows. Below is the actual question.

I have file with 15000 lines. I would like to know if there is a command in Linux to split a file into multiple files with each file containing 1000 lines. So ideally, I need 15 files containing 1000 lines each.

Solution:

Well, you can use split command in Linux. Split is an useful utility that allows you to split large files into a smaller files. For example, a file containing 15000 lines can be split into 15 files and each containing 1000 lines.

$ split -a 2 -d -l 1000 content.txt split

The above command will split content.txt file into multiple files (starting from filename split00) and each with 1000 lines or less. Assuming 15000 lines in content.txt will create 15 files starting from split00 to split14.

Let’s understand the options now.

-a 2 : means use a unique 2 character suffix for every file. E.g., 00 to 99

-d : means suffix will be a number.

-l 1000: means split a file by line and store 1000 lines or less in each file.

That’s it!

Was this article helpful?

Related Articles

Leave a Comment