Splitting and Concatenating Files on Linux Made Easy with the split Command

📆 · ⏳ 3 min read · ·

Introduction

File splitting and concatenation are common tasks that Linux users often encounter. These operations are useful for a variety of reasons, such as distributing large files over email, uploading them to cloud storage platforms, or transferring them over a slow or unstable network connection.

The split command is a built-in utility in Linux that enables you to split files into smaller parts or concatenate multiple files into a single file. In this blog post, we will explore how to use the split command for file splitting and concatenation, and provide practical examples to help you get started.

Using the split Command

File Splitting

The split command is simple and straightforward to use. To split a file into smaller parts, you need to specify the input file name, the output file prefix, and the maximum size of each part.

For example, let’s say we have a large file named ‘bigfile.txt’ that we want to split into parts that are 1 MB in size.

Here’s how we can use the split command to achieve that:

Terminal window
split -b 1M bigfile.txt bigfile-part-

In the above command, -b is used to specify the maximum size of each part, which is 1 MB in this case.

bigfile.txt is the input file, and bigfile-part- is the prefix that will be used for the output files.

The split command will create multiple output files, each with the prefix bigfile-part- followed by a two-letter suffix. For example, the first file will be named bigfile-part-aa, the second file will be named bigfile-part-ab, and so on.

File Concatenation

To concatenate multiple files into a single file, you can use the cat command along with the file names in the desired order. For example, let’s say we have three files named ‘file1.txt’, ‘file2.txt’, and ‘file3.txt’ that we want to concatenate into a single file named ‘allfiles.txt’.

Here’s how we can use the cat command to achieve that:

Terminal window
cat file1.txt file2.txt file3.txt > allfiles.txt

In the above command, ‘file1.txt’, ‘file2.txt’, and ‘file3.txt’ are the input files that will be concatenated into a single file. The > operator is used to redirect the output to a new file named ‘allfiles.txt’.

The order of the input files determines the order of the contents in the output file.

Conclusion

The split command is a handy utility in Linux that enables you to split large files into smaller, more manageable parts or concatenate multiple smaller files into a single, larger file.

In this blog post, we’ve explored how to use the split command for file splitting and concatenation, and provided practical examples to help you get started.

With this knowledge, you can now handle large files with ease and efficiency on your Linux system.

You may also like

  • Mount a drive permanently with fstab in Linux

    Let's see how to mount a drive permanently in Linux using the fstab file which will mount the drive automatically on boot.

  • Setup Jellyfin with Hardware Acceleration on Orange Pi 5 (Rockchip RK3558)

    Recently I moved my Jellyfin to an Orange Pi 5 Plus server. The Orange Pi 5 has a Rockchip RK3558 SoC with integrated ARM Mali-G610. This guide will show you how to set up Jellyfin with hardware acceleration on the Orange Pi 5.

  • HTTPS with self-signed certificates for your Homelab services

    In this article we will deep dive into understanding how we can setup HTTPS with self-signed certificates for our Homelab services.This is often required when you are running your own services and you want to access them over HTTPS.