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.
cpiocommand performs three primary functions namely -
- Copying files to an archive,
- Extracting files from an archive, and
- Passing files to another directory tree.
cpiocan take input from
findcommand which is an advantage while taking selective backups.
The following are a few examples of using cpio in Linux.
Take a backup of all the configuration files residing in your
$ find /etc -iname \*.conf | cpio -o --format=tar > backup.tar
You can use
-Hoption instead of
--formatin the above command for the same effect.
$ find /etc -iname \*.conf | cpio -o -H tar > backup.tar
You can also achieve the same without using redirection
$ find /etc -iname \*.conf | cpio -o --format=tar -F backup.tar
$ find /etc -iname \*.conf | cpio -o -H tar -F backup.tar
Add files to an already existing Tar file
--appendoption to add more files to an already existing tape archive (Tar file).
As an example, let's first create an archive
backup.tarwhich contains the directory
$ find ../dir1 | cpio -o --format=tar -F backup.tar
Now let's add some more files from another directory -
dir2to the newly created
backup.tararchive. For this we use the
--appendoption as shown below.
$ find ../dir2 | cpio -o --format=tar --append -F backup.tar
List contents of the Tar file
$ cpio -it < test.tar
$ cpio -it -F test.tar
Extract the contents from the tar file
You use the
-ioption for the purpose.
$ cpio -i -F test.tar
Copy all (or a subset) of files from current directory to another directory
$ find . -print0 -depth | cpio --null -pvd new-dir
In the above command, the
--nullswitches will act together to send file names between find and cpio, even if special characters are embedded in the file names.
-pswitch tells cpio to pass the files it finds to the directory