Linux cpio command
\nThe Linux cpio command is used for backing up files.
cpio is a utility program used to create and restore backup archives. It can add, extract, or list files within cpio or tar backup archives.
Syntax
\n\ncpio [-C <Input/Output Size>][-F <Archive>][-H <Archive Format>][-O <Archive>][--block-size=<Block Size>]\nor\ncpio [-C <Input/Output Size>][-E <Pattern File>][-F <Archive>][-H <Archive Format>][-I <Archive>][-M <Message>][-R <Owner><:/.><Group>][--block-size=<Block Size>][Pattern...]\nor\ncpio [-R <Owner><:/.><Group>]\n\n
Parameters:
\n- \n
-0or--null: Accepts new line control characters, typically used with the"-print0"parameter of thefindcommand. \n-aor--reset-access-time: Resets the access time of files. \n-Aor--append: Appends to an existing archive. The archive must reside on disk, not on a tape drive. \n-bor--swap: The effect of this parameter is the same as specifying both"-sS"parameters. \n-B: Sets the input/output block size to 5120 Bytes. \n-c: Uses the old ASCII backup format. \n-C<Block Size>or--io-size=<Block Size>: Sets the input/output block size in Bytes. \n-dor--make-directories: Creates directories as needed. \n-E<Pattern File>or--pattern-file=<Pattern File>: Specifies a pattern file containing one or more patterns, allowingcpioto extract files matching the pattern conditions. The format is one pattern per line. \n-for--nonmatching: Makescpioextract all files that do NOT match the pattern conditions. \n-F<Archive>or--file=<Archive>: Specifies the archive name to use instead of standard input or output, allowing access to archives on another host's storage device over the network. \n-H<Archive Format>: Specifies the file format to use during backup. \n-ior--extract: Executes copy-in mode, restoring the backup archive. \n-I<Archive>: Specifies the archive name to use instead of standard input, allowing reading archives from another host's storage device over the network. \n-k: This parameter is ignored and only handles compatibility issues between differentcpioversions. \n-lor--link: Creates hard links instead of copying files; can be used in copy-pass mode. \n-Lor--dereference: Does not create symbolic links; directly copies the original file that the link points to. \n-mor--preserve-modification-time: Does not change the modification time of files. \n-M<Message>or--message=<Message>: Sets the message for changing storage media. \n-nor--numeric-uid-gid: When listing archive contents with the"-tv"parameter, adding"-n"will list files using user ID and group ID instead of owner and group names. \n-oor--create: Executes copy-out mode, creating a backup archive. \n-O<Archive>: Specifies the archive name to use instead of standard output, allowing storage on another host's storage device over the network. \n-por--pass-through: Executes copy-pass mode, skipping the backup step and directly copying files to the destination directory. \n-ror--rename: Uses interactive mode when filenames need to be changed. \n-R<Owner><:/.><Group>or--owner<Owner><:/.><Group>: In copy-in mode when restoring archives, or copy-pass mode when copying files, this specifies the owner and group for the restored/copied files. \n-sor--swap-bytes: Swaps the contents of each pair of bytes. \n-Sor--swap-halfwords: Swaps the contents of each halfword (2 bytes). \n-tor--list: Lists the input contents. \n-uor--unconditional: Replaces all files unconditionally, regardless of date/time, overwriting without asking. \n-vor--verbose: Displays the execution process of the command in detail. \n-Vor--dot: Adds a "." before each file during command execution. \n--block-size=<Block Size>: Sets the input/output block size. If set to 5, block size is 2500; if set to 10, block size is 5120, and so on. \n--force-local: Forces the archive to be stored on the local host. \n--help: Displays online help. \n--no-absolute-filenames: Uses relative paths to create filenames. \n--no-preserve-owner: Does not preserve the file owner; whoever extracts the archive owns the files. \n--only-verify-crc: When the archive uses the CRC backup format, this parameter checks whether each file in the archive is correct. \n--quiet: Does not display how many blocks were copied. \n--sparse: If a file contains many consecutive zero bytes, stores it as a sparse file. \n--version: Displays version information. \n
Examples
\nCreate a backup file:
\n\n[root@.com var]# ll //List files in current directory\ntotal 164\ndrwxr-xr-x 2 root root 4096 2008-03-30 account\ndrwxr-xr-x 9 root root 4096 2008-03-30 cache\ndrwxr-xr-x 3 netdump netdump 4096 2008-03-30 crash\ndrwxr-xr-x 3 root root 4096 2008-03-30 db\ndrwxr-xr-x 3 root root 4096 2008-03-30 empty\ndrwxr-xr-x 3 root root 4096 2008-03-30 ftp\ndrwxrwx--T 2 root gdm 4096 Apr 9 20:17 gdm\ndrwxr-xr-x 25 root root 4096 2008-03-30 lib\ndrwxr-xr-x 2 root root 4096 2004-08-13 local\ndrwxrwxr-x 6 root lock 4096 May 8 15:25 lock\ndrwxr-xr-x 14 root root 4096 May 8 15:14 log\nlrwxrwxrwx 1 root root 10 2008-03-30 mail -> spool/mail\ndrwxr-xr-x 2 root root 4096 2004-08-13 nis\ndrwxr-xr-x 2 root root 4096 2004-08-13 opt\ndrwxr-xr-x 2 root root 4096 2004-08-13 preserve\ndrwxr-xr-x 16 root root 4096 May 8 15:14 run\ndrwxr-xr-x 16 root root 4096 2008-03-30 spool\ndrwxrwxrwt 3 root root 4096 Jan 13 18:53 tmp\ndrwx------ 2 root root 4096 2004-07-08 tux\ndrwxr-xr-x 8 root root 4096 Jan 19 19:39 www\ndrwxr-xr-x 3 root root 4096 2008-03-30 yp\n[root@.com var]# ls | cpio -o >123.cpio //Create backup file\n25 blocks\n[root@.com var]# ll //List files in current directory\ntotal 172\n-rw-r--r-- 1 root root 1024 May 24 13:06 123.cpio\ndrwxr-xr-x 2 root root 4096 2008-03-30 account\ndrwxr-xr-x 9 root root 4096 2008-03-30 cache\ndrwxr-xr-x 3 netdump netdump 4096 2008-03-30 crash\ndrwxr-xr-x 3 root root 4096 2008-03-30 db\ndrwxr-xr-x 3 root root 4096 2008-03-30 empty\ndrwxr-xr-x 3 root root 4096 2008-03-30 ftp\ndrwxrwx--T 2 root gdm 4096 Apr 9 20:17 gdm\ndrwxr-xr-x 25 root root 4096 2008-03-30 lib\ndrwxr-xr-x 2 root root 4096 2004-08-13 local\ndrwxrwxr-x 6 root lock 4096 May 8 15:25 lock\ndrwxr-xr-x 14 root root 4096 May 8 15:14 log\nlrwxrwxrwx 1 root root 10 2008-03-30 mail -> spool/mail\ndrwxr-xr-x 2 root root 4096 2004-08-13 nis\ndrwxr-xr-x 2 root root 4096 2004-08-13 opt\ndrwxr-xr-x 2 root root 4096 2004-08-13 preserve\ndrwxr-xr-x 16 root root 4096 May 8 15:14 run\ndrwxr-xr-x 16 root root 4096 2008-03-30 spool\ndrwxrwxrwt 3 root root 4096 Jan 13 18:53 tmp\ndrwx------ 2 root root 4096 2004-07-08 tux\ndrwxr-xr-x 8 root root 4096 Jan 19 19:39 www\ndrwxr-xr-x 3 root root 4096 2008-03-30 yp\n[root@.com var]#\n\n
Extract a backup file:
\n\n[root@.com var]# ls | cpio -i -l 123.cpio\n\n
Extract backup file and list detailed information:
\n\n[root@.com var]# cpio -t -I 123.cpio\n123.cpio\na.c\nb.c\nc.c\nβ¦β¦Partial results omitted\n\n
Force extraction:
\n\n[root@.com var]# cpio -i -u -I 123.cpio\n\n
Perform reverse matching during extraction, specify files not to extract:
\n\n[root@.com var]# cpio -i -I 123.cpio -f *.c //Do not extract files ending with .c\n\n
Add files to a specified .cpio file:
\n\n[root@.com var]# ls\n123.cpio crash ftp local mail preserve tmp yp\naccount db gdm lock nis run tux cache empty lib log opt spool www\n[root@.com var]# cpio -o -O 123.cpio -A db //User input, press Ctrl+D to end\n1 block\n[root@.com var]#\n\n
Backup files from standard input:
\n\n[root@.com test]# ls\na. a.c b.c c.c d.c f.c\n[root@.com test]# cpio -o >123.cpio\na.c //User input\nb.c\nc.c //Press Ctrl+D to complete input\n3 block\n[root@.com test]#\n\n
Copy files:
\n\n[root@.com test]# cpio -p /root\na.c //User input\nb.c\nc.c //Press Ctrl+D to complete input\n3 block\n\n
YouTip