Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use split command to cut files in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use the split command in Linux to cut files, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The code is as follows:

[root@localhost database] # ls-lh gkdb.db

1 root root 411M Jul 23 17:20 gkdb.db

Divide the 276m file into 20m small files, and the file splitting operation is as follows:

The code is as follows:

[root@localhost database] # split-b 20m gkdb.db gkdb_pack_

Split is the destiny of today.

-B 20m refers to a maximum of 20m per volume file.

Gkdb.db requires split files.

Gkdb_pack_ refers to the beginning of the split file, and the split file name is generally gkdb_pack_aa, gkdb_pack_ab, gkdb_pack_ac, and so on.

Displays the split file name and size

The code is as follows:

[root@localhost database] # ll-lh gkdb_pack_a*

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_aa

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_ab

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_ac

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_ad

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_ae

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_af

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_ag

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_ah

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_ai

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_aj

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_ak

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_al

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_am

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_an

-rw-r--r-- 1 root root 20m Jul 27 16:09 gkdb_pack_ao

-rw-r--r-- 1 root root 20m Jul 27 16:10 gkdb_pack_ap

-rw-r--r-- 1 root root 20m Jul 27 16:10 gkdb_pack_aq

-rw-r--r-- 1 root root 20m Jul 27 16:10 gkdb_pack_ar

-rw-r--r-- 1 root root 20m Jul 27 16:10 gkdb_pack_as

-rw-r--r-- 1 root root 20m Jul 27 16:10 gkdb_pack_at

-rw-r--r-- 1 root root 11m Jul 27 16:10 gkdb_pack_au

The bottom file is the rest. 20*20+11=411MB

Merge Fil

The code is as follows:

[root@localhost database] # cat gkdb_pack_* > gkdb.tar.gz

The MD5 value before and after assembly is the same. If you don't believe it, you can use the md5sum command to check it.

If you want to split a text file, for example, there are thousands of lines, of course, the number of characters per line varies, and you want to split by the number of lines, for example, to generate a file every 100 lines, you only need the-l parameter, as follows:

The code is as follows:

[root@pps public_rw] # split-l 100 test.txt

In fact, if you don't add any parameters, it is split by default with a size of 1000 rows.

Example 1. Split by 1000 lines per file

The split command splits the file into 1000 lines of each file, and the file name is [prefix] aa, [prefix] ab, [prefix] ac, etc., the default prefix is X, and the number of lines of each file is 1000 lines. The command is as follows:

The code is as follows:

$split mylog

$wc-l *

4450 mylog

1000 xaa

1000 xab

1000 xac

1000 xad

450 xae

Example 2. Split by 20MB of each file

Split a file into multiple 20MB files with the-b option command as follows:

The code is as follows:

$split-b 20m logdata

$ls-lh | tail-n + 2

-rw- 1 sathiya sathiya 102m Jul 25 18:47 logdata

-rw- 1 sathiya sathiya 20m Jul 25 19:20 xaa

-rw- 1 sathiya sathiya 20m Jul 25 19:20 xab

-rw- 1 sathiya sathiya 20m Jul 25 19:20 xac

-rw- 1 sathiya sathiya 20m Jul 25 19:20 xad

-rw- 1 sathiya sathiya 20m Jul 25 19:20 xae

-rw- 1 sathiya sathiya 1.6m Jul 25 19:20 xaf

Example 3. Split with a prefix specified by each file 50MB

Use the-bytes option to split the file into multiple 50MB files, and-bytes is similar to the-b option, specifying the prefix in the second parameter.

The code is as follows:

$split-bytes=50M logdata mydatafile

$ls-lh

Total 204M

-rw- 1 sathiya sathiya 102m Jul 25 18:47 logdata

-rw- 1 sathiya sathiya 50m Jul 25 19:23 mydatafileaa

-rw- 1 sathiya sathiya 50m Jul 25 19:23 mydatafileab

-rw- 1 sathiya sathiya 1.6m Jul 25 19:23 mydatafileac

Example 4. Split the file based on the number of lines

Use the-l option to specify the number of lines to split the file into multiple files with the same number of lines.

The code is as follows:

$wc-l testfile

2591 testfile

$split-l 1500 testfile importantlog

$wc-l *

1500 importantlogaa

1091 importantlogab

2591 testfile

Example 5. Name the split file with a numeric suffix

Use the-d option to specify a suffix as a number, such as 00B01Phone02... instead of aa,ab,ac.

The code is as follows:

$split-d testfile

$ls

Testfile x00 x01 x02

The above is how to use the split command to cut files in Linux. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report