How To Use The Linux Split Command To Manage Large Files

Linux Split

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

Managing Large files is very exhausting; for that, you must use the Linux Split command, which is a very powerful tool and can break those big files into smaller pieces.

This command is useful for managing large log and archive files that are hard to handle all at once. With the split command, you can divide files by the number of lines or file size, customize the output file names, and more.

The split command in Linux takes the specified file and writes it in pieces of 1000 lines to a series of output files. The first output file is named by combining the specified prefix (default is x) with the aa suffix, the second file uses the ab suffix, and this continues lexicographically up to zz (allowing for a maximum of 676 files). You can increase the number of letters in the suffix, and thus the number of output files, by using the -a flag.

Remember, you cannot set a prefix longer than PATH_MAX – 2 bytes (or PATH_MAX – SuffixLength bytes if the -a flag is used).

The PATH_MAX variable defines the maximum length of a path name for the system as specified in the /usr/include/sys/limits.h file.

If you do not provide an input file or use a file name of – (minus sign), the split command will read from standard input. The Linux split works with any regular text or binary files. Once a file is split, it can be restored to its original state using the cat command, and the file pieces will be listed in the correct order.

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

Basic Syntax of the Linux Split Command

split [options] name_of_file prefix_for_new_files

Linux split Command Options

The command supports many options. The most common Linux split command options are:

OptionDescription
-aSet suffix length.
-bDetermines size per output file.
-CDetermines the maximum size per output file.
-dChanges default suffixes to numeric values.
-eOmits creating empty output files.
-lCreates files with a specific number of output lines.
-nGenerates a specific number of output files.
--verboseDisplays a detailed output.

Examples

Splitting Files by Number of Lines

split book

This command divides the book into segments of 1000 lines, labeled xaa, xab, xac, and so on.

To divide a file into segments of 50 lines and set a prefix for the file names, Type:

split -l 50 book sect

This command divides the book into segments of 50 lines, labeled sectaa, sectab, sectac, and so on.

To divide a file into segments of 2KB, Type:

split -b 2k book

This command divides the book into segments of 2*1024 bytes, labeled xaa, xab, xac, and so on.

To divide a file into more than 676 segments, Type:

split -l 5 -a 3 book sect

This command divides the book into segments of 5 lines, labeled sectaaa, sectaab, sectaac, and so on, up to sectzzz (with a maximum of 17,576 files).

Display Only a Specific Output File

The split command typically generates multiple files to encompass the whole source file. However, when using -n with split, it divides a file but only shows the designated part(s). This flag does not produce output files; instead, it outputs directly to the terminal.

For example, to split tiny_text into 100 sections but only show the first one, use:

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

split -n 1/100 tiny_text

Custom Output File Names

The split command generates files with a default two-letter suffix. You can modify the length by using the -a flag with split. For example, to set the suffix to 3 characters, enter:

split -a 3 large_text

Split Change Suffix

Utilize split to generate files with various suffixes. For example, you can split large_text into files of 2500 lines each with numeric suffixes:

split -l2500 -d large_text

The result displays six files with numbered suffixes created using the -d flag. The -l2500 flag divides the large_text file into six files, each containing 2500 lines.

Change Prefix

The split command can also produce output files with customizable prefixes. The command syntax is:

split [file] [prefix]

For example, you can split large_text into ten files named part00 to part09 using:

split -d large_text part -n 10

split change suffix terminal output

The prefix changes from x to part and concludes with numbers because of the -d flag. The -n flag divides the file into ten sections.

Split file size using ‘-b’ option.

split -b 16 index.txt index

Here, it will split the file

<em>index.txt</em>

into separate files called indexaa, indexab,…, with each file containing 16 bytes of data.

Quick Recap And Why Split is Still Relevant

Learning the Linux split command lets you manage large files more effectively. You can split files by the number of lines or by file size, customize the names of the output files, or divide files into smaller parts. The split command gives you the flexibility and control you require.

FAQ’s

1. What is the purpose of the Linux split command?

The split command in Linux is designed to divide a large file into smaller, easier-to-handle sections. This is particularly helpful for managing log files, backups, or any data that is too large to transfer or open all at once.

2. How can I split a file by size in Linux?

A Linux split file based on size, you can use the command:

split -b 50M largefile.txt part_

This command will break largefile.txt into 50 MB segments, named part_aa, part_ab, and so forth.

Areeba Nauman
Areeba is a Content Writer with expertise in web content and social media, she can simplify complex concepts to engage diverse audiences. Fueled by creativity and driven by results, she brings a unique perspective and a keen attention to detail to every project she undertakes with her creativity and passion for delivering impactful content strategies for success. Let's connect on Linkedin: https://www.linkedin.com/in/areeba-bhatti/
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!