Detailed explanation of using split command to split Linux files

Detailed explanation of using split command to split Linux files

A few simple Linux commands let you split and reassemble files as needed to fit within storage or email attachment size limitations.

The Linux system provides a very easy-to-use command to split files. You may want to do this before uploading a file to a storage site that limits the size of the file or before attaching it to an email. To split a file into multiple file chunks, just use the split command.

$ split bigfile

By default, the split command uses a very simple naming scheme. The file chunks will be named xaa, xab, xac, etc., and, presumably, if you split a large enough file, you might even get chunks named xza and xzz.

This command will run without any feedback unless you ask it to. However, if you want to see feedback as the file chunks are created, use the --verbose option.

$ split –-verbose bigfile
creating file 'xaa'
creating file 'xab'
creating file 'xac'

You can also give the files a name prefix. For example, to split your original file and name it bigfile.aa, bigfile.ab, etc., you can add the prefix to the end of the split command, like this:

$ split –-verbose bigfile bigfile.
creating file 'bigfile.aa'
creating file 'bigfile.ab'
creating file 'bigfile.ac'

Note that a dot is added to the end of the prefix shown in the above command. Otherwise, the file will be named something like bigfileaa instead of bigfile.aa.

Please note that the split command does not delete your original file, it just creates chunks of it. If you want to specify the file block size, you can add it to the command using the -b option. For example:

$ split -b100M bigfile

The file size can be KB, MB, GB, and the maximum can be YB! Just use the appropriate letters K, M, G, T, P, E, Z, and Y.

If you want to split the file based on the number of lines in each block rather than the number of bytes, you can use the -l (lines) option. In this example, each file will have 1000 lines, although the last file may have fewer lines.

$ split --verbose -l1000 logfile log.
creating file 'log.aa'
creating file 'log.ab'
creating file 'log.ac'
creating file 'log.ad'
creating file 'log.ae'
creating file 'log.af'
creating file 'log.ag'
creating file 'log.ah'
creating file 'log.ai'
creating file 'log.aj'

If you need to reassemble the files on the remote site, you can easily do this using the cat command as follows:

$ cat x?? > original.file
$ cat log.?? > original.file

The split and combine commands shown above are suitable for both binary and text files. In this example, we split the zip binary file into 50KB chunks, then reassembled them using cat, and then compared the reassembled file with the original file. The diff command verifies that files are identical.

$ split --verbose -b50K zip zip.
creating file 'zip.aa'
creating file 'zip.ab'
creating file 'zip.ac'
creating file 'zip.ad'
creating file 'zip.ae'
$ cat zip.a? > zip.new
$ diff zip zip.new
$ <== no output = no difference

The only thing I would caution is that if you use split a lot and use the default naming, some chunks may overwrite other chunks, even more than you expected, because some were split earlier.

Summarize

The above is the editor's introduction to using the split command to split Linux files. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • How to split files using csplit command in Linux
  • Detailed explanation of splitting large files and merging files with cat in Linux
  • Detailed explanation of Linux split command
  • Summary of the usage of split function in awk in Linux

<<:  How to find out uncommitted transaction information in MySQL

>>:  The complete code of the uniapp packaged applet radar chart component

Recommend

N ways to align the last row of lists in CSS flex layout to the left (summary)

I would like to quote an article by Zhang Xinxu a...

A brief discussion on the issue of element dragging and sorting in table

Recently, when using element table, I often encou...

Theory: The two years of user experience

<br />It has been no more than two years sin...

Summary of the three stages of visual designer growth

Many people have read this book: "Grow as a ...

Solution to 1449 and 1045 exceptions when connecting to MySQL

Solution to 1449 and 1045 exceptions when connect...

js memory leak scenarios, how to monitor and analyze them in detail

Table of contents Preface What situations can cau...

The difference between the four file extensions .html, .htm, .shtml and .shtm

Many friends who have just started to make web pag...

MySQL 20 high-performance architecture design principles (worth collecting)

Open Source Database Architecture Design Principl...

A Deeper Look at the Differences Between Link and @import

There are three main ways to use CSS in a page: ad...

Review of the best web design works in 2012 [Part 1]

At the beginning of the new year, I would like to...

In-depth explanation of Session and Cookie in Tomcat

Preface HTTP is a stateless communication protoco...

Sample code for achieving small triangle border effect with pure CSS3+DIV

The specific code is as follows: The html code is...

Advantages and disadvantages of conditional comments in IE

IE's conditional comments are a proprietary (...

A brief discussion on value transfer between Vue components (including Vuex)

Table of contents From father to son: Son to Fath...