Linux cpio Command
The Linux cpio command is used to back up files.
cpio is a utility for creating and restoring archives; it can add to or extract files from cpio or tar archives.
Syntax
cpio [-0aABckLovV][-C <输入/输出大小>][-F <备份档>][-H <备份格式>][-O <备份档>][--block-size=<区块大小>][--force-local][--help][--quiet][--version] 或 cpio [-bBcdfikmnrsStuvV][-C <输入/输出大小>][-E <范本文件>][-F <备份档>][-H <备份格式>][-I <备份档>][-M <回传信息>][-R <拥有者><:/.><所属群组>][--block-size=<区块大小>][--force-local][--help][--no-absolute-filenames][--no-preserve-owner][--only-verify-crc][--quiet][--sparse][--version][范本样式...] 或 cpio [-0adkiLmpuvV][-R <拥有者><:/.><所属群组>][--help][--no-preserve-owner][--quiet][--sparse][--version][目的目]
Parameters:
- -0 or --null Accept the newline control character, usually used with the "-print0" option of the find command.
- -a or --reset-access-time Reset the access time of files.
- -A or --append Append to an existing archive, which must be stored on disk rather than on tape.
- -b or --swap This option has the same effect as specifying the "-sS" options together.
- -B Change the input/output block size to 5210 bytes.
- -c Use the old ASCII archive format.
- -C
or --io-size= Set the input/output block size, in bytes. - -d or --make-directories Create directories automatically if needed.
- -E or --pattern-file= Specify a template file containing one or more patterns, so that cpio extracts files matching the pattern conditions; the format is one pattern per line.
- -f or --nonmatching Make cpio extract all files that do not match the pattern conditions.
- -F
or --file= Specify the name of the archive file to replace standard input or output; this also allows accessing the archive via a storage device on another host over the network. - -H
Specify the file format to be used for the archive. - -i or --extract Run in copy-in mode to restore an archive.
- -l
Specify the name of the archive file to replace standard input; this also allows reading the archive via a storage device on another host over the network. - -k This option is ignored and only serves to resolve compatibility issues between different versions of cpio.
- -l or --link Use hard links instead of copying files; can be used in copy-pass mode.
- -L or --dereference Do not create symbolic links; instead, copy the original file that the link points to.
- -m or preserve-modification-time Do not change the file's modification time.
- -M
or --message= Set the message for changing storage media. - -n or --numeric-uid-gid When using the "-tv" option to list the contents of an archive, adding "-n" lists the file list with user IDs and group IDs instead of owner and group names.
- -o or --create Run in copy-out mode to create an archive.
- -O
Specify the name of the archive file to replace standard output; this also allows storing the archive via a storage device on another host over the network. - -p or --pass-through Run in copy-pass mode, skipping the archive step and copying files directly to the destination directory.
- -r or --rename Use interactive mode when a filename needs to be changed.
- -R
<:/.> or - ----owner
<:/.> When restoring an archive in copy-in mode or copying files in copy-pass mode, you can specify the owner and group for these archived and copied files. - -s or --swap-bytes Swap the contents of each pair of bytes.
- -S or --swap-halfwords Swap the contents of each halfword.
- -t or --list Display the contents of the input.
- -u or --unconditional Replace all files, regardless of whether their dates and times are newer or older, overwriting them without asking.
- -v or --verbose Display the execution process of the command in detail.
- -V or --dot When executing the command, prefix a "." to each file processed.
- --block-size=
Set the input/output block size. If the value is set to 5, the block size is 2500; if set to 10, the block size is 5120, and so on. - --force-local Force archives to be stored on the local host.
- --help Online help.
- --no-absolute-filenames Create filenames using relative paths.
- --no-preserve-owner Do not preserve the file owner; whoever extracts the archive owns those files.
- -only-verify-crc When the archive uses the CRC format, this option can check that each file in the archive is correct.
- --quiet Do not display how many blocks were copied.
- --sparse If a file contains a large number of consecutive zero bytes, store the file as a sparse file.
- --version Display version information.
Example
Create a backup file
[root@runoob.com var]# ll //显示当前目录下的文件
总用量 164
drwxr-xr-x 2 root root 4096 2008-03-30 account
drwxr-xr-x 9 root root 4096 2008-03-30 cache
drwxr-xr-x 3 netdump netdump 4096 2008-03-30 crash
drwxr-xr-x 3 root root 4096 2008-03-30 db
drwxr-xr-x 3 root root 4096 2008-03-30 empty
drwxr-xr-x 3 root root 4096 2008-03-30 ftp
drwxrwx--T 2 root gdm 4096 4月 9 20:17 gdm
drwxr-xr-x 25 root root 4096 2008-03-30 lib
drwxr-xr-x 2 root root 4096 2004-08-13 local
drwxrwxr-x 6 root lock 4096 5月 8 15:25 lock
drwxr-xr-x 14 root root 4096 5月 8 15:14 log
lrwxrwxrwx 1 root root 10 2008-03-30 mail -> spool/mail
drwxr-xr-x 2 root root 4096 2004-08-13 nis
drwxr-xr-x 2 root root 4096 2004-08-13 opt
drwxr-xr-x 2 root root 4096 2004-08-13 preserve
drwxr-xr-x 16 root root 4096 5月 8 15:14 run
drwxr-xr-x 16 root root 4096 2008-03-30 spool
drwxrwxrwt 3 root root 4096 1月 13 18:53 tmp
drwx------ 2 root root 4096 2004-07-08 tux
drwxr-xr-x 8 root root 4096 1月 19 19:39 www
drwxr-xr-x 3 root root 4096 2008-03-30 yp
[root@runoob.com var]# ls | cpio -o >123.cpio //制作备份文件
25 blocks
[root@runoob.com var]# ll //显示当前目录下的文件
总用量 172
-rw-r--r-- 1 root root 1024 5月 24 13:06 123.cpio
drwxr-xr-x 2 root root 4096 2008-03-30 account
drwxr-xr-x 9 root root 4096 2008-03-30 cache
drwxr-xr-x 3 netdump netdump 4096 2008-03-30 crash
drwxr-xr-x 3 root root 4096 2008-03-30 db
drwxr-xr-x 3 root root 4096 2008-03-30 empty
drwxr-xr-x 3 root root 4096 2008-03-30 ftp
drwxrwx--T 2 root gdm 4096 4月 9 20:17 gdm
drwxr-xr-x 25 root root 4096 2008-03-30 lib
drwxr-xr-x 2 root root 4096 2004-08-13 local
drwxrwxr-x 6 root lock 4096 5月 8 15:25 lock
drwxr-xr-x 14 root root 4096 5月 8 15:14 log
lrwxrwxrwx 1 root root 10 2008-03-30 mail -> spool/mail
drwxr-xr-x 2 root root 4096 2004-08-13 nis
drwxr-xr-x 2 root root 4096 2004-08-13 opt
drwxr-xr-x 2 root root 4096 2004-08-13 preserve
drwxr-xr-x 16 root root 4096 5月 8 15:14 run
drwxr-xr-x 16 root root 4096 2008-03-30 spool
drwxrwxrwt 3 root root 4096 1月 13 18:53 tmp
drwx------ 2 root root 4096 2004-07-08 tux
drwxr-xr-x 8 root root 4096 1月 19 19:39 www
drwxr-xr-x 3 root root 4096 2008-03-30 yp
[root@runoob.com var]#
Extract a backup file
[root@runoob.com var]# ls | cpio -i -l 123.cpio
Decompress a backup file and list detailed information
[root@runoob.com var]# cpio -t -I 123.cpio
123.cpio
a.c
b.c
c.c
……省略部分结果
Force decompression
[root@runoob.com var]# cpio -i -u -I 123.cpio
Perform reverse matching during decompression to specify files not to be decompressed
[root@runoob.com var]# cpio -i -I 123.cpio -f *.c
//Do not decompress.c-ending files
Add files to the specified .cpio file
[root@runoob.com var]# ls
123.cpio crash ftp local mail preserve tmp yp
account db gdm lock nis run tux
cache empty lib log opt spool www
[root@runoob.com var]# cpio -o -O 123.cpio -A
db //用户输入 按下Ctrl+D结束输入
1 block
[root@runoob.com var]#
Back up files from standard input
[root@runoob.com test]# ls
a. a.c b.c c.c d.c f.c
[root@runoob.com test]# cpio -o >123.cpio
a.c //用户输入
b.c
c.c //按下Ctrl+D完成输入
3 block
[root@runoob.com test]#
Copy a file
[root@runoob.com test]# cpio -p /root
a.c //用户输入
b.c
c.c //按下Ctrl+D完成输入
3 block
Linux Command Reference