Most people live their whole lives without ever using the cpio utility. However, it is a useful thing, and is sometimes better suited to copying lots of files around than tar or cp. Here's a quick primer.
Some important flags
(In case the man-page wasn't clear enough)
- -o
- Copy out mode: Read in files from disk, output is a cpio stream or file, similar to "tar -c"
- -i
- Copy in mode: Read cpio archive file or stdin, extract as files to disk, similar to "tar -x"
- -p
- Pass-thru mode: Like piping cpio -o into cpio -i
- -V
- Verbose mode: Prints a dot for each file processed (all modes)
- -O
- Outfile: Specify an archive file rather than stdout (copy-out mode)
- -d
- Creates leading directories where needed when extracting files (copy-in and pass-thru modes)
- -a
- Resets atime on files being read, so the original files won't appear to have just been accessed (copy-in and pass-thru modes)
- -m
- Preserves original mtime on files being written out, so the copy will have the same timestamps as the original (copy-out and pass-thru modes)
Basic usage
Create a cpio archive:
localhost% find path/ -depth -print | cpio -oaV > archive.cpio localhost% find path/ -depth -print | cpio -oaV -O archive.cpio
Create a cpio archive and compress it:
localhost% find path/ -depth -print | cpio -oaV | gzip -c > archive.cpio.gz
Extract a cpio archive:
localhost% cpio -imVd < archive.cpio localhost% cpio -imVd -I archive.cpio
List the contents of a cpio archive:
localhost% cpio -it < archive.cpio localhost% cpio -it -I archive.cpio
Use cpio copy-pass to copy a directory structure to another location:
localhost% find path/ -depth -print | cpio -pamVd /new/parent/dir
Pathnames
Avoid using pathnames starting with /, as that can be inflexible and possibly messy when extracting an archive. Good practice is to change to the directory above the one you're copying. That way, extracted files will go into their own directory, rather than the current directory, much like a well-behaved source tarball. For example:
localhost% pwd /usr/src/linux-2.4 localhost% cd .. localhost% find linux-2.4/ -depth -print | cpio -oaV -O linux-2.4.cpio
cpio over ssh
To cpio a local directory, send the output to ssh and feed it to cpio on a remote host:
localhost% find path/ -depth -print | cpio -oaV | ssh user@host 'cpio -imVd'
Ssh to a remote host, cpio a remote directory, and get its output locally:
localhost% ssh user@host "find path/ -depth -print | cpio -oaV" | cpio -imVd
cpio and rpm
Ever wanted to extract files from an RPM package without installing it? It's easy. RPMv4 includes a utility called "rpm2cpio", which creates a cpio stream of files from a given RPM. You can pipe this into cpio just like a regular archive or stream.
List the included files:
localhost% rpm2cpio foo.rpm | cpio -it ./usr/bin/foo ./usr/share/man/man1/foo.1.gz 39 blocks
Note that the pathnames in the cpio stream begin with "./" -- this is important to know if you want to name specific files to extract.
Extract all files:
localhost% rpm2cpio foo.rpm | cpio -imVd .. 39 blocks
Extract only the manpage from that package:
localhost% rpm2cpio foo.rpm | cpio -imVd ./usr/share/man/man1/foo.1.gz