1.1 cpio Basic introduction

cpio It's a very old archive tool . Has been gradually tar replace , But some of the features are tar 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 supports tar Archive in format , But for the compressed tar( 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 : In 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 . stay copy-out In mode , The most typical is the use of find To specify the files to be archived , in use find Time , Better add "-depth" To minimize possible problems , For example, the permission of directory


Copy-in pattern : In 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 . arbitrarily cpio The non option parameters of will be considered as shell Of glob General distribution pattern, Only when the file name matches the specified pattern will it be extracted from it or list come out . stay cpio 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 : In 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 The pattern is copy-in Mode again copy-out Combination of modes , It doesn't involve any archiving behavior . This is cpio 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


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

Option description :

-o:(--create) Specify run as copy-out pattern , Archive Mode

-i:(--extract) Specify run as copy-in pattern , Extraction mode

-p:(--pass-through) Specify run as copy-pass pattern , Directory copy mode

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

 

-B: set up I/O Of block Size is 5120 byte , Default is 512 byte

-C IO-SIZE:(--io-size=IO-SIZE) appoint I/O Of block Size is IO-SIZE size

 

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

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

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

               : be careful copy-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 , Use only -F or -O Specify Archive , It can only be used in copy-out In mode .
Not equivalent to redirect symbol ">>",">>" No additional files found in archive after

--to-stdout: Extract files to standard output , For copy-in pattern

 

-E FILE:(--pattern-file=FILE) from FILE Read in pattern Match , Matches will be decompressed or listed , For copy-in pattern .

-f:(--nonmatching) only copy Mismatch specified pattern Documents of

 

-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's atime, That is, keep the original file atime

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

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

 

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

--quiet: When the copy is not output block Quantity information

-v: Give details

1.3 Example

(1). Archive all files in home directory to tree.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 using find 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 , take find All files in the home directory are archived to tree.cpio, but tree.cpio It will also be in the home directory , It will also be find Search for elements , So it will also be filed in itself . Maybe there will be some confusion here , It's not supposed to be find Do not process until it is finished cpio 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 not find Just search . You can find Exclude from , stay cpio Exclude or archive files in other directories .
[[email protected] ~]# find ~ -depth | cpio -ov -F /tmp/tree.cpio
Generally for accuracy , Will be in find Use on "-print0", And then cpio Use on "--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 <
tree.cpio
This will be listed recursively tree.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 list tree.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 ? stay cpio 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 as pattern.

(3). Append files to archive .


be careful , Do not use redirection symbols ">>" Add , 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 /root/new.txt The following file is appended to the tree.cpio in , because tree.cpio Already in /root catalog , So the path after appending is tree.cpio In /root/new.txt.

And if tree.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 tree.cpio Add one in /boot catalog , It and /root Directory is the same level .

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

(4). Extract file .

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/ Next new.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 , Use 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 , Namely copy-pass pattern .


be careful , In this mode, the replicated directory exists as a subdirectory in the target location . for example , For example, copy /root Catalog to /tmp/abc lower , Then in /tmp/abc There will be root Subdirectory , stay /tmp/abc/root Next is the source /root Files 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