Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Introduction

In this short tutorial, we’ll take a look at a few different ways we can split files in Unix systems.

All of these commands were tested in Bash, but are platform-independent.

2. Using split

One of the most common ways of splitting files is to divide them into chunks by a given size. To do this we can use the split command.

Let’s say we have a 50MB text file and we want to divide it into five 10MB parts. All we do is:

split --bytes=10M data.txt dataPartPrefix

This will result in creating five files with the following names:

dataPartPrefixa
dataPartPrefixb
dataPartPrefixc
dataPartPrefixd
dataPartPrefixe

Notice that our prefix name will be followed by the following letters. Adding -d to the command will use numeric suffixes starting from 0 instead of alphabetic ones.

The –bytes argument accepts integer values or a unit (example: 10K is 10*1024). Units are K, M, G, T, P, E, Z, Y for powers of 1024 or KB, MB, and so on for powers of 1000

We can also split a file into a given number of chunks with equal size.

split --number=2 data.txt dataPartPrefix

This will create two files with 5MB size each:

dataPartPrefixa
dataPartPrefixb

If we are splitting a text file and want to split it by lines we can do:

split -l 500 data.txt dataPartPrefix

This will result in a number of files depending on origin file content length.

To put the data back together we can use cat:

cat dataPartPrefix* > newData.txt

3. Splitting Files Using 7-Zip

We can also split a file while creating a 7-Zip archive. This will result in creating multiple archive volumes.

Let’s assume we have the same 50MB text file and we want to divide it into 10MB archives. All we do is:

7z a -v10m -mx0 data.7z data.tzt

The 10m is the volume size and -mx0 tells 7-Zip to use no compression.

After running this command we’ll have following files in our directory:

data.txt
data.7z.001
data.7z.002
data.7z.003
data.7z.004
data.7z.005

To get back the origin file we should simply extract the first volume:

7z x data.7z.001

7-Zip will automatically start extracting the remaining files in order.

4. Conclusion

In this tutorial, we’ve learned how to split files in Unix systems, having a look at both the split and 7-Zip commands.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!