+5 votes
281 views
How to split a file in Linux from the terminal

in Guides by (552k points)
reopened | 281 views

1 Answer

+3 votes
Best answer

How to divide a file into Linux from the terminal easily and quickly.
How to join the divided files into a single file identical to the original.
Finally, other possibilities and alternatives.
Splitting text files based on the number of lines.

Sometimes we work with larger files of the account and we encounter problems when managing them. That is why I find it very useful to read today's tutorial, in which we show you how to divide a file in Linux from the terminal .

image

The main drawbacks of handling files that are too large, I have encountered when I try to save them in a flash drive whose file system is usually FAT32 and does not allow files larger than 4GB.

Another problem is when I want to share them and I have to save them in the cloud or on a remote server, which usually with large files becomes a bit more complex in case of network failure.

Throughout this mini guide, we will be detailing all the commands to divide a file from the terminal and also to be able to gather the files later and thus obtain the original.

How to divide a file into Linux from the terminal easily and quickly.

First we will use the split command, whose functionality is precisely to divide files . In this tutorial we will show the most elementary examples, but we can always consult the help page with the man split command for other advanced options.

For our example, let's imagine that we have a file called prueba.log that occupies 500MB and we want to divide it into five parts of 100MB each. The command we would have to execute would be:
split -b 100m prueba.log salida

This command will create five files of 100MB each, whose names will be: exit, exitb, exitc, exit and exit. We see that you add a letter at the end of the output name.

We can indicate that instead of a letter add a number with the command:
split -b -d 100m prueba.log salida

Now that we know the basic commands, let's go deeper. My first recommendation is to add a low bar to the output name at the end, so when you add the letter or number that identifies each part, it will turn out that the file names will be easier to read (for example output_a, output_b, ... or exit_1, exit_2, ...) .

Instead of specifying the size of each part, we can specify the number of parts into which we want to divide the file. For example, to divide into five parts, the command would be:
split -d -n 5 prueba.log salida_

Splitting text files based on the number of lines.

So far we have divided files regardless of what type of file it was, but when we are working with text files, it is sometimes a good idea to divide based on the number of lines .

In the following example we divide a text file into several parts, but each part will have 1000 lines . The command would be:
split -l -d 1000 prueba.log salida_

How to join the divided files into a single file identical to the original.

To re-gather the files, the command we will use will be cat . This command is designed to read files and concatenate them. To join all the parts of a file, we use the command:
cat salida_* > prueba.log

With this simple command, we will join all the parts to restore the prueba.log file that will be exactly the same as the original.

Finally, other possibilities and alternatives.

There is one last possibility, which is to use some type of compressor to create volumes, for example with 7zip or Rar. As these programs are not installed by default on most Linux systems, we will devote our attention to a different tutorial.


by (3.5m points)

Related questions

+5 votes
1 answer
+3 votes
1 answer
+5 votes
1 answer
+3 votes
1 answer
+5 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users