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 split and reorganize files in Linux system

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Linux system how to split and reorganize files, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Use csplit to split files

Csplit is one of these interesting little commands. It's always with you, and once you start using it, you can't do without it. Csplit splits a single file into multiple files. This example demonstrates the simplest use, dividing the file foo.txt into three files, with line numbers 17 and 33 as split points:

$csplit foo.txt 17 33 2591 3889 2359csplit creates three new files in the current directory and prints out the size of the new file in bytes. By default, each new file is named xx_nn:

$ls xx00 xx01 xx02 you can use the head command to view the first ten lines of each new file:

$head xx* = > xx00 xx01 xx02 what if you want to split the file into multiple files with the same number of lines? You can specify the number of lines, and then place the number of repeats in curly braces. This example repeats the split four times and dumps the rest to the last file: $csplit foo.txt 5 {4} 57 1488 249 1866 3798 you can use the asterisk wildcard to tell csplit to repeat the split as much as possible. This sounds cool, but if the file is not equally divided, it may fail: $csplit foo.txt 10 {*} 1545 2115 1848 1901 csplit:'10 destroy: line number out of range on repetition 4 1430 the default behavior is to delete the output file when the error occurs. You can use the-k option to solve this problem, and when there is an error, it will not delete the output file. Another behavior is that every time you run csplit, it will overwrite the previously created files, so you need to save them separately with a new file name. Use-prefix= prefix to set a different file prefix: $csplit-k-- prefix=mine foo.txt 5 {*} 57 1488 249 1866 993 csplit: '5numbers: line number out of range on repetition 9 437$ ls mine00 mine01 mine02 mine03 mine04 mine05 option-n can be used to change the number of digits for numbering files (default is 2 digits): $csplit-n 3-- prefix=mine foo.txt 5 {4} 57 1488 249 1866 1381 3798$ ls mine000 mine001 mine002 mine003 mine004 The "c" in mine005csplit means context. This means that you can split the file according to any matching method or clever regular expression. The following example divides the file into two parts. The first file ends at the line before the first occurrence of "fie", and the second file begins with the line containing "fie". $csplit foo.txt / fie/ splits the file each time "fie" appears: $csplit foo.txt / fie/ {*} splits the file in the first five occurrences of "fie": $csplit foo.txt / fie/ {5} is copied only if the content begins with a line containing "fie", and omits all the previous content: $csplit myfile% fie% splits the file into different sizes split similar to csplit. It divides files into specific sizes, which is great when you split large files into small multimedia files or use network transfer. The default size is 1000 lines: $split foo.mv $ls-hl 266K Aug 21 16:58 xaa 267K Aug 21 16:58 xab 315K Aug 21 16:58 xac [.] They are similar in size, but you can specify any size you want. In this example, it is 20m bytes: $split-b 20m foo.mv size unit is abbreviated to Krecinct Mrecrium Gregory T precinct Zreagy (the power of 1024) or KB,MB,GB and so on (the power of 1000). Choose your own prefix and suffix for the file name: $split-a 3-- numeric-suffixes=9-- additional-suffix=mine foo.mv SB 240K Aug 21 17:44 SB009mine 214K Aug 21 17:44 SB010mine 220K Aug 21 17:44 SB011mine-an option controls the numeric location of the number. -numeric-suffixes sets the starting value of the number. The default prefix is x, or you can set a different prefix by typing it after the file name. Merge the split files and you may want to reorganize your files at some point. The commonly used cat command is used here: the asterisk wildcard in the $cat SB0* > foo2.txt example will match all files that start with SB0, which may not get the results you want. You can use question mark wildcards to match more precisely, using a question mark for each character: $cat SB0? > foo2.txt as usual, please refer to the relevant manuals and information pages for complete command options. At this point, the tutorial on file segmentation and reorganization in the Linux system is over. If you have any questions, you can submit them to us through the comments section. The above is the Linux system-related content shared by Liangxu tutorial Network for all friends. If you want to know more about Linux, remember to follow the official account "good Linux", or scan the QR code below to follow, more practical information is waiting for you! What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Development

Wechat

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

12
Report