
0 前言
在Linux系统中,除了tar命令,我们还可以使用cpio命令来进行文件和目录的归档。
1 cpio命令的功能,帮助信息,格式,选项和参数说明
1.1 cpio命令的功能
cpio 名字来自 "copy in, copy out"(拷进拷出),是Linux中用于处理归档文件(例如,.cpio或.tar文件)的工具,即从一个地方复制文件到另一个地方,同时保留文件的属性,如所有权、权限和时间戳。
使用cpio命令必须带参数或选项。
1.2 cpio命令的帮助信息
我们可以使用命令
cpio --help
或
cpio --usage
来查看cpio命令的帮助信息。
1.2.1 bash下的cpio命令帮助信息
[purpleendurer @ bash ~] cpio --help
Usage: cpio [OPTION...] [destination-directory]
GNU `cpio' copies files to and from archives
Examples:
  # Copy files named in name-list to the archive
  cpio -o < name-list [> archive]
  # Extract files from the archive
  cpio -i [< archive]
  # Copy files named in name-list to destination-directory
  cpio -p destination-directory < name-list
 Main operation mode:
  -i, --extract              Extract files from an archive (run in copy-in
                             mode)
  -o, --create               Create the archive (run in copy-out mode)
  -p, --pass-through         Run in copy-pass mode
  -t, --list                 Print a table of contents of the input
 Operation modifiers valid in any mode:
      --block-size=BLOCK-SIZE   Set the I/O block size to BLOCK-SIZE * 512
                             bytes
  -B                         Set the I/O block size to 5120 bytes
  -c                         Identical to "-H newc", use the new (SVR4)
                             portable format.If you wish the old portable
                             (ASCII) archive format, use "-H odc" instead.
  -C, --io-size=NUMBER       Set the I/O block size to the given NUMBER of
                             bytes
      --force-local          Archive file is local, even if its name contains
                             colons
  -f, --nonmatching          Only copy files that do not match any of the given
                             patterns
  -F, --file=[[USER@]HOST:]FILE-NAME
                             Use this FILE-NAME instead of standard input or
                             output. Optional USER and HOST specify the user
                             and host names in case of a remote archive
  -H, --format=FORMAT        Use given archive FORMAT
  -M, --message=STRING       Print STRING when the end of a volume of the
                             backup media is reached
  -n, --numeric-uid-gid      In the verbose table of contents listing, show
                             numeric UID and GID
      --quiet                Do not print the number of blocks copied
      --rsh-command=COMMAND  Use remote COMMAND instead of rsh
  -v, --verbose              Verbosely list the files processed
  -V, --dot                  Print a "." for each file processed
  -W, --warning=FLAG         Control warning display. Currently FLAG is one of
                             'none', 'truncate', 'all'. Multiple options
                             accumulate.
 Operation modifiers valid only in copy-in mode:
  -b, --swap                 Swap both halfwords of words and bytes of
                             halfwords in the data. Equivalent to -sS
  -r, --rename               Interactively rename files
  -s, --swap-bytes           Swap the bytes of each halfword in the files
  -S, --swap-halfwords       Swap the halfwords of each word (4 bytes) in the
                             files
      --to-stdout            Extract files to standard output
  -E, --pattern-file=FILE    Read additional patterns specifying filenames to
                             extract or list from FILE
      --only-verify-crc      When reading a CRC format archive, only verify the
                             checksum of each file in the archive, don't
                             actually extract the files
 Operation modifiers valid only in copy-out mode:
  -A, --append               Append to an existing archive.
      --device-independent, --reproducible
                             Create device-independent (reproducible) archives
      --ignore-devno         Don't store device numbers
  -O [[USER@]HOST:]FILE-NAME Archive filename to use instead of standard
                             output. Optional USER and HOST specify the user
                             and host names in case of a remote archive
      --renumber-inodes      Renumber inodes
 Operation modifiers valid only in copy-pass mode:
  -l, --link                 Link files instead of copying them, when
                             possible
 Operation modifiers valid in copy-in and copy-out modes:
      --absolute-filenames   Do not strip file system prefix components from
                             the file names
      --no-absolute-filenames   Create all files relative to the current
                             directory
 Operation modifiers valid in copy-out and copy-pass modes:
  -0, --null                 A list of filenames is terminated by a null
                             character instead of a newline
  -a, --reset-access-time    Reset the access times of files after reading
                             them
  -I [[USER@]HOST:]FILE-NAME Archive filename to use instead of standard input.
                             Optional USER and HOST specify the user and host
                             names in case of a remote archive
  -L, --dereference          Dereference  symbolic  links  (copy  the files
                             that they point to instead of copying the links).
  -R, --owner=[USER][:.][GROUP]   Set the ownership of all files created to the
                             specified USER and/or GROUP
 Operation modifiers valid in copy-in and copy-pass modes:
  -d, --make-directories     Create leading directories where needed
  -m, --preserve-modification-time
                             Retain previous file modification times when
                             creating files
      --no-preserve-owner    Do not change the ownership of the files
      --sparse               Write files with large blocks of zeros as sparse
                             files
  -u, --unconditional        Replace all files unconditionally
  -?, --help                 give this help list
      --usage                give a short usage message
      --version              print program version
Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.
Report bugs to <bug-cpio@gnu.org>.
1.2.2 银河麒麟(kylin)系统中的cpio命令帮助信息
[purpleendurer @ kylin ~] cpio --help
用法: cpio [选项...] [目标目录]
GNU `cpio' 归档文件及从包中提取文件
例子:
  # 归档 name-list 中的文件到 archive
  cpio -o < name-list [> archive]
  # 从 archive 中提取文件
  cpio -i [< archive]
  # 拷贝 name-list 中的文件到目标目录(destination-directory)
  cpio -p destination-directory < name-list
 主操作模式
  -i, --extract              从包中提取文件 (运行 copy-in 模式)
  -o, --create               创建包 (运行 copy-out 模式)
  -p, --pass-through         运行 copy-pass 模式
  -t, --list                 打印输入内容列表
 应用于所有模式的选项:
      --block-size=BLOCK-SIZE   设置 I/O 块大小为 BLOCK-SIZE * 512
                             字节
  -B                         设置 I/O 块大小为 5120 字节
  -c                         使用老的可移植的 (ASCII) 包格式
  -C, --io-size=NUMBER       设置 I/O 块大小为指定的 NUMBER 字节
      --force-local
                             包文件是本地的,尽管名字中含有冒号
  -f, --nonmatching          仅拷贝不匹配任意给定的模式的文件
  -F, --file=[[用户@]主机:]文件名
                             用"文件名"来替代标准输入和输出。如果是非本地的文件,则用可选的"用户"和"主机"来指定用户名和主机名。
  -H, --format=格式        使用指定的包格式
  -M, --message=STRING       当到达备份介质的尾部的时候打印
                             STRING
  -n, --numeric-uid-gid      在内容列表的详表中,显示数字的 UID
                             和 GID
      --quiet                不要打印已拷贝的块数
      --rsh-command=COMMAND  用 COMMAND 替代 rsh
  -v, --verbose              详细列出已处理的文件
  -V, --dot                  每处理一个文件就打印一个"."
  -W, --warning=FLAG         控制警告信息显示。当前 FLAG
                             可为"none"、"truncate"或"all"。多个选项可以累积。
 命令修饰仅在 copy-in 模式中有效:
  -b, --swap
                             交换数据中每个字的两个半字以及每个半字中的两个字节。等价于
                             -sS
  -r, --rename               交互式重命名文件
  -s, --swap-bytes           交换文件中每个半字中的两个字节
  -S, --swap-halfwords
                             交换文件中每个字(4个字节)中的两个半字
      --to-stdout            提取文件到标准输出
  -E, --pattern-file=FILE    从 FILE
                             中读取额外的用于指定提取或列表的文件名的模式
      --only-verify-crc      当读取一个 CRC
                             格式的包,仅检验包中每个文件的
                             CRC,不提取文件
 应用于 copy-out 模式的选项
  -A, --append               追加到已存在的归档文件。
  -O [[用户@]主机:]文件名
                             使用包文件名而不是标准输出。如果文件在远程机器上,则可指定用户和主机
 应用于 copy-pass 模式的选项:
  -l, --link                 在可行时链接文件而不是拷贝文件
 应用于 copy-in 及 copy-out 模式的选项:
      --absolute-filenames   文件名不去除文件系统前缀
      --no-absolute-filenames   相对于当前目录来创建所有文件
 应用于 copy-out 及 copy-pass 模式的选项:
  -0, --null                 文件名列表采用 NULL
                             而不是换行作为分割符
  -a, --reset-access-time    文件读取后恢复文件的访问时间
  -I [[用户@]主机:]文件名
                             从文件读入而不是从标准输入读入。
                             如果文件在远程机器上,
                             则可指定用户和主机
  -L, --dereference          跟随符号链接
                             (拷贝符号链接指向的文件而不是拷贝链接本身)
  -R, --owner=[用户][:.][组]
                             设置所有文件的所有权信息到指定的用户和/或组
 应用于 copy-in 和 copy-pass 模式的选项:
  -d, --make-directories     需要时创建目录
      --extract-over-symlinks   Force writing over symbolic links
  -m, --preserve-modification-time
                             创建文件时保留以前文件的修改时间
      --no-preserve-owner    不改变文件的所有权
      --sparse
                             把含有大块零的文件以稀疏文件方式写出
  -u, --unconditional        无条件覆盖所有文件
  -?, --help                 显示此帮助列表
      --usage                显示简短的用法说明
      --version              打印程序版本
对长选项必须或可选的参数同样适用于相应的短选项。
请向 <bug-cpio@gnu.org> 报告错误。
[purpleendurer @ kylin ~] 1.3 cpio 命令格式
cpio [选项...] [目标目录]
1.4 cpio命令的选项说明
与tar命令相似,cpio命令的选项也很多,分为
1.4.1 主要操作模式选项

| 选项 | 说明 | 
|---|---|
| -i, --extract | 从包中提取文件 (运行 copy-in 模式) | 
| -o, --create | 创建包 (运行 copy-out 模式) | 
| -p, --pass-through | 运行 copy-pass 模式 | 
| -t, --list | 将输入内容以列表形式打印 | 
1.4.2 应用于所有模式的选项

| 选项 | 说明 | 
|---|---|
| --block-size=BLOCK-SIZE | 设置 I/O 块大小为 BLOCK-SIZE * 512字节 | 
| -B | 设置 I/O 块大小为 5120 字节 | 
| -c | 使用老的可移植的 (ASCII) 包格式 | 
| -C, --io-size=NUMBER | 设置 I/O 块大小为指定的 NUMBER 字节 | 
| --force-local | 包文件是本地的,尽管名字中含有冒号 | 
| -f, --nonmatching | 仅拷贝不匹配任意给定的模式的文件 | 
| -F, --file=[[用户@]主机:]文件名 | 用"文件名"来替代标准输入和输出。如果是非本地的文件,则用可选的"用户"和"主机"来指定用户名和主机名。 | 
| -H, --format=格式 | 使用指定的包格式 | 
| -M, --message=STRING | 当到达备份介质的尾部的时候打印 STRING | 
| -n, --numeric-uid-gid | 在内容列表的详表中,显示数字的 UID和 GID | 
| --quiet | 不要打印已拷贝的块数 | 
| --rsh-command=COMMAND | 用 COMMAND 替代 rsh | 
| -v, --verbose | 详细列出已处理的文件 | 
| -V, --dot | 每处理一个文件就打印一个"." | 
| -W, --warning=FLAG | 控制警告信息显示。 FLAG可为"none"、"truncate"或"all"。多个选项可以累积。 | 
1.4.3 仅在copy-in 模式中有效的选项

| 选项 | 说明 | 
|---|---|
| -b, --swap | 交换数据中每个字的两个半字以及每个半字中的两个字节。等价于-sS | 
| -r, --rename | 交互式重命名文件 | 
| -s, --swap-bytes | 交换文件中每个半字中的两个字节 | 
| -S, --swap-halfwords | 交换文件中每个字(4个字节)中的两个半字 | 
| --to-stdout | 提取文件到标准输出 | 
| -E, --pattern-file=FILE | 从 FILE中读取额外的用于指定提取或列表的文件名的模式 | 
| --only-verify-crc | 当读取一个 CRC格式的包时,仅检验包中每个文件的CRC,不提取文件 | 
1.4.4 仅在copy-out 模式中有效的选项

| 选项 | 说明 | 备注 | 
|---|---|---|
| -A, --append | 追加到已存在的归档文件。 | |
| --device-independent, --reproducible | 创建独立于设备(可重现)的存档 | 银河麒麟操作系统不支持 | 
| --ignore-devno | 不存储设备编号 | 银河麒麟操作系统不支持 | 
| -O [[用户@]主机:]文件名 | 使用包文件名而不是标准输出。如果文件在远程机器上,则可指定用户和主机 | |
| --renumber-inodes | 对 inode 重新编号 | 银河麒麟操作系统不支持 | 
1.4.5 仅在copy-out 模式中有效的选项

| 选项 | 说明 | 
|---|---|
| -l, --link | 在可行时链接文件而不是拷贝文件 | 
1.4.6 在copy-in 及 copy-out 模式中有效的选项

| 选项 | 说明 | 
|---|---|
| --absolute-filenames | 文件名不去除文件系统前缀 | 
| --no-absolute-filenames | 相对于当前目录来创建所有文件 | 
1.4.7 在 copy-out 及 copy-pass 模式中有效的选项

| 选项 | 说明 | 
|---|---|
| -0, --null | 文件名列表采用 NULL而不是换行作为分割符 | 
| -a, --reset-access-time | 文件读取后恢复文件的访问时间 | 
| -I [[用户@]主机:]文件名 | 从文件读入而不是从标准输入读入。如果文件在远程机器上,则可指定用户和主机 | 
| -L, --dereference | 跟随符号链接(拷贝符号链接指向的文件而不是拷贝链接本身) | 
| -R, --owner=[用户][:.][组] | 设置所有文件的所有权信息到指定的用户和/或组 | 
1.4.8 在 copy-in 及 copy-pass 模式中有效的选项

| 选项 | 说明 | 备注 | 
|---|---|---|
| -d, --make-directories | 需要时创建目录 | |
| --extract-over-symlinks | 强制复写符号链接 | 银河麒麟系统支持 | 
| -m, --preserve-modification-time | 创建文件时保留以前文件的修改时间 | |
| --no-preserve-owner | 不改变文件的所有权 | |
| --sparse | 把含有大块零的文件以稀疏文件方式写出 | |
| -u, --unconditional | 无条件覆盖所有文件 | |
| -?, --help | 显示此帮助列表 | |
| --usage | 显示简短的用法说明 | |
| --version | 打印程序版本 | 
1.4.9 相关说明
对长选项必须或可选的参数同样适用于相应的短选项。
1.5 cpio命令的三种模式
cpio命令的选项和参数取决于它运行的模式,即copy-out, copy-in或copy-pass模式。
1.5.1 将文件复制到归档文件中(copy-out模式)
在copy-out模式下,cpio命令将文件复制到归档文件中。它从标准输入读取文件名列表,每行一个,然后将归档文件写入标准输出。
生成文件名列表的典型方法是使用find命令;给find命令加上-depth选项,以最小化不可读目录的权限问题。
1.5.2 从归档文件中提取文件(copy-in模式)
copy-in模式下,cpio命令从归档文件中提取文件或列出归档文件的内容。
它从标准输入读取归档文件。
任何非选项的命令行参数都是shell通配符模式;只有归档文件中名称与一个或多个模式匹配的文件才会从归档文件中提取。
与shell不同的是,文件名中的初始'.'可以匹配模式开头的通配符,而文件名中的'/'也可以匹配通配符。
如果没有给出模式,所有文件都会被提取。
1.5.3 将文件从一个目录树复制到另一个目录树(copy-pass模式)
在copy-pass模式下,cpio命令将文件从一个目录树复制到另一个目录树,将copy-out和copy-in步骤结合在一起,而不实际使用归档文件。
1.6 cpio命令支持的归档格式
cpio命令支持多种归档格式,包括:binary, old ASCII, new ASCII, CRC, HP-UX binary, HP-UX old ASCII, old tar, 和 POSIX .1 tar。
默认情况下,cpio命令创建binary格式的归档文件,以便与旧版本的cpio程序兼容。
在从归档文件中提取文件时,cpio命令会自动识别归档文件的类型,并且可以读取在不同字节顺序的机器上创建的归档文件。