split Command: Splitting and Joining Large Files

split Command: Splitting and Joining Large Files

What you'll be able to do

  • Split a file by size, line count, or piece count with `split`
  • Rebuild the original with `cat prefix* > restored_file`
  • Verify the restored file with `sha256sum`

Prerequisites (read these first)

What If a File Is Too Big?

Lina: Senpai, I tried to copy a 5GB log file to a USB stick and it said "file too large"...
Linny-senpai: That's a job for the split command. It cuts a big file into small pieces.
Linny-senpai: And you can join those pieces back together later, exactly as they were. Let's walk through it.

Words used here (sorted out first)

  • Split: cutting one file into several smaller files. The original stays where it is.
  • Join: putting the pieces back together in order to rebuild the original file.
  • Prefix: the text added to the front of each output file name.
  • Suffix: the characters added at the end of the name. The aa in part_aa is one.
  • Hash: a fingerprint-like string computed from a file's contents. "Checksum" means roughly the same thing.

What You'll Learn

  • How to split a large file by size, lines, or piece count with split
  • How to join the pieces back into the original file with cat
  • How to use numbered suffixes, so you get part_01 instead of xaa
  • How to verify the file is intact after splitting and joining

1. What Is the split Command?

Conclusion: split breaks one file into several smaller files; concatenating them with cat restores the original byte-for-byte.

Lina: Wait, does "splitting" damage the original file?
Linny-senpai: No. split reads the contents and writes them into new files. The original stays untouched.
Linny-senpai: And when you join the pieces in order, you get the original back without losing a single byte.
Lina: That's reassuring. When would I use it?
Linny-senpai: When you need to fit a file onto size-limited media, or transfer a huge file in chunks.
Linny-senpai: It also helps when you want to break a giant log into manageable pieces. Let's start with the simplest form.

First, create a file to practice with.

# Create a 50MB practice file (just zeros inside)
$ head -c 50M /dev/zero > bigfile.dat

/dev/zero is a special file that hands out zeros endlessly. head -c 50M takes only the first 50MB, and > writes that into bigfile.dat.

Plenty of articles write this with the dd command instead. dd takes its destination from of=.

We avoid dd here. Writing a disk name into of= overwrites that disk directly. There is no trash folderA container that organizes files. Same idea as a "folder" on Windows or macOS. and no confirmation promptA symbol (like $ or #) shown when the shell is waiting for your input., and the machine may stop booting.

With head -c, the destination can only be an ordinary file. Stay with this form while you are starting out.

$ ls -lh bigfile.dat
-rw-r--r-- 1 user user 50M Jun  5 10:00 bigfile.dat

2. How to Split by Size

Conclusion: Use split -b SIZE file prefix. -b 100M makes 100MB pieces, -b 10M makes 10MB pieces.

Lina: Let me try cutting it into 10MB pieces.
Linny-senpai: Use -b, which stands for bytes.
Linny-senpai: The part_ at the end is the prefix. It goes at the front of every output file name.
$ split -b 10M bigfile.dat part_
$ ls -lh part_*
-rw-r--r-- 1 user user 10M Jun  5 10:01 part_aa
-rw-r--r-- 1 user user 10M Jun  5 10:01 part_ab
-rw-r--r-- 1 user user 10M Jun  5 10:01 part_ac
-rw-r--r-- 1 user user 10M Jun  5 10:01 part_ad
-rw-r--r-- 1 user user 10M Jun  5 10:01 part_ae
Lina: So it goes part_aa, part_ab... with letters increasing.
Linny-senpai: Right. If you omit the prefix, you get xaa, xab, and so on.
Linny-senpai: The size units are K, M, and G.
Linny-senpai: One thing to watch: 10M means 10x1024x1024 bytes, while 10MB means 10x1000x1000 bytes. The two count differently.

Handy size guide

  • split -b 700M -> fits on one CD
  • split -b 100M -> easy cloud-upload size
  • split -b 1G -> 1GB per piece

3. How to Split by Line Count

Conclusion: For text and logs, split -l LINES file prefix splits on line boundaries so no line is ever cut in half.

Lina: What about splitting a log file every 1000 lines?
Linny-senpai: Use -l, which stands for lines.
Linny-senpai: Splitting by size can cut a line right in the middle. -l always breaks at a line boundary.
Linny-senpai: That makes it much safer for CSV files and logs.
$ split -l 1000 access.log chunk_
$ wc -l chunk_*
   1000 chunk_aa
   1000 chunk_ab
    342 chunk_ac
   2342 total

Splitting by size with -b looks only at byte counts. In a text file, that can leave a line split across two pieces.

When the meaning of each line matters, always use -l.

4. How to Split into a Fixed Number of Pieces

Conclusion: split -n COUNT file prefix divides the file into exactly that many equal pieces.

Lina: Sometimes I don't care about "10MB each" - I just want exactly 5 pieces.
Linny-senpai: Then use -n, which stands for number.
Linny-senpai: It divides the whole file into 5 equal parts, so you never have to calculate sizes yourself.
$ split -n 5 bigfile.dat group_
$ ls -lh group_*
-rw-r--r-- 1 user user 10M Jun  5 10:05 group_aa
-rw-r--r-- 1 user user 10M Jun  5 10:05 group_ab
-rw-r--r-- 1 user user 10M Jun  5 10:05 group_ac
-rw-r--r-- 1 user user 10M Jun  5 10:05 group_ad
-rw-r--r-- 1 user user 10M Jun  5 10:05 group_ae

-n 5 divides by byte count, so in a text file it cuts lines in half.

To get 5 pieces while keeping lines intact, write split -n l/5. The l stands for line.

$ split -n l/5 access.log group_

5. How to Join the Pieces Back

Conclusion: No special command is needed. cat prefix* > restored_file concatenates the pieces in order to rebuild the original.

Lina: I split it, but how do I put it back? Is there a "join" command?
Linny-senpai: Good question. There is a join command, but that one joins table columns. It is a completely different tool.
Linny-senpai: To reassemble split pieces, you use cat.
Lina: The cat command that displays files?
Linny-senpai: Yes. cat also concatenates multiple files in order.
Linny-senpai: Redirect the result with > to write it to a file, and you are done.
$ cat part_* > restored.dat
$ ls -lh restored.dat
-rw-r--r-- 1 user user 50M Jun  5 10:10 restored.dat

Watch the order. The * in cat part_* expands by comparing the names as text.

The aa, ab, ac that split produces all have the same width, so the order stays correct.

But if you name the files yourself as part_1, part_2, ... part_10, then part_10 sorts before part_2. Use the zero-padded numbering in the next section to stay safe.

6. How to Use Numbered Suffixes

Conclusion: -d gives numeric suffixes (00, 01...), -a sets the digit count, and --additional-suffix adds an extension.

Lina: I'd rather have 01, 02 than aa, ab - it's clearer.
Linny-senpai: Add -d to get numbers. It stands for digits.
Linny-senpai: Set the width with -a. And --additional-suffix even lets you add an extension like .part.
$ split -b 10M -d -a 2 --additional-suffix=.part bigfile.dat backup_
$ ls backup_*
backup_00.part  backup_01.part  backup_02.part  backup_03.part  backup_04.part

With zero-padded numbers, cat backup_*.part > restored.dat always joins in the correct order. That is because 00, 01, ... 10, 11 all have the same width.

If you expect more than 100 pieces, use -a 3 for three digits.

7. How to Verify the File Is Intact

Conclusion: Compare sha256sum hashes before and after. Matching values prove the file was restored byte-for-byte.

Lina: I'm nervous the joined file isn't really identical to the original.
Linny-senpai: That's what sha256sum is for. It is a fingerprint computed from the file's contents.
Linny-senpai: If the original and the restored file share the same fingerprint, the contents are identical.
Linny-senpai: It also catches corruption that happened during a transfer or a copy.
$ sha256sum bigfile.dat restored.dat
9f2c7b1a4e6d8035c1af52e0b73d94e6...  bigfile.dat
9f2c7b1a4e6d8035c1af52e0b73d94e6...  restored.dat

(The hash depends on the file's contents. The value above just shows the shape of the output.)

Lina: The long strings on the left match. That's a relief.
Linny-senpai: If they differed, it would be a sign that the join order was wrong. The data may also have been corrupted in transit.
Linny-senpai: In that case, redo the split.

8. Lina Gets Stuck: Your Own Numbers Break the Order

Conclusion: * expands by comparing names as text. Numbers you add yourself, without padding, break the join order.

Lina: Senpai, I renamed the pieces myself, from part_1 to part_10. After joining them the contents were broken.

Let's see what happened, using a tiny example.

$ touch part_1 part_2 part_10
$ ls part_*
part_1
part_10
part_2
Lina: part_10 comes before part_2. The size of the number is ignored.
Linny-senpai: Right. * compares the names as text, from the left. Once it compares 1 against 2, part_10 is already decided to come first.
Lina: So cat part_* > restored.dat joins them in that order?
Linny-senpai: Exactly. The contents end up out of sequence, and the restored file is broken.
Lina: I thought numbers would be clearer, and instead they were riskier. That makes sense now.
Linny-senpai: So use -d -a 2. With part_01, part_02, part_10 all the same width, comparing as text gives the right order.

The aa, ab, ac that split produces are all the same width too. As long as you do not rename them yourself, the ordering accident cannot happen.

If you are unsure, run sha256sum after joining. Matching values prove that both the order and the contents are correct.

9. Common Pitfalls and Fixes

Conclusion: Most trouble comes from join order, unit confusion, or running out of disk space. Check capacity and units before you split.

Symptom Cause Fix
Joined file is corrupted Wrong join order Use zero-padded -d and cat ...*
More/fewer pieces than expected M (1024) vs MB (1000) Use one unit consistently
No space left on device Splitting needs ~2x the space Check free space with df -h first
Text lines cut in half You used -b (bytes) Re-split with -l (lines)

Always do this, before and after a split

  • Test the join before you delete the original
  • Compare hashes with sha256sum once you have joined the pieces
  • Check free space with df -h before you start splitting

split and sha256sum are not available in the virtual terminalAn interactive program that reads the commands you type and runs them. on this site yet. Try the examples in this article on your own Linux machine or terminal.

Delete the original only after the join and the hash check both succeed. Unlike a GUI, rm does not move files to a trash folder. They are gone right away.

10. Mini Exercises: Try It Yourself

Conclusion: Three drills — splitting by line, joining, and verifying the hash — walk the full round trip by hand.

Lina: I follow the steps. I want to confirm them hands-on.
Linny-senpai: Good. Here are three tasks. Make a practice directory first and work inside it.
$ mkdir -p ~/split-practice && cd ~/split-practice
$ seq 1 25 > numbers.txt

Task 1: Split numbers.txt into pieces of 10 lines each, then check the line counts.

Show hint 1 (direction)

Split by line count, not by size. Use the option that respects line boundaries.

Show hint 2 (command name)

You need split and wc. split has an option that divides by line count.

Show answer
$ split -l 10 numbers.txt num_
$ wc -l num_*
10 num_aa
10 num_ab
 5 num_ac
25 total

25 lines split into groups of 10, so only the last piece holds 5.

Task 2: Join the pieces back into a file called joined.txt.

Show hint 1 (direction)

No special command is needed. Use the one that concatenates several files in order.

Show hint 2 (command name)

The command is cat. The symbol that writes output into a file appeared in section 5.

Show answer
$ cat num_* > joined.txt
$ wc -l joined.txt
25 joined.txt

Back to the original 25 lines.

Task 3: Confirm that numbers.txt and joined.txt are exactly the same.

Show hint 1 (direction)

The same line count does not prove the same contents. Compare the fingerprint computed from each file.

Show hint 2 (command name)

The command is sha256sum. It accepts two filenames side by side.

Show answer
$ sha256sum numbers.txt joined.txt

If the long strings on the left are identical on both lines, the contents match exactly.

When you are done practising, you can remove the whole directory. Look at the contents before deleting.

$ cd ~
$ ls ~/split-practice
$ rm -r ~/split-practice

rm -r deletes a directory together with everything inside it. Check that the pathA string that describes the location of a file or directory. really is ~/split-practice before you run it.

11. Review

Lina: Let me sum up. split cuts and cat rebuilds. No dedicated join command is involved.
Linny-senpai: Exactly. And there are three ways to cut: -b for size, -l for lines, -n for piece count.
Lina: Text and logs use -l, because it never cuts a line in half.
Linny-senpai: Perfect. And finish with sha256sum. With that habit you can delete the original with confidence.

Today's 3-Line Summary

  1. split cuts the file, and cat prefix* > restored_file rebuilds it
  2. -b for size, -l for lines, -n for piece count. Text and logs want -l
  3. After joining, compare with sha256sum, and delete the original only once they match

Summary / Next Reading

Share this article

Next steps