The “split” Command in Linux [6 Practical Examples]

The split command in Linux is useful when dealing with a large file or when you want to divide your document into different parts. In this article, I will discuss the “split” command in detail, including its syntax, options, and examples of how it can be used in real-world scenarios.

A. Description

The “split” command in Linux is a useful utility for dividing large files into smaller, more manageable pieces. Whether you need to break up a large text file for processing or transferring, the “split” command provides an efficient solution.

Note: By default, the split command creates file names starting with ‘x’, but you can modify it using various options.

B. Syntax

The syntax of the split command in Linux is simple, as shown below.

split [OPTION]... FILE [PREFIX]

Note: In the syntax above, the OPTION & PREFIX enclosed in square brackets mean they are not mandatory, and three dots after the square brackets mean multiple OPTIONS can be used after the split command.

C. Options

The split command has multiple options available. Here, I have listed a few of them. However, you can learn more about the split command, its options, and their uses by checking the man page.

man split

Useful Options

  • -a, –suffix-length=N: Generates suffixes of length N (default 2).
  • –additional-suffix=SUFFIX: Adds an additional SUFFIX to file names.
  • -b, –bytes=SIZE: You can use it to specify the size of each output file in bytes.
  • -C, –line-bytes=SIZE: Determines the maximum size per output file.
  • -d: Changes the default alphabetic suffixes to numeric suffixes starting at 0.
  • –numeric-suffixes[=FROM]: Same as -d, but allows setting the start value.
  • -x: Generates hexadecimal suffixes starting from 0.
  • –hex-suffixes[=FROM]:It works in the same way as ‘-x’, but allows setting the start value.
  • -e, –elide-empty-files: Prevents the generation of empty output files with the -n option.
  • -l, –lines=NUMBER: Splits a document into specific line numbers.
  • -n, –number=CHUNKS:Generates CHUNKS output files.
  • –verbose: Prints each execution detail in the terminal.
  • –help: Displays this help.
  • –version: Outputs version information.

Note: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “split” Command in Linux

In Linux, the split command is a useful tool for breaking up large files into smaller, more manageable parts. In the section below, I will show you some of the most useful applications for the split command in Linux.

Example 1: Split The Files into a Specific Number Of Files Using the “split” Command in Linux

The split command in Linux allows you to split a large file into multiple parts or files simply using the option -n. I have a file named “hamlet” in my home directory Here, I will split the document into 15 files using the split command. You can do the same with any of your documents by following the steps below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 After that execute the command, which will show you the contents of your current directory.

ls

➌ Tap the ENTER key again.

➍ Then, run the following command to split the desired document.

split hamlet -n 15

➎ After that, press the ENTER key.

➏ Now, run the command to check the newly created file.

ls

➐ Finally, Press ENTER again.

Output >

In the following image, you can see that new 15 files are created after splitting the file named “hamlet”.Showing that new 15 files are created after splitting the file named “hamlet” using split command in linux.


Similar Readings


Example 2: Split Files into Multiple Files With Specific Line Numbers

You can split a document into multiple files by specifying the maximum line numbers for each new file. To do this you have to use the -l option with the split command. Now, I will split the “hamlet” file into multiple files by specifying the maximum line number of “3000” for each new file. You can do the same by following the steps below.

Steps to Follow >

➊ First, open the Ubuntu Terminal.

 Next, run the command to see the total line number of the “hamlet” file.

wc -l hamlet

➌ Then, press the ENTER button.

➍ Next, execute the command, which will split the main file into multiple files having a maximum of 3000 lines.

split -l 3000 hamlet

➎ After that, press the ENTER key

➏ Now, run the command to check the new files that are created after splitting the main file.

ls

➐ Then, press the ENTER key.
➑ To check the number of lines of each newly created file by running the command below.

wc -l xa*

➒ Finally, tap the ENTER button.

Output >

In the image below, I illustrated the process of splitting a large file based on the line numbers.The process of splitting a large file based on the line numbers has been illustrated using split command in linux.

Example 3: Split Files With Custom Name Prefix Using the “split” Command in Linux

The split command allows you to specify the prefix of the name of the newly created files. Here, I will split the file named “hamlet” and name the new files with the prefix ‘part_’. You can do the same by following the steps below.

Steps to Follow >

➊ Open the Ubuntu Terminal.

 Execute the command to see the contents of the current directory.

ls

➌ Then, press the ENTER key.

➍ Now, run the following command to split the file with a custom name prefix. In this case, I have used “part_” as a prefix.

split hamlet part_

➎ Next hit the ENTER button.

➏ To see the current content status in the directory, run the command given below.

ls

➐ Finally, tap the ENTER button.

Output >

In the following image, you can see that I have split the file named “hamlet” into multiple parts with the prefix of the name “part_”.The file named “hamlet” has been split into multiple parts with the prefix “part_" using split command in Linux.


Similar Readings


Example 4: Split a Large File into Parts by Naming them in Numerically Ordered Suffixes of Specified Suffix Length

You can specify the suffix length with the option -a and can change the default alphabetic suffixes to numeric suffixes using option -d. Here, I will set the name of the newly generated file as part_00000, part_00001, part_00002, and so on by executing the following steps. You may follow them to do the same.

Steps to Follow >

➊ Open the Ubuntu Terminal.

➋ Next, execute the command.

ls

➌ Press the ENTER button.

➍ Now, Run the command below to split the file with the desired suffix.

split hamlet part_ -a 5 -d

➎ After that, hit the ENTER key again.

➏ Now, run the below command.

ls

➐ Finally, press the ENTER key.

Output >

The below image shows that I have split the hamlet file and named them in “part_0000*” format with a suffix length of “5”.The hamlet file has been split into parts and named them in “part_0000*” format with a suffix length of “5”.

Example 5: Split Files into Multiple Files of Specific Size Using the “split” Command in Linux

The split command allows you to specify the file size that will be generated after executing the split command. To specify the file size, you can use the -b option. Here, I will split the file named hamlet into multiple files with a file size of 528 KB.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Next, execute the command below.

ls

➌ Tap the ENTER key again.

➍ Then, run the command to split the hamlet file into multiple parts.

split helmet part- -d -b 528KB

➎ After that, press the ENTER key.

➏ Now, run the command to check the file size of the newly created files.

ls -l pa*

➐ Finally, press ENTER again.

Output >

In the following image, you can see that I have split the file named hamlet into multiple files of size equal to or less than 528 KB.Showing that I have split the file named hamlet into multiple files of size 528 KB.


Similar Readings


Example 6: Omit Files With Zero Size Using the “split” Command in Linux

To avoid zero-sized output files during file splitting, use the -e option when using the split command. For example, to split the “hamlet” file into 15 files without generating any zero-sized files you can follow the steps mentioned below.

Steps to Follow >

➊Open the Ubuntu Terminal.

 Then, execute the command below.

ls

➌ Now, tap the ENTER button.

➍ Next, execute the following command.

split -n 15 -e -d hamlet

➎ Then, press the ENTER key.

➏ Type the following command to see the file sizes.

ls -l

➐ Press ENTER again.

Output >

In the following image, you can see that there is no file with zero sizes generated after executing the split command with the -e option.Showing that there is no file with zero sizes has generated after executing the split command with the -e option.

Conclusion

The split command is a powerful tool for managing large files in Linux. Whether you are dealing with large text files or need to break up a file for easier processing or transfer, the split command is a versatile and efficient solution. Through the practical examples covered in this article, I hope that you will have a better understanding of how the “split” command works and how you can utilize it in your own projects.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

Leave a Comment