What is cpio archive and how to extract and create cpio ?

It is a common question what do you mean by cpio archive and what is its use when we have gzip/xz, etc compressions already in place. And how to create a cpio archive, how to extract an archive, etc.

So lets have a look at these questions one by one:

1. What is cpio ?

cpio stands for "copy in and copy out"

It means copy in to an archive and copy out from an archive.

Hence CPIO can be defined as a GNU tool for creating, and 
extracting archives, or copying files from one place to another just like tar.

By default, cpio command creates binary format archives, 
which helps with the compatibility alongwith older cpio programs. 

However it supports multiple format archives including binary, old ASCII, 
new ASCII, HPUX binary, HPUX old ASCII, CRC, old tar, and POSIX.1 new tar.

2. Creating cpio archive
For creating cpio archive, we need to run CPIO command in copy out mode with {-o|–create} option to create archive.
Copy-out mode:- In copy-out mode, cpio copies files into an archive.

[root@nglinux test]# touch file1 file2 file3

[root@nglinux test]# ls -altr
total 16
dr-xr-x---. 32 root root 12288 Dec 29 02:54 ..
-rw-r--r--.  1 root root     0 Dec 29 02:54 file1
-rw-r--r--.  1 root root     0 Dec 29 02:54 file2
-rw-r--r--.  1 root root     0 Dec 29 02:54 file3
drwxr-xr-x.  2 root root  4096 Dec 29 02:54 .

[root@nglinux test]# echo "hello1" > file1;
[root@nglinux test]# echo "hello2" > file2;
[root@nglinux test]# echo "hello3" > file3;
[root@nglinux test]# ls | cpio -ov > ./new.cpio
file1
file2
file3
new.cpio
1 block

[root@nglinux test]# ls -ltr
total 16
-rw-r--r--. 1 root root   7 Dec 29 02:55 file1
-rw-r--r--. 1 root root   7 Dec 29 02:55 file2
-rw-r--r--. 1 root root   7 Dec 29 02:55 file3
-rw-r--r--. 1 root root 512 Dec 29 02:55 new.cpio
[root@nglinux test]# 

As we can see above the new cpio archive new.cpio is created.
Now we will extract this archive and see how to extract a CPIO archive.

3. Extracting a CPIO archive
Lets extract the file new.cpio.
For extraction, we will use the copy in mode.

I. Copy-in mode: In copy-in mode, cpio copies files out of an archive or lists the archive contents.
{-i |–extract} option will be used alngwith -d [–make-directories ] and -v [–verbose].

[root@nglinux test]# cpio -idv < new.cpio 
cpio: file1 not created: newer or same age version exists
file1
cpio: file2 not created: newer or same age version exists
file2
cpio: file3 not created: newer or same age version exists
file3
cpio: new.cpio not created: newer or same age version exists
new.cpio
1 block

Ohh !! It seems the files in archive all exists here, lets create a new directory and extract the contents inside that directory.

[root@nglinux test]# mkdir test2; cd test2

Now lets extract the archive here.

[root@nglinux test2]# cpio -idv < ../new.cpio
file1
file2
file3
new.cpio
1 block

[root@nglinux test2]# ls -ltr
total 12
-rw-r--r--. 1 root root 7 Dec 29 03:01 file3
-rw-r--r--. 1 root root 7 Dec 29 03:01 file2
-rw-r--r--. 1 root root 7 Dec 29 03:01 file1
-rw-r--r--. 1 root root 0 Dec 29 03:01 new.cpio

[root@nglinux test2]# cat file*
hello1
hello2
hello3
[root@nglinux test2]# 

It seems fine now. So we have seen how to create and extract cpio archives.

However there is one more option or feature provided by CPIO archive which is known as copy-pass mode.

II. Copy Pass Mode of CPIO archive
Copy-pass mode: 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.

For using this mode, we will use the { -p |–pass-through} option alongwith -d(to make directories) and ‘-m, to preserve-modification-time’.

Lets see an example of this:
a. First create a new directory where we will copy the contents.

[root@nglinux ~]# mkdir /mnt/test

b. Now goto the path from where you want to copy the files.

[root@nglinux ~]# cd test/
[root@nglinux test]# ls
file1  file2  file3  new.cpio  test2

c. Now use cpio command to copy the contents by compressing and decompressing it.

[root@nglinux test]# find . -depth | cpio -pmdv /mnt/test/
/mnt/test//./file3
/mnt/test//./file1
/mnt/test//./test2/file3
/mnt/test//./test2/file1
/mnt/test//./test2/file2
/mnt/test//./test2/new.cpio
/mnt/test//./test2
/mnt/test//./file2
/mnt/test//./new.cpio
2 blocks
[root@nglinux test]# ls -l /mnt/test/
total 20
-rw-r--r--. 1 root root    7 Dec 29 02:55 file1
-rw-r--r--. 1 root root    7 Dec 29 02:55 file2
-rw-r--r--. 1 root root    7 Dec 29 02:55 file3
-rw-r--r--. 1 root root  512 Dec 29 02:55 new.cpio
drwxr-xr-x. 2 root root 4096 Dec 29 03:01 test2
[root@nglinux test]# 

These are the three modes that are supported by cpio archive.

Further, it is interesting to know that Redhat package manager uses CPIO format archives for creating their RPM packages.

Do subscribe to our blog to stay updated with latest articles.

Leave a Reply

Your email address will not be published.