Cpio is a tool for creating and extracting archives, or copying files from one place to another. It handles a number of cpio formats as well as reading and writing tar files. The GNU cpio performs three primary functions. Copying files to an archive, Extracting files from an archive, and passing files to another directory tree.
Suppose you want to take a tar backup of all your configuration files. This can be achieved easily by the following set of commands:
$ find /etc -iname \*.conf | cpio -o --format=tar > test.taror you could substitute the -H switch for --format in the above command for the same effect.
$ find /etc -iname \*.conf | cpio -o -H tar > test.tarSame command without the redirection ">"
$ find /etc -iname \*.conf | cpio -o --format=tar -F test.tar
$ find /etc -iname \*.conf | cpio -o -H tar -F test.tarYou can append data to an already existing tar file using the --append switch. For that first create a tar file as follows:
$ find ../dir1 | cpio -o --format=tar -F test.tar
Now append some data from 'dir2' to the newly created 'test.tar' file using the --append switch.
$ find ../dir2 | cpio -o --format=tar --append -F test.tarList contents of the tar file
$ cpio -it < test.tar
$ cpio -it -F test.tarExtract the contents from the tar file
You use the -i switch for the purpose.
$ cpio -i -F test.tarThe advantage of 'cpio' over 'tar' is that it can take input from the 'find' command.
Suppose you want to copy all (or a subset of) the files in your directory to another directory. This can be easily achieved by using the combination of find and cpio as follows:
$ find . -print0 -depth | cpio --null -pvd new-dir
The interesting thing to note is the -print0 and the --null switches which act together to send filenames between find and cpio, even if special characters are embedded in the filenames. The -pswitch tells cpio to pass the files it finds to the directory 'new-dir'.