One of the Unix/Linux commands line related to backup tools is
cpio, we have discussed the
tar command line in other articles. The
cpio command is a tools for creating and extracting archives, or copying files from one place to another. Shortly,
cpio command usage is to copy files to and from archives. At this article we will discuss how to use
cpio command line on Unix/Linux with examples. We will use CentOS 7 as our operation system on this tutorial.
cpio Command Line Modes
There are three mode types of
cpio command, namely : copy-out mode, copy-in mode and copy-pass mode.
In copy-out mode, cpio copies files into an archive. It reads a list of filenames, one per line, on the standard input, and writes the archive onto the standard output. A typical way to generate the list of filenames is with the find command; you should give find the -depth option to minimize problems with permissions on directories that are unreadable.
In copy-in mode, cpio copies files out of an archive or lists the archive contents. It reads the archive from the standard input. Any non-option command line arguments are shell globbing patterns; only files in the archive whose names match one or more of those patterns are copied from the archive. Unlike in the shell, an initial ‘ in a filename does match a wildcard at the start of a pattern, and a/’ in a filename can match wildcards. If no patterns are given, all files are extracted.
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.
cpio Command Options
cpio has many feature and usage, in practice this command has many options. Here’s several options are accompanying the cpio option, namely :
-0, --null. Read a list of filenames terminated by a null character, instead of a newline, so that files whose names contain newlines can be archived. GNU find is one way to produce a list of null-terminated filenames. This option may be used in copy-out and copy-pass modes.
-a, --reset-access-time. Reset the access times of files after reading them, so that it does not look like they have just been read.
-A, --append. Append to an existing archive. Only works in copy-out mode. The archive must be a disk file specified with the -O or -F (-file) option.
-b, --swap. Swap both halfwords of words and bytes of halfwords in the data. Equivalent to -sS. This option may be used in copy-in mode. Use this option to convert 32-bit integers between big-endian and little-endian machines.
-B. Set the I/O block size to 5120 bytes. Initially the block size is 512 bytes.
The complate references for
cpio command options could be found an Unix/Linux manual pages.
cpio Command Examples
Our example scenario in using cpio command line is creating new archive file and extraxting its archive file to a new directory. The environment we have, is a directory with several files on
/home/ramans/etc/cups directory, as follow :
[ramans@localhost cups]$ pwd /home/ramans/etc/cups [ramans@localhost cups]$ ls -ltr total 68 -rw-r--r--. 1 ramans ramans 319 Jun 22 2015 paps.convs drwx------. 2 ramans ramans 6 Aug 9 06:07 ssl drwxr-xr-x. 2 ramans ramans 6 Aug 9 06:07 ppd drwxr-xr-x. 2 ramans ramans 6 Aug 9 06:07 interfaces -rw-r--r--. 1 ramans ramans 186 Aug 9 06:07 snmp.conf -rw-r-----. 1 ramans ramans 4504 Aug 9 06:07 cupsd.conf.default -rw-r-----. 1 ramans ramans 4504 Aug 9 06:07 cupsd.conf -rw-------. 1 ramans ramans 0 Aug 9 06:07 printers.conf -rw-r--r--. 1 ramans ramans 0 Aug 9 06:07 lpoptions -rw-r--r--. 1 ramans ramans 0 Aug 9 06:07 client.conf -rw-------. 1 ramans ramans 0 Aug 9 06:07 classes.conf -rw-r--r--. 1 ramans ramans 16072 Aug 9 06:09 cups-browsed.conf.rpmnew -rw-r--r--. 1 ramans ramans 1029 Dec 10 23:10 cups-browsed.conf -rw-r-----. 1 ramans ramans 3091 Dec 12 19:15 cups-files.conf -rw-r-----. 1 ramans ramans 111 Dec 18 17:24 subscriptions.conf.O -rw-r-----. 1 ramans ramans 412 Dec 18 17:25 subscriptions.conf
Create archive file
ls | cpio -ov > archived_file.cpio
The -o option creates the archive, and the-v’ option prints the names of the files archived as they are added.
cpio command line, we will create archive file called
testcpio.cpio where the file’s content will be having files with .
conf extension. Usually, the ls or find command line will be use together with the cpio. We will use
cpio command file to create archive file by submitting command :
[ramans@localhost cups]$ ls *.conf| cpio -ov > testcpio.cpio classes.conf client.conf cups-browsed.conf cupsd.conf cups-files.conf printers.conf snmp.conf subscriptions.conf 19 blocks
cpio -idv < archived_files.cpip
The -i option extracts the archive and the -v shows the file names as they are extracted.
By using cpio command, we extracted the archived files from the directory to the current directory.
[ramans@localhost ~]$ cpio -idv < /home/ramans/etc/cups/testcpio.cpio classes.conf client.conf cups-browsed.conf cupsd.conf cups-files.conf printers.conf snmp.conf subscriptions.conf 19 blocks