cpio: Tutorial

Next: Invoking cpio, Previous: Introduction, Up: Top [Contents][Index]

## 2 Tutorial

GNU cpio performs three primary functions. Copying files to an archive, Extracting files from an archive, and passing files to another directory tree. An archive can be a file on disk, one or more floppy disks, or one or more tapes.

When creating an archive, cpio takes the list of files to be processed from the standard input, and then sends the archive to the standard output, or to the device defined by the `-F` option. See Copy-out mode. Usually find or ls is used to provide this list to the standard input. In the following example you can see the possibilities for archiving the contents of a single directory.

    % ls | cpio -ov > directory.cpio

The `-o` option creates the archive, and the `-v` option prints the names of the files archived as they are added. Notice that the options can be put together after a single `-` or can be placed separately on the command line. The ‘`>`’ redirects the cpio output to the file ‘`directory.cpio`’.

If you wanted to archive an entire directory tree, the find command can provide the file list to cpio:

    % find . -print -depth | cpio -ov > tree.cpio

This will take all the files in the current directory, the directories below and place them in the archive tree.cpio. Again the `-o` creates an archive, and the `-v` option shows you the name of the files as they are archived. See Copy-out mode. Using the ‘`.`’ in the find statement will give you more flexibility when doing restores, as it will save file names with a relative path vice a hard wired, absolute path. The `-depth` option forces ‘`find`’ to print of the entries in a directory before printing the directory itself. This limits the effects of restrictive directory permissions by printing the directory entries in a directory before the directory name itself.

Extracting an archive requires a bit more thought because cpio will not create directories by default. Another characteristic, is it will not overwrite existing files unless you tell it to.

    % cpio -iv < directory.cpio

This will retrieve the files archived in the file directory.cpio and place them in the present directory. The `-i` option extracts the archive and the `-v` shows the file names as they are extracted. If you are dealing with an archived directory tree, you need to use the `-d` option to create directories as necessary, something like:

    % cpio -idv < tree.cpio

This will take the contents of the archive tree.cpio and extract it to the current directory. If you try to extract the files on top of files of the same name that already exist (and have the same or later modification time) cpio will not extract the file unless told to do so by the -u option. See Copy-in mode.

In copy-pass mode, cpio copies files from one directory tree to another, combining the copy-out and copy-in steps without actually using an archive. It reads the list of files to copy from the standard input; the directory into which it will copy them is given as a non-option argument. See Copy-pass mode.

    % find . -depth -print0 | cpio --null -pvd new-dir

The example shows copying the files of the present directory, and sub-directories to a new directory called new-dir. Some new options are the `-print0` available with GNU find, combined with the `--null` option of cpio. These two options act together to send file names between find and cpio, even if special characters are embedded in the file names. Another is `-p`, which tells cpio to pass the files it finds to the directory ‘`new-dir`’.

Next: Invoking cpio, Previous: Introduction, Up: Top [Contents][Index]