Q. Can you explain how do I use cpio under Linux / UNIX?
A. GNU 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. cpio command works just like tar, only better.
As I said earlier, cpio works like tar but it can read input from the “find” command. This is nifty feature. For example you can find out all *.c files and backup with cpio command.# find / -name "*.c" | cpio -o --format=tar > c-file.backup.tar
You can also specify file name using -F option:
# find / -iname "*.pl" | cpio -o -H tar > perl-files.tar# find / -iname "*.pl" | cpio -o -H tar -F perl-files.tar
Where,
- -o: Create archive
- -F: Archive filename to use instead of standard input or output. To use a tape drive on another machine as the archive.
- -H format: Specify file format to use.
- -i: Restore archive
You can extract archive with the following command:# cpio -i -F perl-files.tar
You can list file inside archive i.e. list contents of the cpio file with following command:# cpio -it -F perl-files.tar
You can write archive of /home to tape (drive /dev/nst0), type the following command:# find /home | cpio -o -H tar -F /dev/nst0
Restore backup using following command:# cpio -i -F /dev/nst0
Backup /home dir, to remote system tape drive:# find /home | cpio -o -H tar -F user@backup.nixcraft.in:/dev/nst0 --rsh-command=/usr/bin/ssh
Examples:
(i)
$ ls | cpio -ov > di.cpio
The `-o’ option creates the archive, and the `-v’ option prints the names of the files archived as they are added.The `>’ redirects the cpio output to the file `di.cpio’
(ii)
$ find . -depth -print | cpio -o >/path/archive.cpio
This example uses the find utility to generate a list of path names starting in the current directory to create an archive of the directory tree.
(iii) cpio works like tar but it can read input from the “find” command. This is nifty feature. For example you can find out all *.c files and backup with cpio command.
$ find / -name "*.c" | cpio -o --format=tar > c-file.backup.tar
OR – Specifying format with -H option
$ find / -name "*.c" | cpio -o -H tar > c-file.backup.tar
OR – Specifying Filename with -F option
$ find / -name "*.c" | cpio -o -H tar -F c-file.backup.tar
2. Extraction:
During the copy-in operation, initiated by the -i command line flag, cpio reads an archive from its standard input and recreates the archived files in the operating system’s file system.
Extracting an archive is tedious because cpio does not create directories by default and it does not overwrite existing files unless you tell it to.
(i)
$ cpio -ivd <archive.cpio
This will retrieve the files archived in the file archive.cpio and place them in the present directory.
If you are dealing with an archived directory tree, you need to use the `-d’ option to create directories as necessary. The -v flag lists file names as they are extracted as explained before.
(ii)
If only files in the archive with matching names are to be copied from the archive, the following example shows that as it extracts etc/fstab from the archive.
$ cpio -id etc/fstab <archive.cpio
3. Listing files within archive
You can list file inside archive i.e. list contents of the cpio file with following command:
$ cpio -it < archive.cpio
List may be useful since a cpio archive may contain absolute rather than relative paths.
4. Copy
Cpio supports a third type of operation which copies files. It is initiated with the pass option (-p). cpio copies files from one directory tree to another, as specified by the path given as a command line argument, combining the copy-out and copy-in steps without actually using an archive.
$ find . -depth -print | cpio -pdumv new-path
This example copies the directory tree starting at the current directory to another path new-path in the file system, preserving file modes (-m), creating directories as needed (-d), replacing any existing files unconditionally (-u), while producing a progress listing on standard output (-v):