# split a file into multiple files

**URL:** https://community.unix.com/t/split-a-file-into-multiple-files/217639
**Category:** UNIX for Dummies Questions & Answers
**Created:** [October 23, 2008, 12:09am UTC](https://community.unix.com/t/split-a-file-into-multiple-files/217639 "2008-10-23T00:09:10Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![kumar66](https://community.unix.com/letter_avatar/kumar66/32/5_5575768a8748004e209b776fc1b2916d.png) [@kumar66](https://community.unix.com/u/kumar66)
#### Post date: [October 23, 2008, 12:09am UTC](https://community.unix.com/t/split-a-file-into-multiple-files/217639/1 "2008-10-23T00:09:10Z")

</div>

Hi All,

I have a file ABC.txt and I need to split this file on every 250 rows.

And the file name should be ABC1.txt , ABC2.txt and so on.

I tried with split command

split -l 250 \<filename\> '\<filename\>'

but the file name returned was  
ABC.txtaa  
ABC.txtab.

Please Advise.

Thanks & Regards,  
Kumar66

---

<div class="post-metadata">

### Author: ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)
#### Post date: [October 23, 2008, 12:52am UTC](https://community.unix.com/t/split-a-file-into-multiple-files/217639/2 "2008-10-23T00:52:41Z")

</div>

Afaik, there is no such option for split, as stated in the man page. Split on Debian for example offers a -d for using numbers instead of letters, but it will be always a suffix at the end and not be positioned somewhere inside/between a string.  
You might have to accept it as it is or could move the splitted files to the desired names.

---

<div class="post-metadata">

### Author: ![kumar66](https://community.unix.com/letter_avatar/kumar66/32/5_5575768a8748004e209b776fc1b2916d.png) [@kumar66](https://community.unix.com/u/kumar66)
#### Post date: [October 23, 2008, 2:09am UTC](https://community.unix.com/t/split-a-file-into-multiple-files/217639/3 "2008-10-23T02:09:15Z")

</div>

hi ,

Thanks for your reply. Is there any way to split the files ? Or by writing a script?

As I am new to Unix , I am not familar with scripts .

Please Advise.

Thanks & Regrads,  
Kumar.

---

<div class="post-metadata">

### Author: ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)
#### Post date: [October 23, 2008, 3:11am UTC](https://community.unix.com/t/split-a-file-into-multiple-files/217639/4 "2008-10-23T03:11:44Z")

</div>

> [@](#):
>
> Is there any way to split the files ?

Uhm you used split already to split files... 😕  
Yes, you can achieve something like that with awk head etc. for example.

---

<div class="post-metadata">

### Author: ![vimes](https://community.unix.com/letter_avatar/vimes/32/5_5575768a8748004e209b776fc1b2916d.png) [@vimes](https://community.unix.com/u/vimes)
#### Post date: [October 23, 2008, 5:59am UTC](https://community.unix.com/t/split-a-file-into-multiple-files/217639/5 "2008-10-23T05:59:10Z")

</div>

You could use split, since it does a good job.  
Since split makes the files alphabetically, you could change the filenames afterwards, like so (leave out the "echo" if you want to execute the mv, this is just to show what it would do).

```nohighlight
# j=0; for i in `ls -1 ABC.txt??`; do j=`expr $j + 1`; echo mv $i ABC$j.txt; done
mv ABC.txtaa ABC1.txt
mv ABC.txtab ABC2.txt
mv ABC.txtac ABC3.txt
mv ABC.txtad ABC4.txt
mv ABC.txtae ABC5.txt
mv ABC.txtaf ABC6.txt
mv ABC.txtag ABC7.txt
mv ABC.txtah ABC8.txt
mv ABC.txtai ABC9.txt

```

---

<div class="post-metadata">

### Author: ![palsevlohit\_123](https://community.unix.com/user_avatar/community.unix.com/palsevlohit_123/32/1053_2.png) [@palsevlohit\_123](https://community.unix.com/u/palsevlohit_123)
#### Post date: [October 23, 2008, 6:00am UTC](https://community.unix.com/t/split-a-file-into-multiple-files/217639/6 "2008-10-23T06:00:18Z")

</div>

can you try with "csplit"?

---

<div class="post-metadata">

### Author: ![palsevlohit\_123](https://community.unix.com/user_avatar/community.unix.com/palsevlohit_123/32/1053_2.png) [@palsevlohit\_123](https://community.unix.com/u/palsevlohit_123)
#### Post date: [October 23, 2008, 6:20am UTC](https://community.unix.com/t/split-a-file-into-multiple-files/217639/7 "2008-10-23T06:20:30Z")

</div>

Hay the below example will do the following

1. will split the file into pieces of 5 lines per file(i specified 6, it will take 6-1=5)
2. -k option will Leaves previously created files intact, if any error occurs.
3. -f option for provide the prefix of the file
4. line\_above\_it -\> is my source file.

```nohighlight
csplit -k -f ABC line_above_it 6 {10}

```

---

<div class="post-metadata">

### Author: ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/5_5575768a8748004e209b776fc1b2916d.png) [@bakunin](https://community.unix.com/u/bakunin)
#### Post date: [October 23, 2008, 7:45am UTC](https://community.unix.com/t/split-a-file-into-multiple-files/217639/8 "2008-10-23T07:45:42Z")

</div>

Why don't you simply rename the files afterwards? You know the files name already ("some\_name"aa, "some\_name"ab, etc.). "ls" sorts them alphabetically already, yes? Now set up a counter, cycle through the filenames sorted this way and rename them using the filename template and the counter:

```nohighlight
(( iCnt = 1 ))
ls | while read filename ; do
     mv $filename file${iCnt}.txt
     (( iCnt += 1 ))
done

```

I hope this helps.

bakunin

---

<div class="post-metadata">

### Author: ![kumar66](https://community.unix.com/letter_avatar/kumar66/32/5_5575768a8748004e209b776fc1b2916d.png) [@kumar66](https://community.unix.com/u/kumar66)
#### Post date: [October 23, 2008, 4:25pm UTC](https://community.unix.com/t/split-a-file-into-multiple-files/217639/9 "2008-10-23T16:25:32Z")

</div>

Hi All,

Thanks a ton.

#!/bin/ksh  
split -l 100 FilePO1.txt 'FilePO1.txt'  
j=0  
for i in `ls -1 File.txt??`;  
do j=`expr $j + 1`;  
mv $i File$j.txt;  
done

I used this script and it worked for me.

With Regards,  
Kumar66
