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?
split command. It cuts a big file into small pieces.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
aainpart_aais 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_01instead ofxaa - 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.
split reads the contents and writes them into new files. The original stays untouched.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 100Mmakes 100MB pieces,-b 10Mmakes 10MB pieces.
-b, which stands for bytes.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
part_aa, part_ab... with letters increasing.xaa, xab, and so on.K, M, and G.10M means 10x1024x1024 bytes, while 10MB means 10x1000x1000 bytes. The two count differently.Handy size guide
split -b 700M-> fits on one CDsplit -b 100M-> easy cloud-upload sizesplit -b 1G-> 1GB per piece
3. How to Split by Line Count
Conclusion: For text and logs,
split -l LINES file prefixsplits on line boundaries so no line is ever cut in half.
-l, which stands for lines.-l always breaks at a line boundary.$ 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 prefixdivides the file into exactly that many equal pieces.
-n, which stands for number.$ 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_fileconcatenates the pieces in order to rebuild the original.
join command, but that one joins table columns. It is a completely different tool.split pieces, you use cat.cat command that displays files?cat also concatenates multiple files in order.> 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:
-dgives numeric suffixes (00,01...),-asets the digit count, and--additional-suffixadds an extension.
01, 02 than aa, ab - it's clearer.-d to get numbers. It stands for digits.-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
sha256sumhashes before and after. Matching values prove the file was restored byte-for-byte.
sha256sum is for. It is a fingerprint computed from the file's contents.$ 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.)
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.
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
part_10 comes before part_2. The size of the number is ignored.* compares the names as text, from the left. Once it compares 1 against 2, part_10 is already decided to come first.cat part_* > restored.dat joins them in that order?-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
sha256sumonce you have joined the pieces - Check free space with
df -hbefore 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.
$ 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
split cuts and cat rebuilds. No dedicated join command is involved.-b for size, -l for lines, -n for piece count.-l, because it never cuts a line in half.sha256sum. With that habit you can delete the original with confidence.Today's 3-Line Summary
splitcuts the file, andcat prefix* > restored_filerebuilds it-bfor size,-lfor lines,-nfor piece count. Text and logs want-l- After joining, compare with
sha256sum, and delete the original only once they match