[Previous] [Contents] [Next]

cpio

Copy file archives in and out (UNIX)

Syntax:

Read/list an archive:

   cpio -i[Bcdfmrtuv] [pattern...]

Write an archive:

   cpio -o[Bacv]

Copy files:

   cpio -p[adlmuv] directory

Options:

-a
Reset access times of input files after they've been copied. When the -l option is also specified, the access times of linked files aren't reset. Can be used only with the -o or -i options.
-B
Cause input/output to be blocked 5120 bytes to the record. Can be used only with the -o or -i options for data directed to or from character special files.
-c
Write header information in ASCII (Default; option present for compatibility)
-d
Create directories as needed. Can be used only with the -i or -p options.
-f
Copy in all files except those in patterns. Can be used only with the -i option.
-i
Copy in. (Extract files from an archive being read from standard input.)
-l
("el") Whenever possible, link files rather than copy them. Can be used only with the -p option.
-m
Retain previous modification times. This option won't work on directories that are being copied. Can be used only with the -i or -p options.
-o
Copy out. (Write an archive to standard output.)
-p
Pass. Conditionally copy files from a list read from standard input to the destination directory named as an argument to cpio.
-r
Interactively rename files. A new name for each file will be requested from the user. Read and write permissions for the controlling terminal (/dev/tty) are required for this option. If you type a null line, the file is skipped. Should be used only with the -i or -o options.
-t
Print a table of contents of the input. No files are created. Can be used only with the -i option.
-u
Copy files unconditionally. Usually an older file will not replace a new file with the same name. Can be used only with the -i or -p options.
-v
Be verbose. Print the names of the affected files. Can be used only with the -i option. Provides a detailed listing when used with the -t option.
pattern
Simple regular expression given in the name-generating notation of the shell.
directory
The destination directory.

Description:

The cpio utility produces and reads files in the format specified by the POSIX cpio Archive/Interchange File Format. It operates in three modes.

The -i mode (copy in) extracts files from the standard input, which is assumed to be the product of a previous cpio -o. Only files with names that match patterns are selected. Multiple patterns may be specified. If no patterns are specified, the default for patterns is to select all files. The extracted files are conditionally created and copied into the current directory, and possibly any levels below, based on the options used. The permissions of the files will be those stored by the previous cpio -o invocation. The owner and group of the files will be that of the current user unless the user has appropriate privileges, which causes cpio to retain the owner and group of the files stored by the previous cpio -o invocation.

The -o mode writes the archive to the standard output.

The -p mode (pass) reads the standard input to obtain a list of pathnames of files that are conditionally created and copied into the destination directory based upon the options used.

If an error is detected, the cause is reported and the cpio utility will continue to copy other files. The utility will skip over any unrecognized files encountered in the archive.

The following restrictions apply to the cpio utility:

Examples:

Copy out the files listed by the ls utility and redirect them to the file archive:

    ls | cpio -o >archive

Use the output file archive from the cpio -o utility, extract those files that match the patterns memo/al and memo/b*, create the directories below the current directory, and place the files in the appropriate directories:

    cpio -id "memo/al" "memo/b*" <archive

Take the filenames piped to cpio from the find utility and copy or link those files to another directory named newdir, while retaining the modification time:

    find . -depth -print | cpio -pdlmv newdir

Exit status:

0
All input files were copied.
2
The utility encountered errors in copying or accessing files or directories. An error will be reported for nonexistent files or directories or for permissions that don't allow the user to access the source or target files.

Caveats:

When cpio restores a directory, it matches the permissions of the directory created to those of the original. If that directory lacks write permission, any attempt to copy additional files under that directory will fail. To get around this, save the files under a directory first before the directory itself. If find is used to generate pathnames for cpio, the -depth option should be supplied to find.

Note also that the controlling terminal (/dev/tty) is used to prompt the user for information when the -i or -r options are specified.

See also:

cp, find, lockfd, pax, tar


[Previous] [Contents] [Next]