Go to the first, previous, next, last section, table of contents.


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 section 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 and its sub-directories, 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 section Copy-out mode. Using the `.' argument to find will give you more flexibility when doing restores, as it will save file names with a relative path instead of a hard-wired, absolute path. The `-depth' option forces `find' to print all the entries in a directory before printing the directory itself. This limits the effects of restrictive directory permissions by putting the directory entries into the aarchive before the directory name itself.

Extracting an archive requires a bit more thought because cpio will not create directories by default. Also, 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 into 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 files unless told to do so by the `-u' option. See section 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 creating 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 section Copy-pass mode.

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

This 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. The `-p' option tells cpio to pass the files it finds to the directory `new-dir'.


Go to the first, previous, next, last section, table of contents.