When you are dealing with massive log files, highly complex database dumps, or raw video data, you will often encounter files that are simply too large to handle. A 50-gigabyte text file will instantly crash most text editors and cannot be transferred via standard email or cloud storage limits. Instead of struggling with the massive file, you can use the Linux split command to perfectly slice it into smaller, more manageable pieces.
How to Split a File by Size
The primary use for the split utility is to break a file into chunks based on a maximum file size constraint. For example, if you want to transfer a massive system backup via an old file transfer protocol that limits files to exactly 1 Gigabyte each, you can tell split to enforce that limit.
Use the -b (byte size) flag followed by your desired chunk size (e.g., 1G for gigabytes, 100M for megabytes, or 500K for kilobytes).
split -b 1G massive_database_dump.sql
The command will execute silently. When it finishes, if you look at your directory, the original massive_database_dump.sql file will still be there untouched, but you will also see a sequence of new files created by the tool:
xaa
xab
xac
xad
The split command names the output chunks alphabetically by default (starting with xaa, then xab, etc.). If the original file was 3.5 Gigabytes, xaa, xab, and xac will each be exactly 1 Gigabyte, and xad will hold the remaining 500 Megabytes.
How to Split a File by Line Count
If you are processing massive server logs, splitting the file blindly by gigabytes might cut a critical error message in half directly in the middle of a sentence. To prevent this, you can instruct split to slice the file strictly by the number of lines instead.
Use the -l (lines) flag followed by the number of lines you want each chunk to contain.
split -l 100000 access_log.txt log_chunk_
This command splits the massive access_log.txt file into smaller files containing exactly 100,000 lines each. Notice that we also added an extra argument at the end (log_chunk_). This is a custom prefix. Instead of naming the files xaa and xab, the tool will intelligently name them log_chunk_aa, log_chunk_ab, etc., making your directory much easier to understand.
How to Recombine the Files
Once you have transferred the sliced chunks to the destination server, you must reassemble them to restore the original file. You do not need a special “unsplit” command to do this; you simply use the standard cat command to concatenate them back together in alphabetical order.
cat log_chunk_* > original_access_log.txt