Linux split command - splitting files into slices

Linux split command: splitting files into slices

Linux split command Function Description

Use the split command to split the input file into pieces and output fixed size blocks, the output file name is "prefix aa", "prefix ab", the default prefix is "x", the default size is 1000 lines.

The split command can split a large file into many smaller files. Sometimes it is necessary to split a file into smaller pieces, for example to improve readability, generate logs, etc.

Linux split command Syntax

split [Option] [Enter [prefix]]

The meaning of each option in the command is shown in the following table.

Option Description
-d Use numeric suffixes, not letters
-C <Size> Places the maximum size of lines in each output file in bytes
-l <Line count> Specify how many lines in each output file
--verbose Display diagnostics before each output file is opened
-a <Suffix length> Use a suffix of the specified length, which defaults to 2
-b <Size> Specifies the size of each output file in bytes

Linux split command Example

Split the /root/apidemos.com.split.txt file into slices, with each output file having a size of 10k

Linux split command

Split the /root/install.log file into pieces, with each output file being 1024 bytes in size

[root@rhel ~]# split -b 1024 /root/install.log

Split the /root/install.log file into pieces and prefix the output file with PREFIX

[root@rhel ~]# split -b 1024 /root/install.log PREFIX

Split the /root/install.log file into pieces, specifying the number of output file suffixes as 5

[root@rhel ~]# split -a 5 /root/install.log

Split the /root/install.log file into pieces, specifying that each output file has 100 lines

[root@rhel ~]# split -l 100 /root/install.log

Generate a test file with a size of 100KB

[root@localhost split]# dd if=/dev/zero bs=100k count=1 of=date.file
1+0 records in
1+0 records out
102400 bytes (102 kB) copied, 0.00043 seconds, 238 MB/s

Use the split command to split the date.file file created above into smaller files of 10KB in size

[root@localhost split]# split -b 10k date.file 
[root@localhost split]# ls
date.file  xaa  xab  xac  xad  xae  xaf  xag  xah  xai  xaj

The file is split into multiple files with alphabetic suffixes, if you want to use numeric suffixes you can use the -d parameter, and you can use -a length to specify the length of the suffix

[root@localhost split]# split -b 10k date.file -d -a 3
[root@localhost split]# ls
date.file  x000  x001  x002  x003  x004  x005  x006  x007  x008  x009

Specify the file name prefix for the split file

[root@localhost split]# split -b 10k date.file -d -a 3 split_file
[root@localhost split]# ls
date.file  split_file000  split_file001  split_file002  split_file003  split_file004  split_file005  split_file006  split_file007  split_file008  split_file009

Use the -l option to split the file according to the number of lines in the file

For example, splitting a file into smaller files containing 10 lines each

split -l 10 date.file
Like(1)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?