• Earn real money by being active: Hello Guest, earn real money by simply being active on the forum — post quality content, get reactions, and help the community. Once you reach the minimum credit amount, you’ll be able to withdraw your balance directly. Learn how it works.

Linux How to split a file into parts?

dEEpEst

☣☣ In The Depths ☣☣
Staff member
Administrator
Super Moderator
Hacker
Specter
Crawler
Shadow
Joined
Mar 29, 2018
Messages
13,860
Solutions
4
Reputation
27
Reaction score
45,546
Points
1,813
Credits
55,090
‎7 Years of Service‎
 
56%
How to split a file into parts?

In some cases, one file needs to be split into several parts, for example, for downloading or copying if the file is too large. The easiest way to do this is with the split utility.

The command is used to split files by size, number of lines and into a certain number of components.

Break down by size

To do this, we use the " -b " option, which defines the maximum element size. We will take a 721-byte file as a basis. It needs to be split into 100-byte parts. For convenience, we will set the name prefix " file-part_ ":


Bash:
$ ls -l
721 Apr 20 13:11 file_orig

Bash:
$ split -b 100 ./file_orig ./file-part_

In addition to the original, 8 more files will appear:



Bash:
$ ls -l
721 Apr 20 13:11 file_orig
100 Apr 20 13:12 file-part_aa
...
21 Apr 20 13:12 file-part_ah


Split by number of lines

It may be necessary to split one text document into several, with the number of lines " <= n ". In this case, the option " -l " will be useful. The command will split a large log file into parts with 10 thousand lines in each:


Bash:
$ split -l 1000 ~/Logs/log ~/Logs/log-part_


Split into a certain number of files

Another interesting task, in the solution of which the option " -n " will help. The size will be, if possible, divided equally. It is enough to write the final number of files and run the command:


Bash:
$ split -n 3 ./file_orig ./file-part_


Bash:
$ ls -l
240 Apr 20 13:31 file-part_aa
240 Apr 20 13:31 file-part_ab
241 Apr 20 13:31 file-part_ac
721 Apr 20 13:11 file_orig


Setting up file part names

As we already understood, for split the prefix defines the name of the file part, after which comes the default suffix of two Latin letters.

With the help of options you can change its length " -a ", switch to numbers " -d " or hex-symbols " -x ". In the last two scenarios you can choose the starting point of the report " --numeric-suffixes " for numbers and "- -hex-suffixes " for hex-symbols.

Let's take the task of splitting a text file into 3 equal-sized parts, each of which has a prefix " file-part_ " and a numeric suffix of one character, starting with 1:


Bash:
$ split -a 1 --numeric-suffixes=1 -n 3 ./file_orig ./file-part_



Bash:
$ ls -l
240 Apr 20 13:36 file-part_1
240 Apr 20 13:36 file-part_2
241 Apr 20 13:36 file-part_3


Merging parts of a file

The next step is to combine several parts into a single file. The cat utility is perfect for this purpose. First, you need to specify the names of the parts, then the resulting file:


Bash:
$ cat ~/Archive/split-archive.part_* > ~/Archives/cat-archive.tar.gz
 
Back
Top