Linux / Unix cpio command

Quick links

About cpio
Syntax
Examples
Related commands
Linux / Unix main page

About cpio

Creates and un-creates archived cpio files. And also is capable of copying files to things other than a hard disk.

Syntax

cpio -i [ bBcdfkmPrsStuvV6 ] [ -C bufsize ] [ -E file ] [ -H header ] [ -I file [ -M message ] ] [ -R id ] [pattern ... ]

cpio -o [ aABcLPvV ] [ -C bufsize ] [ -H header ] [ -O file [ -M message ] ]

cpio -p [ adlLmPuvV ] [ -R id ] directory

-i(copy in) cpio -i extracts files from the standard input.
-o(copy out) cpio -o reads the standard input to obtain a list of path names and copies those files onto the standard output.
-p(pass) cpio -p reads the standard input to obtain a list of path names of files.

In conjunction with the above commands the below commands can be used.

-aReset access times of input files after they have been copied. Access times are not reset for linked files when cpio -pla is specified (mutually exclusive with -m).
-AAppend files to an archive. The -A option requires the -O option. Valid only with archives that are files, or that are on floppy diskettes or hard disk partitions.
-bReverse the order of the bytes within each word. (Use only with the -i option.)
-BBlock input/output 5120 bytes to the record. The default buffer size is 512 bytes when this and the -C options are not used. -B does not apply to the pass option; -B is meaningful only with data directed to or from a character special device, for example, /dev/rmt/0m.
-cRead or write header information in ASCII character form for portability. There are no UID or GID restrictions associated with this header format. Use this option between SVR4-based machines, or the -H odc option between unknown machines. The -c option implies the use of expanded device numbers, which are only supported on SVR4-based systems. When transferring files between SunOS 4 or Interactive Unix and the Solaris 2.6 Operating environment or compatible versions use -H odc.
-C bufsizeBlock input/output bufsize bytes to the record, where bufsize is replaced by a positive integer. The default buffer size is 512 bytes when this and -B options are not used. (-C does not apply to the pass option; -C is meaningful only with data directed to or from a character special device, for example, /dev/rmt/0m.)
-dCreates directories as needed
-E fileSpecify an input file (file) that contains a list of filenames to be extracted from the archive (one filename per line).
-fCopy in all files except those in patterns. (See OPERANDS for a description of patterns.)
-H headerRead or write header information in header format. Always use this option or the -c option when the origin and the destination machines are different types (mutually exclusive with -c and -6). Valid values for header are:

bar bar head and format. Used only with the -i option ( read only)

crc|CRC ASCII header with expanded device numbers and an additional per-file checksum. There are no UID or GID restrictions associated with this header format.

odc ASCII header with small device numbers. This is the IEEE/P1003 Data Interchange
Standard cpio header and format. It has the widest range of portability of any of the header formats. It is the official format for transferring files between POSIX-conforming systems. Use this format to communicate with SunOS 4 and Interactive Unix. This header format allows UIDs and GIDs up to 262143 to be stored in the header.

tar |TAR tar header and format. This header format allows UIDs and GIDs up to 2097151 to be stored in the header.

ustar|USTAR
IEEE/P1003 Data Interchange Standard tar header and format. This header format
allows UIDs and GIDs up to 2097151 to be stored in the header.

Files with UIDs and GIDs greater than the limit stated above will be archived with the UID and GID of 60001.

-I fileRead the contents of file as an input archive. If file is a character special device, and the
current medium has been completely read, replace the medium and press RETURN to  continue to the next medium. This option is used only with the -i option.
-kAttempt to skip corrupted file headers and I/O errors that may be encountered. If you want to copy files from a medium that is corrupted or out of sequence, this option lets you read only those files with good headers. (For cpio archives that contain other cpio archives, if an error is encountered cpio may terminate prematurely. cpio will find the next good header, which may be one for a smaller archive, and terminate when the  smaller archive's trailer is encountered.) Used only with the -i option.
-lWhenever possible, link files rather than copying them. (Usable only with the -p option.)
-LFollow symbolic links. The default is not to follow symbolic links.
-mRetain previous file modification time. This option is ineffective on directories that are being copied (mutually exclusive with -a).
-M messageDefine a message to use when switching media. When you use the -O or -I options and specify a character special device, you can use this option to define the message that is printed when you reach the end of the medium. One %d can be placed in message to print the sequence number of the next medium needed to continue.
- O fileDirect the output of cpio to file. If file is a character special device and the current medium is  full, replace the medium and type a carriage return to continue to the next medium. Use only with the -o option.
-PPreserve ACLs. If the option is used for output, ACLs if existed are written along with other attributes to the standard output. ACLs arecreated as special files with a special file type. If the option is used for input, ACLs if existed are extracted along with other attributes from standard input. The option recognizes the special file type. Note that errors will occur if a cpio archive with ACLs is extracted by previous versions of cpio . This option should not be used with the -c option, as ACL support may not be present on all systems, and hence is not portable. Use ASCII headers for portability.
-rInteractively rename files. If the user types a carriage return alone, the file is skipped. If the user types a ``.'' the original pathname will be retained. (Not available with cpio -p.)
-R idReassign ownership and group information for each file to user ID (ID must be a valid login ID from /etc/passwd). This option is valid only for the super-user.
-sSwap bytes within each half word.
-SSwap halfwords within each word.
-tPrint a table of contents of the input. No files are created (mutually exclusive with -V).
-uCopy unconditionally (normally, an older file will not replace a newer file with the same name).
-vVerbose. Print a list of file names. When used with the -t option, the table of contents looks like the output of an ls -l command (see ls)
-VSpecial verbose. Print a dot for each file read or written. Useful to assure the user that cpio is working without printing out all file names.
directoryA path name of an existing directory to be used as the target of cpio -p.
patternExpressions making use of a pattern-matching notation similar to that used by the shell  for filename pattern matching, and similar to regular expressions. The following metacharacters are defined:

* Matches any string, including the empty string.

? Matches any single character.

[...] Matches any one of the enclosed characters. A pair of characters separated by `-' matches any symbol between the pair (inclusive), as defined by the system default collating sequence. If the first character following the opening `[' is a `!', the results are unspecified.

! means not. (For example, the !abc* pattern would exclude all files that begin with abc.)

In patterns, metacharacters ?, *, and [...] match the slash (/) character, and backslash (\) is an escape character. Multiple cases of pattern can be  specified and if no pattern is specified, the default for pattern is * (that is, select all files).

Each pattern must be enclosed in double quotes; otherwise, the name of a file in the current directory might be used.

Examples

find . -print | cpio -ocv > /dev/fd0

Above, using the find command would list all files and directories and using the cpio command copy those files listed to the floppy drive.

find . -print | cpio -dumpv /home/users/hope

In the above example the find command would find all files and directories and using the cpio command copy those files to the hope user account.

cpio -icuvd < /dev/fd0

The above command would restore the files back from the floppy.

Related commands

ar
cat
echo
gzip
ls
setfacl
tar