Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to split large log files under Linux

Shulou Source: shulou.com Published: 2022-06-01 06:28:18 09月30日 Update

This article mainly introduces how to split large log files under Linux, which is very detailed and has a certain reference value. Interested friends must read it!

How to split

Split orders to play.

Split by number of rows

Suppose the file test.txt has multiple lines, and every 2 lines are split into a file, and you can use the-l parameter:

$split-l 2 test.txt $ls-al-rw-r--r-- 1 root root 32 Aug 31 16:55 test.txt-rw-r--r-- 1 root root 20 Aug 31 16:55 xaa-rw-r--r-- 1 root root 12 Aug 31 16:55 xab

If you take a closer look, it is difficult to describe the file name after the split.

Could you name it properly?

Canonical naming

Use the-d parameter to use a number as a new file suffix:

$split-l 2 test.txt-d $ls test.txt x00 x01 x02

You can also specify the length of the suffix number with-a (the default is 2):

$split-l 2 test.txt-d-a 4$ ls test.txt x0000 x0001 x0002

However, the first x is out of order. We can also specify a prefix, such as test:

$split-l 2 test.txt-d-a 4 test $ls test0000 test0001 test0002 test.txt

And you can also start with a specified numeric subscript and use-- numeric-suffixes, for example, from 0004:

$split-- numeric-suffixes=4 test.txt-a 4-l 2 test.txt x0004 x0005

Split by size

Use the-b parameter to split by size, in bytes:

$split-b 10 test.txt

Of course, k (kilobytes) and m (megabytes) can also be used. That is:

$split-b 10k test.txt

Split into a specified number of files

You can use the-n parameter, such as split into 3 files regardless of size and number of lines:

$split-n 4 test.txt $test.txt xaa xab xac xad

Other methods

Of course, typed log files can be opened instead of creating temporary files (for example, vim opens a file to create a temporary file). Instead, you can use commands such as more,tail to view some of the contents.

The above is all the contents of the article "how to split large log files under Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: File parameter log content size number prefix suffix command byte article subscript no value creation interest unit boy buddy beginning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL vpn Shulou Information Shulou Tech Info macOS