DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

cpio(C)


cpio -- copy file archives in and out

Syntax

cpio -o [ -aBLuvV ] [ -C bufsize ] [ -c | -H format ]
[ -K volumesize ] [ [ -O file [, file ... ] ] [ -M message ] ] [ -Pifd,ofd ]

cpio -i [ -6AbBcdfkmnqrsStTuvV ] [-C bufsize ] [ [ -I file [, file ... ] ]
[ -M message ] ] [ -Pifd,ofd ] [ pattern ... ]

cpio -p [ -adlLmruvV ] [ -Pifd,ofd ] directory

Description

cpio copies files to and from an archive file or another directory hierarchy.

There are three main options to cpio which determine its mode of operation. These modes are used to create an archive (cpio -o), extract files from an archive (cpio -i), and copy files from one filesystem to another (cpio -p). There are a number of secondary options, which are interpreted differently depending on which mode of operation cpio is in.


-o
(copy out) Reads a list of pathnames from the standard input, and copies those files onto the standard output together with pathname and status information. Output is padded to a 512-byte boundary by default.

-i
(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 the wildcard patterns are selected. (See ``Using patterns'' for details of the notation used.) Extracted files are conditionally created and copied into the current directory tree in accordance with the secondary options described below.

If cpio is used to copy files by a process without appropriate privileges, the access permissions are set in the same fashion that creat(S) would have set them when given the mode argument, matching the file permissions supplied by the c_mode field of the cpio format. The owner and group of the files will be that of the current user unless the user is root, which causes cpio to retain the owner and group of the files of the previous cpio -o.


NOTE: If cpio -i tries to create a file that already exists and the existing file is the same age or newer, cpio will output a warning message and not replace the file. (The -u option can be used to unconditionally overwrite the existing file.)


-p
(pass) Reads the standard input to obtain a list of pathnames of files that are conditionally created and copied into the destination directory tree based upon the options described below. Archives of text files created by cpio are portable between implementations of UNIX System V.

The following additional options are recognized by cpio. Their interpretation depends on the context selected by the primary option (-o, -i, or -p):

-6
Process a UNIX System Sixth Edition format file. Used only with the -i option.

-a
Reset access times of input files after they have been copied. Access times are not reset for linked files when cpio -pla is specified.

-A
Suppress absolute filenames. A leading ``/'' character is removed from the filename during copy-in. If a pattern is provided, it should match the relative (rather than the absolute) pathname.

-b
Reverse the order of the bytes within each word. Use only with the -i option.

-B
Block input/output 5,120 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/rdsk/f0q15dt.)

-c
For portability, write header information in ASCII character form. Always use this option when the origin and destination machines are of different types.

-C bufsize
Block 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/c0s0.) When used with the -K option, bufsize is forced to be a 1KB multiple.

-d
Create directories as needed.

-f
Copy in all files except those in patterns. (See the paragraph on cpio -i for a description of patterns.)

-H format
format may take any of the following values:

bin
(binary format) This is a non-portable version of the -c header format.

crc
(new character format with checksum) Write checksum information in the header of each file. This option uses the SVR4 extended ASCII header format.

newc
(new character format without checksum) Write archives using the SVR4 extended ASCII header format.

odc
(old character format) This has the same functionality as -c.


-I file[, file ... ]
Read the contents of file as input. If file is a character-special device, when the first medium is full, replace the medium and type a carriage return to continue to the next medium. You can perform an unattended multi-archive restore if you specify a comma-separated list of files.

Use only with the -i option.


-k
Attempt 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.

-K volumesize
Specify the size of the media volume. Must be in 1KB blocks. For example, a 1.2MB floppy disk has a volumesize of 1200. Must include the -C option with a bufsize multiple of 1KB. If you specify an incorrect size with -K, the command executes without error, but cpio generates the message ``out of sync: bad magic'' when the volume is read. (-K is not available with cpio -i.)

Note that this option is not needed with SCSI tape devices.


-l
Link files rather than copying them whenever possible. Used only with the -p option.

-L
Follow symbolic links.

This option requires that you use the -follow option to find(C) when it is used in conjunction with cpio.


-m
Retain previous file modification time. This option is ineffective on directories that are being copied.

-M message
Define 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 the message to print the sequence number of the next medium needed to continue.

-n
Calculate the checksum value for each file read from an archive (the checksum value is equivalent to that produced by the command sum -r). Filetypes other than regular files produce a checksum of 0 (zero). This option is intended to be used in conjunction with the -itv options; the first entry on each output line is the checksum of an archived file.

-O file[, file ... ]
Direct the output of cpio to file. If file is a character-special device, when the first medium is full, replace the medium and type a carriage return to continue to the next medium. You can change the end-of-media (EOM) message using the -M option.

You can perform an unattended multi-archive backup if you specify a comma-separated list of files. Unless you use SCSI devices, you should specify the volume size using the -K option. Note that cpio assumes that all specified devices have the same volume size.

Use only with the -o option.


-Pifd,ofd
Use file descriptors ifd and ofd for input from and output to a parent process. Any end-of-media (EOM) message is written to ofd rather than to /dev/tty. This option is only useful to programmers who want to write applications that communicate with cpio after a fork(S) and exec(S) by a parent process. The file descriptors must be opened prior to, and remain open on the invocation of cpio by the exec call.

-q
Extract files quickly. cpio extracts only the specified files from the archive and then stops. Shell regular expressions (wildcards) cannot be used in the filenames to be extracted.

-r
Rename files interactively. If the user types a null line, the file is skipped. If the user types a ``.'', the original pathname will be copied.

-s
Swap bytes within each half word. Use only with the -i option.

-S
Swap halfwords within each word. Use only with the -i option.

-T
Truncate long filenames to 14 characters. Use only with the -i option.

-t
Print a table of contents of the input. No files are created.

-u
Copy unconditionally (normally, an older file will not replace a newer file with the same name).

-v
(verbose) Print a list of filenames. When used with the -t option, the table of contents looks like the output of an ls -l command (see ls(C)).

-V
(special verbose) Print a dot for each file seen. This shows cpio is working without printing filenames.

Absolute and relative pathnames

Files may be archived with relative or absolute pathnames. Absolute pathnames specify the location of a file in relation to the root directory (/); relative pathnames specify the location of a file relative to the current working directory. As an example, consider the following cpio commands, as executed from the directory /u/bulls:

ls /u/bulls/eye | cpio -ocv > arcfile1
ls eye | cpio -ocv > arcfile2

The first command archives the file /u/bulls/eye, including its absolute pathname, as arcfile1. The second command archives eye as arcfile2, without storing any information about the path.

If you restore from arcfile1, eye will be written back to the directory /u/bulls no matter what your working directory. Restoring from arcfile2 will write eye to your current directory. In either case, you are not allowed to restore the file to a directory if you do not have write permission on that directory.

When making a cpio archive, consider whether you will always want to restore the files with absolute pathnames. You can extract files archived with absolute pathnames into their original directory, whatever your current working directory. If necessary, you can specify the -A option to suppress the absolute pathname, and extract files into a different path.

If you opt to archive files using relative pathnames, you will have to change directory to the one where the archive was created in order to extract the files with their original pathnames.

Handling multiple-volume archives

If cpio reaches end of medium (the end of a floppy disk, for example) when writing to (-o) or reading from (-i) a character-special device, and -O and -I are not used, cpio will print the message:
   If you want to go on, type device/filename when ready.
To continue, you must replace the medium and type the character-special device name (for example, /dev/rdsk/f0q15dt) and a carriage return. You may want to continue by directing cpio to use a different device. For example, if you have two floppy drives, you may want to switch between them so cpio can proceed while you are changing the floppies. (A carriage return alone causes the cpio process to exit.)

The -I and -O options allow you to read and write archives using multiple devices. This may be useful if you wish to perform an unattended backup or restore and have several archive devices available. If cpio runs out of devices it will prompt you to replace the medium in the last device in the list. It will repeat this action until the end of the archive.

Transferring files between different systems

cpio assumes 4-byte words. This is an important consideration when writing a portable archive for transfer to UNIX systems running on different hardware platforms. The byte order within each word can be changed using the -s and -S options; this may be necessary when transferring files between systems running on big-endian and little-endian hosts.

You should also use one of the options -c, -H crc, -H newc or -H odc when transferring files between different systems. These options write the header information in a portable ASCII character form.

The -H newc and -H crc options are suitable for systems that understand the SVR4 extended-ASCII cpio archive format.

cpio is capable of recognizing the cpio format that was used to create an archive.

You can also use the file(C) command to discover the format type used by a cpio archive.

Using patterns

A pattern is a regular expression given in the filename-generating notation of sh(C). Several patterns may be used to specify the filenames to be extracted from a cpio archive when the -i flag is set.

In patterns, metacharacters ?, *, and [ ... ] also match the slash (/) character, and backslash (\) is an escape character. A ``!'' metacharacter means not. (For example, !abc* would exclude all files that begin with abc.)

Multiple patterns may be specified. However, if no pattern is given, the default pattern is ``*'' (that is, select all files).

See regexp(M) for more information about shell regular expressions.

Each pattern must be enclosed in double quotes to prevent the shell from expanding it before it is passed to cpio.

Exit values

cpio returns the following values:

0
successful completion

>0
an error occurred

Examples

The following examples show cpio being used to create, verify and read archives, and to copy a complete directory structure.

Creating archives

The first example demonstrates the creation of an archive, as a file, or on a device such as a floppy drive. The filenames output by the ls command are directed through a pipe to cpio -o. These files are grouped and directed (>) to a single file (../newfile). The -c option ensures that the file will be portable to other machines. Instead of ls, you could use find, echo(C), cat(C), and so on, to pipe a list of names to cpio.

ls | cpio -ocv > ../newfile

The -v option is used to output a list of filenames as they are extracted.

You can also direct the output to a device instead of a file (here specifying a blocking factor using the -B option):

ls | cpio -ocvB -O /dev/rfd096ds15

These files are stored with pathnames relative to the current directory. In this way, they can be extracted into any desired destination directory.

If you use find with cpio, you can place conditions on which files are to be archived. For example, you can choose to archive:

It is important to use the -depth option of find to generate pathnames for cpio. This eliminates problems cpio could have trying to copy files from non-writable directories.

You should also use the -follow option of find if you follow symbolic links using the -L option of cpio.

You can create a multivolume archive of the entire filesystem on floppies using the -O option of cpio:

find / -depth -print | cpio -ovcBK 1200 -O /dev/rfd096ds15

This archive stores the files with absolute pathnames. When extracted, they will be put in exactly the same places in the directory structure. (If necessary, you can suppress the leading / of absolute pathnames, using the -A option, to change them to relative ones.)

To archive using relative pathnames, change directory to the one to be archived (for example /u), and specify a relative path (.), instead of an absolute one (/u), to find:

cd /u
find . -depth -print | cpio -ocvB -O /dev/rct0

To archive onto block devices (divisions. partitions, or whole disks) larger than 2GB, pipe cpio output through the dd(C) command using the conv=bmode option:

find . -depth -print | cpio -ovcC8192 | dd of=/dev/my_spare_division conv=bmode bs=8k

The -mount option of find limits the archive to the mounted filesystem where the search starts. For example, you might want to archive just those files on the filesystem (/), omitting any in filesystems mounted below / (depending on the way your system was installed, this may include /usr or /u):

find . -depth -mount -print | cpio -ocvB -O /dev/rct0

To limit the archive to just those files modified within the last seven days, use the -mtime option of find:

find . -depth -mount -mtime -7 -print | cpio -ocvB -O /dev/rct0

Extracting files from an archive

The next example uses cpio -i to reverse the action of cpio -o, and extract files stored with relative pathnames in an archive.

cat newfile | cpio -icvd "memo/a1" "info/b*"

Files that match the patterns memo/a1 and info/b* are extracted from the archive file, newfile. (If no patterns were given, all files from newfile would be extracted.) The -d option creates the directories, memo and info, below the current directory if they are not already present, and places the extracted files in the appropriate directories. If an archive has been created using absolute pathnames, the files to be read have to be specified with their original pathnames. The files would then be extracted into the same directories.

If the archive had been written to a tape drive, you might use:

cpio -icdv -I /dev/rct0 "memo/a1" "memo/b*"

The -d option will cause cpio to create directories as needed.

To read an archive from a block device larger than 2GB, use the conv=bmode option of the dd(C) command to read the data and pipe its output to cpio:

dd if=/dev/my_spare_division conv=bmode bs=8k | cpio -ivcC8192

Verifying an archive

If you specify the -t option as well as -i, the archive will be read but not extracted. This combination is used for verifying the contents of an archive. When combined with option -v, the output list of contents looks like that from the command ls -l:

cpio -ivt -I /dev/rct0

If you want to verify the checksums of files in an archive, use the -n option to display these:

cpio -invt -I /dev/rct0

Copying a directory structure

cpio used with the -p option takes the filenames piped to it and copies or links (-l option) those files to another directory (../newdir), keeping the modification time of the copied files (-m option).

find . -depth -print | cpio -pdlmv ../newdir

Reading from and writing to several devices

The -O option allows you to specify a comma-separated list of devices to allow you to perform multi-volume unattended backups and restores. For example, to perform a backup onto two SCSI tape drives:

find . -depth -print | cpio -ov -O /dev/rStp0,/dev/rStp1

This example restores files from a two-volume archive using two SCSI tape drives:

cpio -iv -I /dev/rStp0,/dev/rStp1

Directories with versioning enabled

Using cpio to overwrite a directory with versioning enabled will disable versioning. It must be re-enabled if desired.

Limitations

Open UNIX 8 compatibility notes

When running ACP on Open UNIX 8 and UnixWare 7 systems, set OSRCMDS=on to use the SCO OpenServer version of the <cpio> command. This provides the expected behaviors for SCO OpenServer applications. The SCO OpenServer version of this command is also provided on Open UNIX 8 systems under the OSP feature See the Running SCO OpenServer Applications topic in the Open UNIX 8 documentation set.

See also

cat(C), cpio(F), echo(C), find(C), ls(C), pax(C), pcpio(C), regexp(M), sum(C), tar(C)

Standards conformance

cpio is conformant with:

AT&T SVID Issue 2;
X/Open CAE Specification, Commands and Utilities, Issue 4, 1992: note that this command is marked as to be withdrawn.


© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003