命令行大全

cpio

rose1 发表于 2020-08-20 15:36浏览次数:

cpio将文件复制到存档中或从存档中复制文件。

查看英文版

目录:

1 cpio 运行系统环境

2 cpio 说明

3 cpio 语法

4 cpio 例子

cpio 运行系统环境

Linux

cpio 说明

GNU cpio是用于创建和提取档案,或将文件从一个地方复制到另一个地方的工具。它处理许多cpio格式以及读写tar文件。

支持以下存档格式:二进制,旧ASCII,新ASCII,CRC,HP-UX二进制,HP-UX旧ASCII,旧tar和POSIX .1 tar。提供tar格式是为了与tar程序兼容。默认情况下,cpio创建二进制格式的归档文件,以与较早的cpio程序兼容。从存档中提取时,cpio会自动识别它正在读取的存档类型,并且可以读取在具有不同字节顺序的计算机上创建的存档。

GNU cpio is a tool for creating and extracting archives, or copying files from one place to another. It handles many cpio formats as well as reading and writing tar files.

The following archive formats are supported: binary, old ASCII, new ASCII, CRC, HP-UX binary, HP-UX old ASCII, old tar, and POSIX.1 tar. The tar format is provided for compatibility with the tar program. By default, cpio creates binary format archives, for compatibility with older cpio programs. When extracting from archives, cpio automatically recognizes which kind of archive it is reading and can read archives created on machines with a different byte-order.

查看英文版

查看中文版

cpio 语法

复制输出模式语法:

在复制模式下,cpio将文件复制到归档文件中。它读取标准输入上的文件名列表(每行一个),并将存档写入标准输出。生成文件名列表的一种典型方法是使用find命令。

cpio {-o|--create} [-0acvABLV] [-C bytes] [-H format] [-M message] 
     [-O [[user@]host:]archive] [-F [[user@]host:]archive] 
     [--file=[[user@]host:]archive] [--format=format] 
     [--message=message][--null] [--reset-access-time] [--verbose] [--dot] 
     [--append] [--block-size=blocks] [--dereference] [--io-size=bytes] 
     [--rsh-command=command] [--help] [--version] < name-list [> archive]

复制模式语法:

在复制模式下,cpio将文件复制出归档文件或列出归档文件内容。它从标准输入中读取存档。任何非选项命令行 参数都是shell的 glob模式。仅从存档中复制名称与这些模式中的一种或多种匹配的存档文件。与外壳不同,首字母为' 。文件名中的'确实与模式开头的通配符匹配,文件名中的' / '可以匹配通配符。如果未提供任何模式,则将提取所有文件。复制模式的语法:

cpio {-i|--extract} [-bcdfmnrtsuvBSV] [-C bytes] [-E file] [-H format] 
     [-M message] [-R [user][:.][group]] 
     [-I [[user@]host:]archive] [-F [[user@]host:]archive] 
     [--file=[[user@]host:]archive] [--make-directories] [--nonmatching] 
     [--preserve-modification-time] [--numeric-uid-gid] [--rename] [-t|--list] 
     [--swap-bytes] [--swap] [--dot] [--unconditional] [--verbose] 
     [--block-size=blocks] [--swap-halfwords] [--io-size=bytes] 
     [--pattern-file=file] [--format=format] [--owner=[user][:.][group]] 
     [--no-preserve-owner] [--message=message] [--force-local] 
     [--no-absolute-filenames] [--absolute-filenames] [--sparse] 
     [--only-verify-crc] [--to-stdout] [--quiet] [--rsh-command=command] 
     [--help] [--version] [pattern...] [< archive]

复制通过模式语法:

在复制通过模式下,cpio将文件从一个目录树复制到另一个目录树,将复制和复制步骤组合在一起,而无需实际使用归档。它从标准输入中读取要复制的文件列表。将它们复制到的目录作为非选项参数给出。复制通过模式语法:

cpio {-p|--pass-through} [-0adlmuvLV] [-R [user][:.][group]] [--null] 
     [--reset-access-time] [--make-directories] [--link] [--quiet] 
     [--preserve-modification-time] [--unconditional] [--verbose] [--dot] 
     [--dereference] [--owner=[user][:.][group]] [--no-preserve-owner] 
     [--sparse] [--help] [--version] destination-directory < name-list

选件

-0, --null 读取以空字符结尾的文件名列表,而不是换行符,以便可以存储名称包含换行符的文件。GNU find是产生以空值结尾的文件名列表的一种方法。此选项可以在复制和复制通过模式下使用。
-a, --reset-access-time 读取文件后,重置文件的访问时间,以使其看起来不像已被读取。
-A, --append 追加 追加到现有存档。仅在复制模式下工作。归档文件必须是使用-O-F(-file)选项指定的磁盘文件。
-b, --swap 交换 交换数据中的单词半字和字节半字节。等效于-sS。此选项可以在复制模式下使用。使用此选项可转换的32位 整数大端之间尾数和little-endian机器。
-B 将I / O 块大小设置为5120字节。最初,块大小为512字节。
--block-size=BLOCK-SIZE 将I / O块大小设置为BLOCK-SIZE * 512字节。
-c ' -H newc ' 相同;使用新的(SVR4)可移植格式。如果要使用旧的可移植(ASCII)归档格式,请改用' -H odc '。
-C IO-SIZE, --io-size=IO-SIZE 将I / O块大小设置为IO-SIZE字节。
-d, --make-directories 在需要的地方创建引导目录。
-E FILE, --pattern-file=FILE 阅读其他指定文件名的模式以从FILE中提取或列出。FILE的行被视为cpio的非选项参数。此选项在复制模式下使用。
-f, --nonmatching 仅复制与任何给定模式都不匹配的文件。
-F, --file=archive 要使用的归档文件名,而不是标准输入或输出。要将另一台计算机上的磁带机用作存档,请使用以“ HOSTNAME: ” 开头的文件名。该主机可以通过前面的用户名和一个“ @ ”来访问远程磁带驱动器作为用户,如果你有权限这样做(在该用户的“的条目〜/ .rhosts中 ”文件)。
--force-local 使用-F,-I-O,即使存档文件名包含冒号(通常会指示远程主机名),也可以将其作为本地文件。
-H FORMAT, --format=FORMAT 使用存档格式FORMAT。有效格式如下:在大写字母中也可以识别相同的名称。复制模式下的默认设置是自动检测存档格式,复制模式下的默认设置是' bin '。

bin: 过时的二进制格式。

odc: 旧的(POSIX .1)可移植格式。

newc: 新的(SVR4)可移植格式,支持具有超过65536个inode的文件系统。

crc: 添加了校验和的新(SVR4)可移植格式。

tar: 旧的tar格式。

ustar: POSIX .1 tar格式。另外,可以识别相似但不相同的GNU tar存档。

hpbin: HP-UX的cpio使用的过时二进制格式(以不同方式存储设备文件)。

hpodc: HP-UX的cpio使用的可移植格式(以不同方式存储设备文件)。
-i, --extract 在复制模式下运行。(请参阅“ 复制模式 ”)。
-I archive 要使用的归档文件名,而不是标准输入。要将另一台计算机上的磁带机用作存档,请使用以“ HOSTNAME: ” 开头的文件名。主机名前面可以有一个用户名和一个@,以该用户的身份访问远程磁带驱动器(如果您具有这样做的权限)(该用户的〜/ .rhosts文件中的一项)。
-k 忽略 与其他版本的cpio兼容。
-l, --link 尽可能链接文件而不是复制它们。
-L, --dereference 复制符号链接指向的文件,而不是符号链接本身。
-m, --preserve-modification-time 创建文件时,请保留以前的文件修改时间。
-M MESSAGE, --message=MESSAGE 当备份介质(例如磁带或软盘)的卷结束时,请打印MESSAGE,以提示用户插入新的卷。如果MESSAGE包含字符串' %d ',则将其替换为当前卷号(从1开始)。
-n, --numeric-uid-gid 使用' --verbose '选项时,显示数字UID和GID而不是将其转换为名称
--no-absolute-filenames 即使在归档中具有绝对文件名,也以复制方式创建相对于当前目录的所有文件。
--absolute-filenames 这是默认设置:在插入模式下,告诉cpio不要从文件名中删除包含“ .. ”和前导斜杠的前导文件名组件。
--no-preserve-owner 不要更改文件的所有权;留给用户解压缩。这是非root用户的默认设置,因此System V上的用户不会无意中放弃文件。此选项可用于复制输入模式和复制通过模式。
-o, --create 在复制模式下运行。(请参阅“ 复制模式 ”)。
-O archive 要使用的归档文件名,而不是标准输出。要将另一台计算机上的磁带机用作存档,请使用以“ HOSTNAME: ” 开头的文件名。主机名前面可以有一个用户名和一个@,以该用户的身份访问远程磁带驱动器(如果您具有这样做的权限)(该用户的〜/ .rhosts文件中的一项)。
--only-verify-crc 读取CRC格式的存档时,请验证存档中每个文件的CRC。实际不提取文件。
-p, --pass-through 在复制通过模式下运行。(请参阅“ 复制通过模式 ”)。
--quiet 不要打印复制的块数。
-r, --rename 交互式重命名文件。
-R [user][:.][group], --owner [user][:.][group] 在复制和复制通过模式下,将创建的所有文件的所有权设置给指定的用户和/或组。用户,组或两者都必须存在。如果省略该组,但使用' : '或' 。'给出分隔符,使用给定用户的登录组。只有超级用户可以更改文件的所有权。
--rsh-command=COMMAND 通知应使用COMMAND与远程设备通信的cpio
-s, --swap-bytes 交换文件中每个半字的字节(一对字节)。此选项可以在复制模式下使用。
-S, --swap-halfwords 交换文件中每个单词的半字(4个字节)。此选项可以在复制模式下使用。
--sparse 将大块零的文件写为稀疏文件。此选项用于复制和复制通过模式。
-t, --list 打印输入的目录。
--to-stdout 将文件提取到标准输出。此选项可以在复制模式下使用。
-u, --unconditional 替换所有文件,而无需询问是否用旧文件替换现有的新文件。
-v, --verbose 列出已处理的文件,或使用“ -t ” 列出目录的“ ls -l ”样式表。在ustar归档文件的详细目录中,归档文件中本地系统上不存在的用户名和组名被替换为与存储在归档文件中的数字UID和GID本地对应的名称。
-V, --dot 打印一个' 。',用于处理的每个文件。
--version 打印cpio程序的版本号并退出。

Copy-Out Mode Syntax:

In copy-out mode, cpio copies files into an archive. It reads a list of filenames, one per line, on the standard input and writes the archive onto the standard output. A typical way to generate the list of filenames is with the find command; you should give find the -depth option to minimize problems with permissions on directories that are unreadable. Copy-Out mode syntax:

cpio {-o|--create} [-0acvABLV] [-C bytes] [-H format] [-M message] 
     [-O [[user@]host:]archive] [-F [[user@]host:]archive] 
     [--file=[[user@]host:]archive] [--format=format] 
     [--message=message][--null] [--reset-access-time] [--verbose] [--dot] 
     [--append] [--block-size=blocks] [--dereference] [--io-size=bytes] 
     [--rsh-command=command] [--help] [--version] < name-list [> archive]

Copy-In Mode Syntax:

In copy-in mode, cpio copies files out of an archive or lists the archive contents. It reads the archive from the standard input. Any non-option command line arguments are shell globbing patterns; only files in the archive whose names match one or more of those patterns are copied from the archive. Unlike in the shell, an initial '.' in a filename does match a wildcard at the start of a pattern, and a '/' in a filename can match wildcards. If no patterns are given, all files are extracted. Copy-In mode syntax:

cpio {-i|--extract} [-bcdfmnrtsuvBSV] [-C bytes] [-E file] [-H format] 
     [-M message] [-R [user][:.][group]] 
     [-I [[user@]host:]archive] [-F [[user@]host:]archive] 
     [--file=[[user@]host:]archive] [--make-directories] [--nonmatching] 
     [--preserve-modification-time] [--numeric-uid-gid] [--rename] [-t|--list] 
     [--swap-bytes] [--swap] [--dot] [--unconditional] [--verbose] 
     [--block-size=blocks] [--swap-halfwords] [--io-size=bytes] 
     [--pattern-file=file] [--format=format] [--owner=[user][:.][group]] 
     [--no-preserve-owner] [--message=message] [--force-local] 
     [--no-absolute-filenames] [--absolute-filenames] [--sparse] 
     [--only-verify-crc] [--to-stdout] [--quiet] [--rsh-command=command] 
     [--help] [--version] [pattern...] [< archive]

Copy-Pass Mode Syntax:

In copy-pass mode, cpio copies files from one directory tree to another, combining the copy-out and copy-in steps without actually using an archive. It reads the list of files to copy from the standard input; the directory into which it will copy them is given as a non-option argument. Copy-Pass mode syntax:

cpio {-p|--pass-through} [-0adlmuvLV] [-R [user][:.][group]] [--null] 
     [--reset-access-time] [--make-directories] [--link] [--quiet] 
     [--preserve-modification-time] [--unconditional] [--verbose] [--dot] 
     [--dereference] [--owner=[user][:.][group]] [--no-preserve-owner] 
     [--sparse] [--help] [--version] destination-directory < name-list

Options

-0--null Read a list of filenames terminated by a null character, instead of a newline, so that files whose names contain newlines can be archived. GNU find is one way to produce a list of null-terminated filenames. This option may be used in copy-out and copy-pass modes.
-a--reset-access-time Reset the access times of files after reading them, so that it does not look like they have been read.
-A--append Append to an existing archive. Only works in copy-out mode. The archive must be a disk file specified with the -O or -F (-file) option.
-b--swap Swap both halfwords of words and bytes of halfwords in the data. Equivalent to -sS. This option may be used in copy-in mode. Use this option to convert 32-bit integers between big-endian and little-endian machines.
-B Set the I/O block size to 5120 bytes. Initially the block size is 512 bytes.
--block-size=BLOCK-SIZE Set the I/O block size to BLOCK-SIZE * 512 bytes.
-c Identical to '-H newc'; uses the new (SVR4) portable format. If you want the old portable (ASCII) archive format, use '-H odc' instead.
-C IO-SIZE, --io-size=IO-SIZE Set the I/O block size to IO-SIZE bytes.
-d--make-directories Create leading directories where needed.
-E FILE, --pattern-file=FILE Read additional patterns specifying filenames to extract or list from FILE. The lines of FILE are treated as if they had been non-option arguments to cpio. This option is used in copy-in mode.
-f--nonmatching Only copy files that do not match any of the given patterns.
-F--file=archive Archive filename to use instead of standard input or output. To use a tape drive on another machine as the archive, use a filename that starts with 'HOSTNAME:'. The hostname can be preceded by a username and an '@' to access the remote tape drive as that user, if you have permission to do so (an entry in that user's '~/.rhosts' file).
--force-local With -F-I, or -O, take the archive file name to be a local file even if it contains a colon, which would ordinarily indicate a remote hostname.
-H FORMAT, --format=FORMAT Use archive format FORMAT. The valid formats are listed below; the same names are also recognized in all-caps. The default in copy-in mode is to automatically detect the archive format, and in copy-out mode is 'bin'.

bin: The obsolete binary format.

odc: The old (POSIX .1) portable format.

newc: The new (SVR4) portable format, which supports file systems having more than 65536 inodes.

crc: The new (SVR4) portable format with a checksum added.

tar: The old tar format.

ustar: The POSIX .1 tar format. Also, recognizes GNU tar archives, which are similar but not identical.

hpbin: The obsolete binary format used by HP-UX's cpio (which stores device files differently).

hpodc: The portable format used by HP-UX's cpio (which stores device files differently).
-i--extract Run in copy-in mode. (see 'Copy-in mode').
-I archive Archive filename to use instead of standard input. To use a tape drive on another machine as the archive, use a filename that starts with 'HOSTNAME:'. The hostname can be preceded by a username and an '@' to access the remote tape drive as that user, if you have permission to do so (an entry in that user's '~/.rhosts' file).
-k Ignored; for compatibility with other versions of cpio.
-l--link Link files instead of copying them, when possible.
-L--dereference Copy the file that a symbolic link points to, rather than the symbolic link itself.
-m--preserve-modification-time Retain previous file modification times when creating files.
-M MESSAGE, --message=MESSAGE Print MESSAGE when the end of a volume of the backup media (such as a tape or a floppy disk) is reached, to prompt the user to insert a new volume. If MESSAGE contains the string '%d', it is replaced by the current volume number (starting at 1).
-n--numeric-uid-gid Show numeric UID and GID instead of translating them into names when using the '--verbose' option.
--no-absolute-filenames Create all files relative to the current directory in copy-in mode, even if they have an absolute file name in the archive.
--absolute-filenames This is the default: tell cpio not to strip leading file name components that contain '..' and leading slashes from file names in copy-in mode.
--no-preserve-owner Do not change the ownership of the files; leave them owned by the user extracting them. This is the default for non-root users, so that users on System V don't inadvertently give away files. This option can be used in copy-in mode and copy-pass mode.
-o--create Run in copy-out mode. (see 'Copy-out mode').
-O archive Archive filename to use instead of standard output. To use a tape drive on another machine as the archive, use a filename that starts with 'HOSTNAME:'. The hostname can be preceded by a username and an '@' to access the remote tape drive as that user, if you have permission to do so (an entry in that user's '~/.rhosts' file).
--only-verify-crc Verify the CRC of each file in the archive, when reading a CRC format archive. Do not actually extract the files.
-p--pass-through Run in copy-pass mode. (see 'copy-pass mode').
--quiet Do not print the number of blocks copied.
-r--rename Interactively rename files.
-R [user][:.][group], --owner [user][:.][group] Set the ownership of all files created to the specified user and/or group in copy-out and copy-pass modes. Either the user, the group, or both, must be present. If the group is omitted but the ':' or '.' separator is given, use the given user's login group. Only the super-user can change files' ownership.
--rsh-command=COMMAND Notifies cpio that is should use COMMAND to communicate with remote devices.
-s--swap-bytes Swap the bytes of each halfword (pair of bytes) in the files. This option can be used in copy-in mode.
-S--swap-halfwords Swap the halfwords of each word (4 bytes) in the files. This option may be used in copy-in mode.
--sparse Write files with large blocks of zeros as sparse files. This option is used in copy-in and copy-pass modes.
-t--list Print a table of contents of the input.
--to-stdout Extract files to standard output. This option may be used in copy-in mode.
-u--unconditional Replace all files, without asking whether to replace existing newer files with older files.
-v--verbose List the files processed, or with '-t', give an 'ls -l' style table of contents listing. In a verbose table of contents of a ustar archive, user and group names in the archive that do not exist on the local system are replaced by the names that correspond locally to the numeric UID and GID stored in the archive.
-V--dot Print a '.' for each file processed.
--version Print the cpio program version number and exit.

查看英文版

查看中文版

cpio 例子

创建归档文件时,cpio从标准输入中获取要处理的文件列表,然后将归档文件发送到标准输出或“ -F ”选项定义的设备。通常使用findls将此列表提供给标准输入。在以下示例中,您可以看到归档单个目录内容的可能性:

% ls | cpio -ov > directory.cpio

“ -o ”选项创建存档,而“ -v ”选项打印文件的名称存档,因为他们补充说。请注意,这些选项可以放在单个' - '之后,也可以在命令行上单独放置。“ > ”cpio输出重定向到文件“ directory.cpio ”。

如果要归档整个目录树,则find命令可以将文件列表提供给cpio

% find . -print -depth | cpio -ov > tree.cpio

这将把当前目录中的所有文件(位于下面的目录)放入归档文件tree.cpio中。同样,“ -o ”创建了一个归档文件,“ -v ”选项为您显示了归档文件的名称(请参见“ 复制模式 ”)。使用' 。find语句中的'为您提供了恢复时的更大灵活性,因为它将通过硬连线的绝对路径使用相对路径保存文件名。' -depth '选项强制' find'在打印目录本身之前打印目录中的条目。通过在目录名称本身之前打印目录中的目录条目,可以限制限制性目录权限的影响。

提取存档需要更多的考虑,因为cpio默认情况下不会创建目录。另一个特点是,除非指定,否则不会覆盖现有文件。

% cpio -iv < directory.cpio

这将检索在文件directory.cpio中归档的文件,并将它们放在当前目录中。' -i '选项提取存档,而' -v '显示提取时的文件名。如果要处理已归档的目录树,则需要使用' -d '选项来根据需要创建目录,例如:

% cpio -idv < tree.cpio

这将获取归档文件tree.cpio的内容并将其解压缩到当前目录。如果您尝试在已经存在(并且具有相同或更高修改时间)的同名文件之上提取文件,除非-u选项告知cpio,否则cpio将不会提取该文件(请参见“ 复制”模式 ')。

在复制通过模式下,cpio将文件从一个目录树复制到另一个目录树,将复制和复制步骤组合在一起,而无需实际使用归档。它从标准输入中读取要复制的文件列表。将它们复制到的目录是一个非选项参数(请参阅“ 复制通过模式 ”)。

% find . -depth -print0 | cpio --null -pvd new-dir

该示例显示了将当前目录和子目录的文件复制到名为new-dir的新目录中。一些新选项是GNU find提供的' -print0 ',以及cpio的' --null '选项。这两个选项一起起作用,以在find和cpio之间发送文件名,即使文件名中嵌入了特殊字符也是如此。另一个是“ -p ”,它告诉cpio将找到的文件传递到目录“ new-dir ”。

find . -print | cpio -ocv > /dev/fd0

在上面,使用find命令将列出所有文件和目录,并使用cpio命令将列出的那些文件复制到软盘驱动器中。

find . -print | cpio -dumpv /home/users/hope

在上面的示例中,find命令将列出当前目录的所有文件和子目录,并将它们通过管道传送到cpio命令,该命令会将这些文件复制到希望用户帐户。

cpio -icuvd < /dev/fd0

上面的命令将从软盘恢复文件。

When creating an archive, cpio takes the list of files to be processed from the standard input, and then sends the archive to the standard output, or to the device defined by the '-F' option. Usually find or ls is used to provide this list to the standard input. In the following example you can see the possibilities for archiving the contents of a single directory:

% ls | cpio -ov > directory.cpio

The '-o' option creates the archive, and the '-v' option prints the names of the files archived as they are added. Notice that the options can be put together after a single '-' or can be placed separately on the command line. The '>' redirects the cpio output to the file 'directory.cpio'.

If you wanted to archive an entire directory tree, the find command can provide the file list to cpio:

% find . -print -depth | cpio -ov > tree.cpio

This takes all the files in the current directory, the directories below and place them in the archive tree.cpio. Again the '-o' creates an archive, and the '-v' option shows you the name of the files as they are archived (see 'Copy-out mode'). Using the '.' in the find statement gives you more flexibility when doing restores, as it will save file names with a relative path via a hard-wired, absolute path. The '-depth' option forces 'find' to print of the entries in a directory before printing the directory itself. This limits the effects of restrictive directory permissions by printing the directory entries in a directory before the directory name itself.

Extracting an archive requires a bit more thought because cpio will not create directories by default. Another characteristic, is it will not overwrite existing files unless specified.

% cpio -iv < directory.cpio

This will retrieve the files archived in the file directory.cpio and place them in the present directory. The '-i' option extracts the archive and the '-v' shows the file names as they are extracted. If you are dealing with an archived directory tree, you need to use the '-d' option to create directories as necessary, something like:

% cpio -idv < tree.cpio

This takes the contents of the archive tree.cpio and extract it to the current directory. If you try to extract the files on top of files of the same name that already exist (and have the same or later modification time) cpio will not extract the file unless told to do so by the -u option (see 'Copy-in mode').

In copy-pass mode, cpio copies files from one directory tree to another, combining the copy-out and copy-in steps without actually using an archive. It reads the list of files to copy from the standard input; the directory into which it will copy them is given as a non-option argument (see 'Copy-pass mode').

% find . -depth -print0 | cpio --null -pvd new-dir

The example shows copying the files of the present directory, and sub-directories to a new directory called new-dir. Some new options are the '-print0' available with GNU find, combined with the '--null' option of cpio. These two options act together to send file names between find and cpio, even if special characters are embedded in the file names. Another is '-p', which tells cpio to pass the files it finds to the directory 'new-dir'.

find . -print | cpio -ocv > /dev/fd0

Above, using the find command would list all files and directories and using the cpio command copy those files listed to the floppy drive.

find . -print | cpio -dumpv /home/users/hope

In the above example, the find command would list all files and subdirectories of the current directory, and pipe them to the cpio command, which copies those files to the hope user account.

cpio -icuvd < /dev/fd0

The above command would restore the files back from the floppy.

查看英文版

查看中文版