How to Split a Large File into Smaller Pieces Using the split Command in Linux

When working in a Linux environment, you will eventually encounter a file that is simply too massive to manage. You might have a 50GB database dump that you need to transfer to another server, but your network connection keeps dropping halfway through. Or perhaps you need to upload a massive log file to a support ticket system that strictly enforces a 2GB file size limit.

Rather than struggling with incomplete transfers or rejected uploads, you can use the native Linux split command. This utility quickly cuts a massive file into smaller, perfectly sized chunks, which you can easily transfer and then seamlessly reassemble at their final destination.

Splitting a File by Size

The most common use case for the split command is breaking a file into chunks based on a specific byte size. This ensures no individual chunk exceeds your target limit.

Assume you have a massive file named database_backup.sql, and you want to split it into chunks that are exactly 1GB each. Open your terminal and use the -b (bytes) flag:

split -b 1G database_backup.sql

The split command will run silently. When it finishes, you can run the ls command to view your directory. You will see your original file is completely intact, but next to it, you will find several new files named:

  • xaa
  • xab
  • xac

By default, the split command generates new files using the prefix “x” followed by a two-letter alphabetical progression. If your original file was 2.5GB, xaa and xab will be exactly 1GB, and xac will contain the remaining 500MB.

You can use different suffixes for the size flag, such as K for Kilobytes, M for Megabytes, and G for Gigabytes (e.g., -b 500M).

Assigning Custom Prefixes to the Chunks

Having chunks named “xaa” and “xab” is confusing, especially if you are splitting multiple files in the same directory. You should assign a custom prefix to keep things organized. You simply append the desired prefix to the end of the command.

split -b 1G database_backup.sql db_chunk_

Now, when you run ls, the output will look much cleaner:

  • db_chunk_aa
  • db_chunk_ab
  • db_chunk_ac

Splitting a File by Line Count

If you are dealing with massive CSV files or raw text server logs, splitting by physical file size might break a single line of data in half, corrupting the specific record at the seam. For structured text, it is safer to split the file by the number of lines.

Use the -l (lines) flag to specify exactly how many lines each chunk should contain. To split a massive log file into smaller files containing exactly 10,000 lines each, type:

split -l 10000 server_logs.txt log_part_

How to Reassemble the File

Once you have successfully transferred the smaller chunks (e.g., db_chunk_aa, db_chunk_ab) to their final destination, you must glue them back together. You do not need a special “unsplit” command; you simply use the standard cat (concatenate) command to read them all and redirect the output into a new file.

Navigate to the directory containing all the chunks, and run:

cat db_chunk_* > rebuilt_database_backup.sql

The wildcard asterisk (*) tells the cat command to grab every file that starts with “db_chunk_”. Because Linux reads wildcards in alphabetical order, it naturally processes “aa”, then “ab”, then “ac”, ensuring the file is stitched back together in the exact, perfect sequence.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.