cpio: Tutorial

1 
1 2 Tutorial
1 **********
1 
1 GNU cpio performs three primary functions.  Copying files to an archive,
1 Extracting files from an archive, and passing files to another directory
1 tree.  An archive can be a file on disk, one or more floppy disks, or
1 one or more tapes.
1 
1    When creating an archive, cpio takes the list of files to be
1 processed from the standard input, and then sends the archive to the
11 standard output, or to the device defined by the '-F' option.  ⇒
 Copy-out mode.  Usually find or ls is used to provide this list to the
1 standard input.  In the following example you can see the possibilities
1 for archiving the contents of a single directory.
1 
1      % ls | cpio -ov > directory.cpio
1 
1    The '-o' option creates the archive, and the '-v' option prints the
1 names of the files archived as they are added.  Notice that the options
1 can be put together after a single '-' or can be placed separately on
1 the command line.  The '>' redirects the cpio output to the file
1 'directory.cpio'.
1 
1    If you wanted to archive an entire directory tree, the find command
1 can provide the file list to cpio:
1 
1      % find . -print -depth | cpio -ov > tree.cpio
1 
1    This will take all the files in the current directory, the
1 directories below and place them in the archive tree.cpio.  Again the
1 '-o' creates an archive, and the '-v' option shows you the name of the
1 files as they are archived.  ⇒Copy-out mode.  Using the '.' in
1 the find statement will give you more flexibility when doing restores,
1 as it will save file names with a relative path vice a hard wired,
1 absolute path.  The '-depth' option forces 'find' to print of the
1 entries in a directory before printing the directory itself.  This
1 limits the effects of restrictive directory permissions by printing the
1 directory entries in a directory before the directory name itself.
1 
1    Extracting an archive requires a bit more thought because cpio will
1 not create directories by default.  Another characteristic, is it will
1 not overwrite existing files unless you tell it to.
1 
1      % cpio -iv < directory.cpio
1 
1    This will retrieve the files archived in the file directory.cpio and
1 place them in the present directory.  The '-i' option extracts the
1 archive and the '-v' shows the file names as they are extracted.  If you
1 are dealing with an archived directory tree, you need to use the '-d'
1 option to create directories as necessary, something like:
1 
1      % cpio -idv < tree.cpio
1 
1    This will take the contents of the archive tree.cpio and extract it
1 to the current directory.  If you try to extract the files on top of
1 files of the same name that already exist (and have the same or later
1 modification time) cpio will not extract the file unless told to do so
1 by the -u option.  ⇒Copy-in mode.
1 
1    In copy-pass mode, cpio copies files from one directory tree to
1 another, combining the copy-out and copy-in steps without actually using
1 an archive.  It reads the list of files to copy from the standard input;
1 the directory into which it will copy them is given as a non-option
1 argument.  ⇒Copy-pass mode.
1 
1      % find . -depth -print0 | cpio --null -pvd new-dir
1 
1    The example shows copying the files of the present directory, and
1 sub-directories to a new directory called new-dir.  Some new options are
1 the '-print0' available with GNU find, combined with the '--null' option
1 of cpio.  These two options act together to send file names between find
1 and cpio, even if special characters are embedded in the file names.
1 Another is '-p', which tells cpio to pass the files it finds to the
1 directory 'new-dir'.
1