How to Split a Gzip File in Linux

Written by: Bobbin Zachariah   |   Last updated: August 30, 2022

How to split a gz file (database dump) into smaller files and move to another server and restore the database dump there?

In this tutorial, we learn how to split a gzip file in Linux.

Split Gzip File

You can split a larger file into smaller pieces using the “split” command.

The syntax of the split command is as follows.

split [OPTION]... [INPUT [PREFIX]]

This command will output fixed sized pieces of the input file to PREFIXaa, PREFIXab etc. You can split the file according to the size of the required split files (option –b) or according to the number of lines (-l).

For example, you can split the file into 512 MB files by using the following command.

split –b 512m “file.gz” “file.gz.part-“

This will create 512MB files named file.gz.part-aa, files file.gz.part-ab etc.

As you are trying to split a database dump, it is important that the files should not get divided in between a single line. In order to avoid such problem, you can split the file according to the number of lines.

You can make use of zcat or “gunzip -c” to output the lines from the zip file without unzipping the actual file and then pipe the output to split command as follows.

zcat file.gz | split -l 2000000 - file.part
or
gunzip –c file.gz | split -l 2000000 - file.part
or
gzip -c file | split -b 1024m - file.gz.part

This should create files with 2000000 lines in each with names such as file.partaa, file.partab etc.
You can then copy the split files into the other server. As this is a database dump file, there is no need to combine the files. You can separately import the split files into the database as follows.

mysql –u username –p dbname < file.partaa
mysql –u username –p dbname < file.partab

If you want to combine the split files to generate a single file, you can do it as,

cat file.part* > file.gz

Conclusion

In this short tutorial, we learned to split gzip file in Linux.

Thanks for reading, please leave your feedback and suggestions in the below comment section.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment