Linux cpio Command
The Linux cpio command is used for backing up files.
cpio is a tool for creating and restoring backup archives. It can add to, extract from, or list the contents of cpio or tar backup archives.
Syntax
cpio [-0aABckLovV][-C <input/output size>][-F <backup file>][-H <backup format>][-O <backup file>][--block-size=<block size>][--force-local][--help][--quiet][--version] or cpio [-bBcdfikmnrsStuvV][-C <input/output size>][-E <pattern file>][-F <backup file>][-H <backup format>][-I <backup file>][-M <return message>][-R <owner><:/.><group>][--block-size=<block size>][--force-local][--help][--no-absolute-filenames][--no-preserve-owner][--only-verify-crc][--quiet][--sparse][--version][pattern...] or cpio [-0adkiLmpuvV][-R <owner><:/.><group>][--help][--no-preserve-owner][--quiet][--sparse][--version][destination directory]
Parameters:
-0 or --null: Accepts newline control characters, typically used with the find command's "-print0" parameter.
-a or --reset-access-time: Resets the access time of the file.
-A or --append: Appends to an existing backup file, which must be on a disk and not on a tape drive.
-b or --swap: This parameter has the same effect as specifying both "-sS" parameters.
-B: Changes the input/output block size to 5210 Bytes.
-c: Uses the old ASCII backup format.
-C<block size> or --io-size=<block size>: Sets the input/output block size in Bytes.
-d or --make-directories: cpio will create directories if needed.
-E<pattern file> or --pattern-file=<pattern file>: Specifies a pattern file containing one or more patterns, allowing cpio to extract files matching the patterns, with each pattern on a new line.
-f or --nonmatching: Extracts all files that do not match the pattern.
-F<backup file> or --file=<backup file>: Specifies the name of the backup file, replacing standard input or output, and allowing network access to another host's storage device for the backup file.
-H<backup format>: Specifies the file format to use for backup.
-i or --extract: Executes the copy-in mode to restore the backup file.
-l<backup file>: Specifies the name of the backup file, replacing standard input, and allowing network access to another host's storage device to read the backup file.
-k: This parameter is ignored and is used to resolve compatibility issues between different versions of cpio.
-l or --link: Uses hard links instead of copying files, applicable in copy-pass mode.
-L or --dereference: Copies the original file instead of creating a symbolic link.
-m or --preserve-modification-time: Does not change the modification time of the file.
-M<return message> or --message=<return message>: Sets the message for changing storage media.
-n or --numeric-uid-gid: When using the "-tv" parameter to list the contents of the backup file, adding "-n" will list the files with user and group IDs instead of owner and group names.
-o or --create: Executes the copy-out mode to create a backup file.
-O<backup file>: Specifies the name of the backup file, replacing standard output, and allowing network access to another host's storage device to store the backup file.
-p or --pass-through: Executes the copy-pass mode, skipping the backup step and directly copying files to the destination directory.
-r or --rename: Uses interactive mode when filenames need to be changed.
-R<owner><:/.><group> or --owner<owner><:/.><group>: Specifies the owner and group of the backup or copied files in copy-in mode or copy-pass mode.
-s or --swap-bytes: Swaps the content of each pair of bytes.
-S or --swap-halfwords: Swaps the content of each half-word.
-t or --list: Displays the input content.
-u or --unconditional: Replaces all files without asking, regardless of their date and time.
-v or --verbose: Display detailed execution process of the command.
-V or --dot: Prefix each file's execution program with a "." when executing the command.
--block-size=<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 the backup file to be stored on the local host.
--help: Online help.
--no-absolute-filenames: Create file names using relative paths.
--no-preserve-owner: Do not preserve file ownership; the files belong to whoever extracts the backup.
--only-verify-crc: When the backup file uses CRC format, this option checks if each file inside the backup is correct.
--quiet: Do not display the number of blocks copied.
--sparse: If a file contains a large sequence of consecutive zero bytes, store it as a sparse file.
--version: Display version information.
Example
Creating a backup file
[[email protected] var]# ll // Display files in the current directory
total 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
[[email protected] var]# ls | cpio -o >123.cpio // Create a backup file
25 blocks
[[email protected] var]# ll // Display files in the current directory
total 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 May 8 15:25 lock drwxr-xr-x 14 root root 4096 May 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 May 8 15:14 run drwxr-xr-x 16 root root 4096 2008-03-30 spool drwxrwxrwt 3 root root 4096 Jan 13 18:53 tmp drwx------ 2 root root 4096 2004-07-08 tux drwxr-xr-x 8 root root 4096 Jan 19 19:39 www drwxr-xr-x 3 root root 4096 2008-03-30 yp [[email protected] var]#
Extract backup file
[[email protected] var]# ls | cpio -i -l 123.cpio
Extract backup file and list detailed information
[[email protected] var]# cpio -t -I 123.cpio 123.cpio a.c b.c c.c ……omitted part of the result
Force extraction
[[email protected] var]# cpio -i -u -I 123.cpio
Extract with reverse matching, specifying files not to extract
[[email protected] var]# cpio -i -I 123.cpio -f *.c
//Do not extract files ending with .c
Add files to the specified .cpio file
[[email protected] 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 [[email protected] var]# cpio -o -O 123.cpio -A db //user input, press Ctrl+D to end input 1 block [[email protected] var]#
Backup files from standard input
[[email protected] test]# ls a. a.c b.c c.c d.c f.c [[email protected] test]# cpio -o >123.cpio a.c //user input b.c c.c //press Ctrl+D to complete input 3 block [[email protected] test]#
Copy files
[[email protected] test]# cpio -p /root a.c //user input b.c c.c //press Ctrl+D to complete input 3 block ```