There was a time when people used to debate whether the BSD tar (20.1, 19.5) (tape archiver) or the System V cpio (copy in/out) was the better file archive and backup program. At this point, there's no question. No one ships out cpio archives over the Net (1.33). tar is widespread, and because there are free versions available, including GNU tar (19.6), there's no reason why you should have to read a cpio archive from someone else.
Still, if you're on an older System V machine, you might use cpio. Though we don't give it much air time in this book, here are a few 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 (17.1), but 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
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 will restore 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 -o option, followed by the name of the destination directory. (This is one of the nicer features of cpio.) 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.