Previous Table of Contents Next

Module 25
cpio (SV)


The external cpio command is used to copy files and directory structures in and out of archive files. It can also copy directory structures from one location to another. There are three modes of operation handled by cpio. The "copy out" mode creates an archive file. The "copy in" mode allows you to retrieve a previously archived file. The "pass" mode passes a copy of a directory structure from the source to a new destination. The new directory structure is an exact image of the source structure.


Following is the general format of the cpio command.

     cpio -i [ /6bBcdfkmrsStuvV/ ] [ -C size ] [ -E file ] [ -H hdr ] [ -O file ]     [ -I file [ -M msg ]] [ -R ID ] [ patterns ]
     cpio -o [ /aABcLvV/ ] [ -C size ] [ -H hdr ] [ -O file ] [ -M msg ]
     cpio -p[ /adlmruvV/ ] [ -R ID ] directory

The first format is the "copy in" mode, the second is the "copy out," and the third is the "pass" mode.

Not all systems support the -r option in PASS mode.

The cpio command is not a standard utility on BSD. But many vendors include it in their release of BSD. It is a standard SV utility.


The cpio command must have one of the three options -i, -o, or -p specified on the command line. Each of these options places cpio in one of three modes. The following list describes each of these modes.


-i patterns Causes cpio to extract files from the standard input. This usually implies input redirection on the command line. It assumes the input is the result of a previous cpio -o command. For example,
cpio -i < cpio.i
assumes you have previously created the file cpio.i using cpio and attempts to extract all files archived.
If you do not specify any patterns, all files in the archive file are extracted. If you do specify patterns, only the files that match the patterns are extracted. The patterns you use may contain any of the shell's Filename Generation Characters (?, *, [class]). You may specify multiple patterns.
The permissions of the extracted files are the same as the permissions when the files were stored in the archive file. The owner and group IDs of the extracted files are set to your owner and group ID unless you are super-user. In which case the owner and group IDs at the time of archival are restored.

If a file exists on disk and cpio tries to extract a file by the same name, the extracted file will replace the disk file only if it (the extracted file) is newer. If the disk file is the same age or newer, cpio displays a warning message but does not overwrite the disk file.

When extracting files be aware of how the files were copied out. It is not uncommon for relative pathnames to be used. Thus when you extract a file it may actually be placed in your parent directory instead of in the current directory or some other directory. For example, if a cpio archive file was made using the following command,

     ls .. ../.. | cpio -o > arch

the files stored in the arch file would have pathnames of ../file1, ../../fileX, etc. When you extract the files, they would be placed in the parent and parent's parent directory. To check the pathnames of files in an archive file use the -t option.


-o The copy out option causes cpio to read the standard input to acquire a list of pathnames (files and directories). It then copies each of the specified files to the standard output. The pathnames and header (inode) information is copied in conjunction with each file. For example,
ls | cpio -o > arch
copies all of the files in the current directory to the arch file.

When creating an archive file using cpio you should not begin pathnames with a /. Instead use relative pathnames. Either start with a . or the name of a subdirectory. This allows you to extract a file or the entire archive file to a new location. By doing this you can restore files from an archive file without overlaying the current copy of the file.

Previous Table of Contents Next