首页 > 系统相关 >Linux中split大文件分割和cat合并文件

Linux中split大文件分割和cat合并文件

时间:2023-06-01 14:24:39浏览次数:44  
标签:文件 分割 -- cat split output

当需要将较大的数据上传到服务器,或从服务器下载较大的日志文件时,往往会因为网络或其它原因而导致传输中断而不得不重新传输。这种情况下,可以先将大文件分割成小文件后分批传输,传完后再合并文件。

1.分割 -- split命令

可以指定按行数分割和按字节大小分割两种模式。

(1) 按行数分割

$ split -l 300 large_file.txt new_file_prefix

加上-d,使用数字后缀;加上--verbose,显示分割进度:

$ split -l50000 -d large_file.txt part_ --verbose

(2) 按字节大小分割

$ split -b 10m large_file.log new_file_prefix

 

2.合并 -- cat命令

$ cat part_* > merge_file.txt

 

[注] split命令语法:

复制代码
 split --h
Usage: split [OPTION]... [FILE [PREFIX]]
Output pieces of FILE to PREFIXaa, PREFIXab, ...;
default size is 1000 lines, and default PREFIX is 'x'.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -a, --suffix-length=N   generate suffixes of length N (default 2)            后缀名称的长度 (默认为2) 
      --additional-suffix=SUFFIX  append an additional SUFFIX to file names
  -b, --bytes=SIZE        put SIZE bytes per output file                       每个输出文件的字节大小
  -C, --line-bytes=SIZE   put at most SIZE bytes of records per output file    每个输出文件每行的最大字节大小
  -d                      use numeric suffixes starting at 0, not alphabetic   使用数字后缀代替字母后缀
      --numeric-suffixes[=FROM]  same as -d, but allow setting the start value
  -e, --elide-empty-files  do not generate empty output files with '-n'        不产生空的输出文件
      --filter=COMMAND    write to shell COMMAND; file name is $FILE           写入到shell命令行
  -l, --lines=NUMBER      put NUMBER lines/records per output file             设定每个输出文件的行数,默认行数是1000行
  -n, --number=CHUNKS     generate CHUNKS output files; see explanation below  产生chunks文件
  -t, --separator=SEP     use SEP instead of newline as the record separator;  使用新字符分割
                            '\0' (zero) specifies the NUL character
  -u, --unbuffered        immediately copy input to output with '-n r/...'     无需缓存
      --verbose           print a diagnostic just before each                  显示分割进度
                            output file is opened
      --help     display this help and exit                                    显示帮助信息
      --version  output version information and exit                           显示版本信息

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

CHUNKS may be:
  N       split into N files based on size of input
  K/N     output Kth of N to stdout
  l/N     split into N files without splitting lines/records
  l/K/N   output Kth of N to stdout without splitting lines/records
  r/N     like 'l' but use round robin distribution
  r/K/N   likewise but only output Kth of N to stdout

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/split>
or available locally via: info '(coreutils) split invocation'
复制代码

cat命令语法:

复制代码
$ cat --h
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

  -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -n
  -e                       equivalent to -vE
  -E, --show-ends          display $ at end of each line
  -n, --number             number all output lines
  -s, --squeeze-blank      suppress repeated empty output lines
  -t                       equivalent to -vT
  -T, --show-tabs          display TAB characters as ^I
  -u                       (ignored)
  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
      --help     display this help and exit
      --version  output version information and exit

Examples:
  cat f - g  Output f's contents, then standard input, then g's contents.
  cat        Copy standard input to standard output.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/cat>
or available locally via: info '(coreutils) cat invocation'
复制代码

功能说明:切割文件。

  语  法:split [--help][--version][-<行数>][-b <字节>][-C <字节>][-l <行数>][要切割的文件][输出文件名]

  补充说明:split可将文件切成较小的文件,预设每1000行会切成一个小文件。

  参  数:

  -<行数>或-l<行数>  指定每多少行就要切成一个小文件。

  -b<字节>  指定每多少字就要切成一个小文件。支持单位:m,k

  -C<字节>  与-b参数类似,但切割时尽量维持每行的完整性。

  --help  显示帮助。

  --version  显示版本信息。

  [输出文件名]  设置切割后文件的前置文件名,split会自动在前置文件名后再加上编号。

  使用例子:

    split -b 100m 1111.log (按照字节分隔)
    split -l 1000000 1111.log(按照行数分隔)

1. 分割文件

文件分割可以使用split命令,该即支持文本文件分割,又支持二进制文件分割;而合并文件可以使用cat命令。

1.1 文本文件分割

分割文本文件时,可以按文件大小分割,也可以按文本行数分割。

按文件大小分割

按文件大小分割文件时,需要以-C参数指定分割后的文件大小:

1 $ split -C 100M large_file.txt stxt

如上所示,我们将大文件large_file.txt按100M大小进行分割,并指定了分割后文件前缀stxt;当不指定前缀时,split会自动对分割文件进行命名,一般会以x开头。

按行分割

文本文件还可以以行为单位进行分割,以行数进行分割时会忽略文件大小,并以-l参数指定分割后文件的行数:

1 $ split -l 1000 large_file.txt stxt

1.2 二进制文件分割

二进制文件分割类似于按大小分割文本文件,不同的是以-b参数来指定分割后的文件大小:

1 $ split -b 100M data.bak sdata

2. 文件合并

文件合并使用cat命令,上面几种方式分割的文件都可以使用cat命令合并。

cat命令合并分割文件:

1 $ cat stxt* > new_file.txt

3. 命令格式

3.1 split命令说明

split命令格式如下:

split [选项]... [要切割的文件 [输出文件前缀]]

命令参数

-a, --suffix-length=N   使用长度为 N 的后缀 (默认 2)

-b, --bytes=SIZE        设置输出文件的大小。支持单位:m,k

-C, --line-bytes=SIZE   设置输出文件的最大行数。与 -b 类似,但会尽量维持每行的完整性

-d, --numeric-suffixes  使用数字后缀代替字母

-l, --lines=NUMBER      设备输出文件的行数

    --help     显示版本信息

    --version  输出版本信息

3.2 cat命令说明

cat是Linux下使用频率较高的命令之一,该令详细介绍:

cat连接文件并打印到标准输出设备上

cat命令的常见使用场景有:

显示文件内容:

1 $ cat filename

创建一个空文件:

1 $ cat > filename

文件合并:

1 $ cat file1 file2 > file

标签:文件,分割,--,cat,split,output
From: https://www.cnblogs.com/kn-zheng/p/17448776.html

相关文章

  • couldn't clear tomcat cache java.lang.NoSuchFieldException: resourceEntries
    2015-09-2500:17:11,435WARN[dqapp24http-nio-8002-exec-22]com.opensymphony.xwork2.util.LocalizedTextUtilcouldn'tcleartomcatcachejava.lang.NoSuchFieldException:resourceEntriesatjava.lang.Class.getDeclaredField(Class.java:2062)~[na:1.8......
  • aardio获取exe文件的版本号
    看文档以为是这样取varinfo=fsys.version.getInfo(io._exepath);fsys.version.getInfo(io._exepath).productVersion;或者fsys.version.getInfo(io._exepath).fileVersion;结果不是,返回的是1.2.3.4中的一个数字  后来才查到要自己拼接varr=fsys.version.getInfo......
  • 玩转服务器之数据传输篇:如何快速搭建FTP文件共享服务器
    FTP文件共享服务器介绍FTP服务(FileTransferProtocol,FTP)是最早应用于主机之间数据传输的基本服务之一,是目前使用最广泛的文件传送协议。FTP文件共享服务器在日常办公中可以实现多人之间文件的传递和共享,极大提高协同办公的效率。Vsftpd是一款在Linux发行版中最受推崇的FTP服务......
  • Navicat远程链接mysql数据库问题
    问题描述:docker安装mysql数据后使用Navicat链接mysql后,创建数据库报“1044-Accessdenidforuser'root'...”解决方案:1.登录远程服务器,进入mysql容器dockerexec-itmysql/bin/bash2.mysql-uroot-p->grantallprivilegeson*.*to'root'@'%';->flush......
  • [Java] try catch finally注意点
    trycatchfinaly注意点finaly块中有return语句publicstaticvoidmain(String[]args){System.out.println(throwException());}publicstaticintthrowException(){try{inti=1/0;}catch(Exceptione){e.printStackTrace();......
  • Alluxio : 开源分布式内存文件系统
    Alluxio:开源分布式内存文件系统Alluxioisamemoryspeedvirtualdistributedstoragesystem.Alluxio是一个开源的基于内存的分布式存储系统,现在成为开源社区中成长最快的大数据开源项目之一。公司简介:由项目的创建者李浩源以及来自UCBerkeley,Google,CMU,Palantir,Stan......
  • nginx+tomcat+https
    nginx:config :location/{add_headerContent-Security-Policyupgrade-insecure-requests;proxy_passhttp://127.0.0.1:8080;proxy_set_headerHost$host:$server_port;proxy_set_headerREMOTE_HOST$remote_addr;proxy_set_headerX-Real-IP$remot......
  • python通过文件操作字典
    python通过文件操作字典python把字典保存到文件中python从文件中加载字典importjsonmy_dict={'Apple':4,'Banana':2,'Orange':6,'Grapes':11,'area1':[[23,56],[66,12],[68,89],[90,890]]}#保存文件tf=open("myDictionary.js......
  • 在linux服务器上使用命令行下载百度网盘中的文件
    转载自https://blog.csdn.net/qq_37428140/article/details/124219739 1、安装bypy工具pipinstallbypy2、认证自己的网盘账号bypyinfo按照提示,在控制台和浏览器中完成认证3、授权成功后,我们可以在网盘中的“我的应用数据”目录下看到如下文件夹: 将需要下载的......
  • Replicate
    YoucanuseReplicatetorunmachinelearningmodelsinthecloudfromyourowncode,withouthavingtosetupanyservers.Ourcommunityhaspublished hundredsofopen-sourcemodelsthatyoucanrun,oryoucanrunyourownmodels.RunamodelfromPyth......