There was a time when people used to debate whether BSD tar (Section 38.2, Section 39.2) (tape archiver) or System V cpio (copy in/out) was the better file archive and backup program. At this point, though, no one ships out cpio archives over the Net (Section 1.21). tar is widespread, and there are free versions available, including GNU tar (Section 39.3).
There's still a good reason to use cpio: it's better at recovering backups from partially damaged media. If a block of your tape or disk archive goes bad, cpio can probably recover all files except the one with the bad block. A tar archive may not fare as well. Though we don't give it much air time in this book, here are a few cpio basics:
To write out an archive, use the -o option and redirect output either to a tape device or to an archive file. The list of files to be archived is often specified with find (Section 9.1), but it can be generated in other ways -- cpio expects a list of filenames on its standard input. For example:
% find . -name "*.old" -print | cpio -ocBv > /dev/rst8
or:
% find . -print | cpio -ocBv > mydir.cpio
To read an archive in, use the -i option and redirect input from the file or tape drive containing the archive. The -d option is often important; it tells cpio to create directories as needed when copying files in. You can restore all files from the archive or specify a filename pattern (with wildcards quoted to protect them from the shell) to select only some of the files. For example, the following command restores from a tape drive all C source files:
% cpio -icdv "*.c" < /dev/rst8
Subdirectories are created if needed (-d), and cpio will be verbose (-v), announcing the name of each file that it successfully reads in.
To copy an archive to another directory, use the -p option, followed by the name of the destination directory. (On some versions of cpio, this top-level destination directory must already exist.) For example, you could use the following command to copy the contents of the current directory (including all subdirectories) to another directory:
% find . -depth -print | cpio -pd newdir
There are lots of other options for things like resetting file access times or ownership or changing the blocking factor on the tape. See your friendly neighborhood manual page for details. Notice that options are typically "squashed together" into an option string rather than written out as separate options.
--TOR and JP
Copyright © 2003 O'Reilly & Associates. All rights reserved.