Linux cpio command

Linux 命令大全Linux Command Collection

The Linux cpio command is used to back up files.

cpio is a utility for creating and restoring backup archives. It can add files to or extract files from cpio or tar backup 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][目的目]

Options:

  • -0 or --null: Accept newline control characters, 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 backup archive, and this archive must be stored on disk, not on a tape drive.
  • -b or --swap: This option has the same effect as specifying the '-sS' option simultaneously.
  • -B: Change the input/output block size to 5210 Bytes.
  • -c: Use the old ASCII backup format.
  • -C <block size> or --io-size=<block size>: Set the input/output block size in Bytes.
  • -d or --make-directories: cpio will create directories as needed.
  • -E <pattern file> or --pattern-file=<pattern file>: Specify a pattern file containing one or more pattern styles, allowing cpio to extract files that match the pattern conditions. The format is one pattern style per line.
  • -f or --nonmatching: Let cpio extract all files that do not match the pattern conditions.
  • -F <archive> or --file=<archive>: Specify the name of the backup archive to replace standard input or output. This can also be used to access the archive on another host's storage device over the network.
  • -H <backup format>: Specify the file format to be used for backup.
  • -i or --extract: Execute copy-in mode to restore the backup archive.
  • -l <archive>: Specify the name of the backup archive to replace standard input, and this can also be used to read the archive from another host's storage device over the network.
  • -k: This option is ignored and not processed; it is only used to resolve compatibility issues between different cpio versions.
  • -l or --link: Replace file copying with hard links; can be used in copy-pass mode.
  • -L or --dereference: Do not create symbolic links; directly copy the original file that the link points to.
  • -m or --preserve-modification-time: Do not change the modification time of files.
  • -M <message> or --message=<message>: Set the message for changing storage media.
  • -n or --numeric-uid-gid: When using the '-tv' option to list the contents of a backup archive, adding the '-n' option will list the files with user ID and group ID instead of owner and group names.
  • -o or --create: Execute copy-out mode to create a backup archive.
  • -O <archive>: Specify the name of the backup archive to replace standard output. It can also be used to store the archive on another host's storage device over the network.
  • -p or --pass-through: Execute copy-pass mode, skipping the backup step, and directly copying files to the destination directory.
  • -r or --rename: Use interactive mode when file names need to be changed.
  • -R <owner><:/.><group> or
  • --owner <owner><:/.><group>: When restoring an archive in copy-in mode or copying files in copy-pass mode, you can specify the owner and group of these backed up or 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 the date/time is newer or older, and overwrite directly without asking.
  • -v or --verbose: Display detailed information about the command execution process.
  • -V or --dot: When executing the command, add a '.' before the processing of each file.
  • --block-size=<block size>: Set the input/output block size. For example, 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 the backup archive to be stored on the local host.
  • --help: Online help.
  • --no-absolute-filenames: Use relative paths to create file names.
  • --no-preserve-owner: Do not preserve file ownership; whoever extracts the archive owns the files.
  • --only-verify-crc: When the backup archive uses the CRC backup format, this option can be used to check whether 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.

Examples

Create a backup file

[root@example.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@example.com var]# ls | cpio -o >123.cpio //制作备份文件
25 blocks
[root@example.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@example.com var]# 

Extract a backup file

[root@example.com var]# ls | cpio -i -l 123.cpio

Extract a backup file and list detailed information

[root@example.com var]# cpio -t -I 123.cpio  
123.cpio
a.c
b.c
c.c
……省略部分结果

Force extraction

[root@example.com var]# cpio -i -u -I 123.cpio 

Perform reverse matching during extraction, specifying files not to extract

[root@example.com var]# cpio -i -I 123.cpio -f *.c 

//不解压.c结尾的文件

Add files to the specified .cpio file

[root@example.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@example.com var]# cpio -o -O 123.cpio -A
db  //用户输入 按下Ctrl+D结束输入
1 block
[root@example.com var]# 

Back up files from standard input

[root@example.com test]# ls              
a. a.c b.c c.c d.c    f.c
[root@example.com test]# cpio -o >123.cpio
a.c  //用户输入
b.c
c.c  //按下Ctrl+D完成输入
3 block
[root@example.com test]# 

Copy files

[root@example.com test]# cpio -p /root
a.c  //用户输入
b.c
c.c  //按下Ctrl+D完成输入
3 block

Linux 命令大全Linux Command Collection

Other extensions