[ << ] [ < ] [ 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 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, 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 section 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. First of all, by default cpio extracts the files with exactly the same name as stored in the archive. That means that if the archive contains absolute paths, you will extract files to their absolute locations no matter what directory you’re in when running the command. You can instruct cpio to remove leading slashes using the ‘--no-absolute-filenames’ option. Nevertheless, the good practice is to always test the archive using cpio -t prior to extracting it.

Furthermore, 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 restore them to their locations. 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. 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 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 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 section 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’.


[ << ] [ < ] [ Up ] [ > ] [ >> ]

This document was generated on May 1, 2023 using texi2html 5.0.