Splitting and Re-Assembling Files in Linux

Linux has several utilities for splitting up files. So why would you want to split your files? One use case is to split a large file into smaller sizes so that it fits on smaller media, like USB sticks. This is also a good trick to transfer files via USB sticks when you’re stuck with FAT32, which has a maximum file size of 4GB, and your files are bigger than that. Another use case is to speed up network file transfers, because parallel transfers of small files are usually faster.

We’ll learn how to use csplit, split, and cat to chop up and then put files back together. These work on any file type: text, image, audio, .iso, you name it.

Split Files With csplit

csplit is one of those funny little commands that has been around forever, and when you discover it you wonder how you ever made it through life without it. csplit divides single files into multiple files. This example demonstrates its simplest invocation, which divides the file foo.txt into three files, split at line numbers 17 and 33:

$ csplit foo.txt 17 33259138892359

csplit creates three new files in the current directory, and prints the sizes of your new files in bytes. By default, each new file is named xxnn:

$ lsxx00xx01xx02

You can view the first ten lines of each of your new files all at once with the head command:

$ head xx*==> xx00 <==Foo Fileby Carla SchroderFoo textFoo subheadingMore foo text==> xx01 <==Foo textFoo subheadingMore foo text==> xx02 <==Foo textFoo subheadingMore foo text

What if you want to split a file into several files all containing the same number of lines? Specify the number of lines, and then enclose the number of repetitions in curly braces. This example repeats the split 4 times, and dumps the leftover in the last file:

$ csplit foo.txt 5 {4}57148824918663798

You may use the asterisk wildcard to tell csplit to repeat your split as many times as possible. Which sounds cool, but it fails if the file does not divide evenly:

$ csplit foo.txt 10 {*}1545211518481901csplit: '10': line number out of range on repetition 41430

The default behavior is to delete the output files on errors. You can foil this with the -k option, which will not remove the output files when there are errors. Another gotcha is every time you run csplit it overwrites the previous files it created, so give your splits new filenames to save them. Use --prefix=prefix to set a different file prefix:

$ csplit -k --prefix=mine foo.txt 5 {*} 5714882491866993csplit: '5': line number out of range on repetition 9437$ lsmine00mine01mine02mine03 mine04mine05

The -n option changes the number of digits used to number your files:

 $ csplit -n 3 --prefix=mine foo.txt 5 {4}571488249186613813798$ lsmine000mine001mine002mine003mine004mine005

The “c” in csplit is “context”. This means you can split your files based on all manner of arbitrary matches and clever regular expressions. This example splits the file into two parts. The first file ends at the line that precedes the line containing the first occurrence of “fie”, and the second file starts with the line that includes “fie”.

$ csplit foo.txt /fie/ 

Split the file at every occurrence of “fie”:

$ csplit foo.txt /fie/ {*}

Split the file at the first 5 occurrences of “fie”:

$ csplit foo.txt /fie/ {5}

Copy only the content that starts with the line that includes “fie”, and omit everything that comes before it:

$ csplit myfile %fie% 

Splitting Files into Sizes

split is similar to csplit. It splits files into specific sizes, which is fabulous when you’re splitting large files to copy to small media, or for network transfers. The default size is 1000 lines:

$ split foo.mv$ ls -hl266K Aug 21 16:58 xaa267K Aug 21 16:58 xab315K Aug 21 16:58 xac[...]

They come out to a similar size, but you can specify any size you want. This example is 20 megabytes:

$ split -b 20M foo.mv

The size abbreviations are K, M, G, T, P, E, Z, Y (powers of 1024), or KB, MB, GB, and so on for powers of 1000.

Choose your own prefix and suffix for the filenames:

$ split -a 3 --numeric-suffixes=9 --additional-suffix=mine foo.mv SB240K Aug 21 17:44 SB009mine214K Aug 21 17:44 SB010mine220K Aug 21 17:44 SB011mine

The -a controls how many numeric digits there are. --numeric-suffixes sets the starting point for numbering. The default prefix is x, and you can set a different prefix by typing it after the filename.

Putting Split Files Together

You probably want to reassemble your files at some point. Good old cat takes care of this:

$ cat SB0* > foo2.txt

The asterisk wildcard in the example will snag any file that starts with SB0, which may not give the results you want. You can make a more exact match with question mark wildcards, using one per character:

$ cat SB0?????? > foo2.txt

As always, consult the relevant and man and info pages for complete command options.

Learn more about Linux through the free “Introduction to Linux” course from The Linux Foundation and edX.

Leave a Reply

Your email address will not be published.