8

While studying for the lpic exam, I've run into the command cpio.

The description of cpio states:

Cpio was originally designed to store backup file archives on a tape device in a sequential, contiguous manner. Cpio does not compress any content, but resulting archives are often compressed using gzip or other external compressors.

In looking at examples of how to use cpio, I can't find a single scenario where cpio would better than more common tools like tar.

What is an example where using cpio would be used, (besides tape backups), or is it no longer relevant on todays systems?

spuder
  • 17,643
  • 36
  • 91
  • 119
  • 3
    I've always wondered this myself. But I do know that a current frequent use is for linux kernel initramfs images. Initramfs is a relatively recent addition to the kernel as well, so it's still relevant for today's systems. – phemmer Jan 31 '14 at 06:08
  • You excluded tape backups but not floppy disk images. I found it was useful in [one case](http://unix.stackexchange.com/questions/106765/mounting-an-old-floppy-image-file-ima-format-how-hard-can-it-be) where nothing else worked! –  Jan 31 '14 at 08:24

6 Answers6

5

It is still relevant for unpacking RPMs. There is a rpm2cpio utility that converts an RPM package to a cpio archive. As far as I know, there is nothing to convert an RPM to a tar archive. Here is an example usage:

rpm2cpio myrpmfile.rpm | cpio -idmv
jordanm
  • 41,988
  • 9
  • 116
  • 113
  • 2
    While this is true, it doesn't really cover the main part of the question asking why cpio is better than tar. This just leads to another question, why doesn't a rpm2tar utility exist? Does RPM use cpio internally, if so why did they chose it over tar? – phemmer Jan 31 '14 at 20:38
  • 3
    RPM is [partially based on `cpio`](http://en.wikipedia.org/wiki/RPM_Package_Manager#Format). Thus, `rpm2cpio` is just doing an extraction function, not a conversion. This is why there is no `rpm2tar`. – Warren Young Feb 07 '14 at 07:32
2

If your system doesn't have a GNU tar with its -T and --null option cpio is the way to get a specific subset of files into a a tar archive:

find . -name CVS -prune -o -print0 | cpio -o -Hustar > all.tar

I also often find the -V/--dot much more informative on progress then full verbose mode (in tar or cpio)in cases where you rougly know how many files you are going to have to pack (assuming you know how long your terminal is and in my case are good with the multiplication table of 80).

There is always use for multiple solutions to the same problem. Since cpio can read tar archives you might as well do away with tar and just use cpio everywhere.

Timo
  • 6,202
  • 1
  • 26
  • 28
  • find does not have a primary called `-print0`and cpio is unable to understand the output from the `gfind -print0` . – schily Sep 10 '15 at 13:58
2

tar uses the ustar archive definition, which limits the pathname length to 100 bytes. Deeply nested hierarchies -- not at all uncommon these days -- can't be archived with tar. cpio does not have that handicap.

I prefer pax(1). Like cpio, pax makes better use of standard input and output than tar does. pax can use cpio format, which means if you bump into the pathname limit you can keep using the same utility just by adding -x cpio to the command. You use that option only to create archives; on reading pax detects the format automatically.

James K. Lowden
  • 2,052
  • 13
  • 15
  • 2
    GNU tar does not have such a limit. – psusi Feb 01 '14 at 18:23
  • The limit belongs to the file format definition, not to the utility. Please refer to `char name[100]` in the [GNU tar](http://www.gnu.org/software/tar/manual/html_node/Standard.html) documentation. – James K. Lowden Feb 20 '14 at 05:28
  • Well they got around it somehow because I just made a 108 character named file and tar handles it just fine. – psusi Feb 20 '14 at 14:29
  • Not correct, the only standardized CPIO format supports a maximum filename length of 255 chars, while the standardized TAR archive format has unlimited filename length. – schily Sep 10 '15 at 14:06
2

In former times, there was an advantage of using cpio but this special feature is not implemented in e.g. gcpio. I am talking about the fact that the original AT&T cpio implementation did implement file extraction in a specific way that is similar to what install does and this is the reason why AT&T preferred to use cpio to install binaries on a live system.

If you call starunder the name cpio, you get this feature and if you add the option -install to the options for star when extracting files, you get this behavior.

BTW: the CPIO archive format is no longer seen as a useful format by POSIX, as it is not extensible. The four CPIO format "variants" are completely incompatible to each other and every time an extension was needed, a completely new/incompatible CPIO format variant was created.

schily
  • 18,806
  • 5
  • 38
  • 60
1

I can't find a single scenario where cpio would better than more common tools like tar.

Here's one, which I learned back before rsync and cp -r existed:

$ find /foo -print | cpio -pud /bar

This copies all files in /foo into /bar, preserving all file permissions and directory structure.

To do that with tar, you need to create a temporary archive file and then unpack it again, because tar doesn't have this pass-thru filter mode.

Warren Young
  • 71,107
  • 16
  • 178
  • 168
  • Not correct, you are correct for the historical UNIX tar implementation but not for `star`as star supports a -copy option and is able to do copies more efficient than cpio. – schily Sep 10 '15 at 14:00
  • @schily: this question isn't about star, it's about tar. The OP did not open the door to installing third party tools. – Warren Young Sep 10 '15 at 14:03
  • From what I can tell, every sysadmin that is frequently using tar usually has star installed anyway. If you believe that the OP is using Linux, then we would need to strongly encourage users not to use "cpio" on that platform as usually `gcpio`is installed as cpio under Linux and gcpio unfortunately is an implementation with many problems. – schily Sep 10 '15 at 14:23
  • @schily: One of us lives in a bubble, and I'm pretty sure it isn't me. I've never used star in a quarter century of Unix use. It isn't installed by default on RHEL/CentOS, Solaris, Ubuntu Server, FreeBSD, or OS X. It isn't even on the standard package repo for most of those. That covers a huge chunk of today's sysadmin-centric Unix world. Meanwhile, you wrote the thing, so of course it's installed on all your boxes. – Warren Young Sep 10 '15 at 15:52
  • Star was installed on most Linux distros for a long time and it is the oldest free tar implementation. If you don't know star, it seems that you did not check for installed alternatives during the past 20 years. Because of various problems with gtar, I avoid gtar wherever possible and many people who know star do the same. So it is up to the reader to decide who is a bubble. – schily Sep 10 '15 at 16:38
0

I have used cpio for many years and still do. It has saved me many times in recovery without extensive Linux/Unix knowledge.

Example I have an Ubuntu 17.04LTS Server installed and regularly update my files to backup using cpio approx. every month using an external USB 500Gb drive.

I mount the Backup drive as such:

mount /dev/sdb1 /BACKUP 

Ensure you have created a folder called BACKUP in your root directory as a mount point and use following command;

mount /dev/sdb1 /BACKUP    

Do a df to ensure it is mounted. Then cd /BACKUP and create a folder called Ubuntu_Backup_17.04LTS.

Then cd /, check that you are in / before starting the cpio process with pwd, and then and execute the following command:

find . -mount -print | cpio -pdumv /BACKUP/Ubuntu_Backup_17.04LTS

This will then copy all files and folders from the ROOT directory EXCLUDING the mount point BACKUP.

After finishing if you go to /BACKUP/Ubuntu_Backup_17.04LTS and list the files and folders you will see it is exactly the same as your ROOT drive listing.

Restore is exactly the same, it can be a complete restore of simply a folder such as /etc.

Example:

cd /BACKUP/Ubuntu_Backup_17.04LTS

and execute the reverse;

find . -mount -print | cpio -pdumv /  

or for a folder /etc ) the command would be

find /etc -mount -print | cpio -pdumv /  

This will copy /etc folder out of Backup to / for recovery.

This will then copy all files and folders back to / partition then reboot. It is very handy when an update goes horribly wrong.

Stephen Rauch
  • 4,209
  • 14
  • 22
  • 32