1.1 cpio Basic introduction

cpio It's a very old archive tool. Has gradually beentar Replace, But some of the features aretar Nonexistent, So share its usage.

cpio - copy files to and from archives

cpio Used to create, Unzip Archive, You can also copy the archive file in and out, I.e. append files to the archive, Or extract files from archive. It also supportstar Archive in format, But for the compressedtar( as.tar.gz format) I can't support it,cpio After all, the corresponding( solution) Compression tool.

cpio Data is generally obtained from standard input, Write to standard output, So it is usually combined with pipeline, input redirection , Output redirection using.

cpio There are three modes of operation:

Copy-out Pattern: Under this mode,cpio Copy file to archive, Archive now, So it becomes an archive model. It reads the files to be archived from standard input, Archive them to the target, If the target of the archive is not specified, Archive to standard output. staycopy-out Mode, The most typical is the use offind To specify the files to be archived, in usefind Time, Better add"-depth" To minimize possible problems, For example, the permission of directory

Copy-in Pattern: Under this mode,cpio Files will be extracted from the archive, Or list the files in the archive. It will read the archive from standard input. Arbitrarilycpio The non option parameters of will be considered asshell Ofglob Pass throughpattern, Only when the file name matches the specified pattern will it be extracted from it orlist come out. staycpio in, Wildcard cannot match to"." or"/", So if necessary, Must be specified explicitly"." or"/". If no match pattern is specified, Extract or list all files.

Copy-pass Pattern: Under this mode,cpio Copy a tree( All files in the directory) Go to another directory, And exists in the target directory as a subdirectory with the same name.copy-pass Pattern iscopy-in Mode recopy-out Combination of modes, It doesn't involve any archiving behavior. This iscpio A special use of. It reads the content to be copied from the standard input, Then copy them to the target path.

1.2 cpio Command instructions

aboutcpio For this archive tool, It differs from other commands in one way: staycpio The non option parameters given on the command line are considered to bepattern. Non option parameter means that this parameter is not the one specified for the option. ascpio
-t abc <a.cpio in,"-t" The function of the option is to list files, It doesn't need parameters, But it's given laterabc, List matchesabc Files or directories for.

Option description:

-o:(--create) Specify run ascopy-out Pattern, Archive Mode

-i:(--extract) Specify run ascopy-in Pattern, Extraction mode

-p:(--pass-through) Specify run ascopy-pass Pattern, Directory copy mode

-t:(--list) List files in archive


-B: Set upI/O Ofblock Size is5120 byte, Default is512 byte

-C IO-SIZE:(--io-size=IO-SIZE) AppointI/O Ofblock Size isIO-SIZE Size


-I archive: Replaces standard input with the specified archive file name( Read from standard input, So it must be decompression mode, Namelycopy-in)

-O archive: Replaces standard output with the specified archive file name( Output to standard output, So it must be the archive mode, Namelycopy-out)

archive:(--file=archive) Replaces standard input or output with the specified archive file name. So whether it'scopy-in stillcopy-out All modes can be used-F Specify Archive

               : Be carefulcopy-out I.e. in Archive Mode, Its default behavior is equivalent to the redirection symbol">", So the content will be completely covered, But Archive(inode) Unchanged

-A:(--append) Append files to an existing archive, Only use-F or-O Specify Archive, Can only be used incopy-out Mode.
Not equivalent to redirect symbol">>",">>" No additional files found in archive after

--to-stdout: Extract files to standard output, Use incopy-in Pattern


-E FILE:(--pattern-file=FILE) fromFILE Read inpattern Matching, Matches will be decompressed or listed, Use incopy-in Pattern.

-f:(--nonmatching) onlycopy Mismatch specifiedpattern Documents


-u:(--unconditional) When there is a file with the same name in the target, Force replace conflict file

-a:(--reset-access-time) Reset the file'satime, That is, keep the original fileatime

-m:(--preserve-modification-time) Keep the original of the filemtime

-d:(-make-directories) Automatically create directories when needed


-0:(--null) Parse empty string\0

--quiet: When the copy is not outputblock Quantitative information

-v: Give details

1.3 Example

(1). Archive all files in home directory totree.cpio in.
[[email protected] ~]# find ~ -depth | cpio -ov > tree.cpio or [[email protected] ~]# find
~ -depth | cpio -ov -F tree.cpio
But be very careful, If usedfind search, When the archive and search directory are the same path, It will also archive the archive itself into the archive, That's iterative archiving.

For example, in the above example, takefind All files in the home directory are archived totree.cpio, buttree.cpio It will also be in the home directory, It will also befind Search for vegetarian products, So it will also be filed in itself. Maybe there will be some confusion here, Should notfind Do not process until it is finishedcpio Do you? This is not so. The role of pipeline is only the role of data transfer between processes, But it's not necessary to wait until the processing on the left side of the pipeline is completed, It's the left-hand side processing that passes to the right-hand side, If it's too fast on the left, Causing pipeline congestion, The program on the left side of the pipeline will wait for the pipeline to have free space.

To solve the problem of iterative archiving, As long as the archive is notfind Just search. Can be infind Exclude, staycpio Exclude or archive files in other directories.
[[email protected] ~]# find ~ -depth | cpio -ov -F /tmp/tree.cpio
Generally for accuracy, Will be infind Upper use"-print0", Then incpio Upper use"--null" Resolve empty characters.
[[email protected] ~]# find ~ -depth -print0 | cpio --null -ov -F /tmp/tree.cpio
(2). List files in archive.
[[email protected] tmp]# cpio -t -F tree.cpio or [[email protected] tmp]# cpio -t <
This will be listed recursivelytree.cpio All files in, So that's not what you want, A match pattern should be specified for it.
[[email protected] tmp]# cpio -t -F tree.cpio /root/* or [[email protected] tmp]# cpio -t
/root/* < tree.cpio
This will listtree.cpio All in/root/ Contents under the directory, But obviously,"*" The number cannot match the hidden file at the beginning of the point, So hidden files are not shown.
[[email protected] tmp]# cpio -t -F tree.cpio /root/.*
But only hidden files will be listed. How to list hidden files, Common files are also listed? staycpio China doesn't seem to directly support such generic configuration, Here's a way I came up with.
[[email protected] tmp]# cpio -t -F tree.cpio /root/{.*,*}
Be careful,cpio Any non option arguments on the command line are treated aspattern.

(3). Append files to archive.

Be careful, Do not use redirection symbols">>" Append, Although the final typing of the archive file is successful, But actually you don't know where it's going, We couldn't find anything to add. therefore, Still use"-A" option.
[[email protected] tmp]# ls /root/new.txt | cpio -oA -F tree.cpio

This will bring/root/new.txt The following file is appended to thetree.cpio in, Becausetree.cpio Already in/root Catalog, So the path after appending istree.cpio Medium/root/new.txt.

And iftree.cpio There is no directory in which to append files, A new directory will be created.
[[email protected] tmp]# find /boot -depth -print0 | cpio -oA -F tree.cpio
This will be intree.cpio Add one in/boot Catalog, Its sum/root Directory is the same level.

It seems that there is no way to directly specify the additional target path.

(4). Extract files.

Be careful:cpio Only files can be extracted to the current directory.
[[email protected] tmp]# cpio -idv -F tree.cpio /root/ new.txt cpio: new.txt not
created: newer or same age version exists

This will extract/root/ Lowernew.txt To current directory, Obviously, Here's a mistake, Prompt that there is already a file with the same name under the current directory that needs to be updated by the file to be extracted, So there was no extraction. If you want to forcibly withdraw, Usage function"-u" option, This will overwrite existing actions.
[[email protected] tmp]# cpio -idvu -F tree.cpio new.txt
actually, As long as it's a file with the same name, Whether or not its timestamp is newer than the one to be extracted, No extraction, Unless forced.

Here"-d" Option to extract if the leading directory does not exist, Automatically created. The existing directory will not be affected anyway, therefore"-i" It's usually added"-d".

(5). Directory file copy, Namelycopy-pass Pattern.

Be careful, In this mode, the replicated directory exists as a subdirectory in the target location. for example, Copy for example/root Directory to/tmp/abc lower, While in/tmp/abc There will beroot Subdirectory, stay/tmp/abc/root The bottom is the source./root Documents in.
[[email protected] tmp]# find ~ -depth -print0 | cpio --null -pvd /tmp/abc
[[email protected] tmp]# ll/tmp/abc dr-xr-x--- 6 root root 4096 Jun 13 09:45 root