Linux/UNIX cpio (copy in/out) command copies, lists, and extracts files to and from a single file or archives. Some of the options available with cpio command are listed in the below table. The cpio command requires that one of the o, i, or p options must be specified.
Option | Description |
-o | Copies data. |
-i | Extracts from a copy. |
-t | Lists copy contents. |
-v | Verbose mode. |
-p | Reads from a copy to get pathnames. |
-a | Resets access times on files after they are copied. |
In this post, we will discuss few examples to understand the usage of cpio command.
Example 1: To copy the contents of /home, run the find command as demonstrated and redirect the output to /tmp/home.cpio.
# find /home | cpio -ov > /tmp/home.cpio /home /home/geek /home/geek/CentOS-7.0-1406-x86_64-DVD.iso.3 /home/geek/CentOS-7.0-1406-x86_64-DVD.iso.4 /home/geek/.profile /home/geek/CentOS-7.0-1406-x86_64-DVD.iso.2 /home/geek/.bashrc ...............................................
Example 2: To list the contents of home.cpio.
# cpio -itv < /tmp/home.cpio -rw-rw-r-- 1 geek geek 2556 Nov 29 13:03 /home/geek/ubuntu -rw-rw-r-- 1 geek geek 2255 Sep 1 14:32 /home/geek/.grive-last-sync.log drwxr-xr-x 17 geek geek 0 Dec 5 15:46 /home/geek/Desktop -rw-rw-r-- 1 geek geek 357376 Sep 11 12:23 /home/geek/Desktop/mysql-classroom.doc -rw-rw-r-- 1 geek geek 43008 Sep 27 13:16 /home/geek/Desktop/linux_interview_question.doc drwxrwxr-x 3 geek geek 0 Sep 12 13:59 /home/geek/Desktop/July-2014 -rw-rw-r-- 1 geek geek 8147 Sep 11 13:04 /home/geek/Desktop/July-2014/brainuse.php -rw-rw-r-- 1 geek geek 10885 Sep 11 13:16 /home/geek/Desktop/July-2014/news.php drwxrwxr-x 2 geek geek 0 Sep 11 12:34 /home/geek/Desktop/July-2014/images -rw-rw-r-- 1 geek geek 13609 Sep 11 12:34 //cdn.thegeekdiary.com/home/geek/Desktop/July-2014/images/3.gif -rw-rw-r-- 1 geek geek 206334 Sep 11 12:34 //cdn.thegeekdiary.com/home/geek/Desktop/July-2014/images/July14-header.gif -rw-rw-r-- 1 geek geek 1736 Sep 11 12:34 //cdn.thegeekdiary.com/home/geek/Desktop/July-2014/images/2.gif ..............................................
Example 3: To restore files from home.cpio.
# cpio -iv < /tmp/home.cpio
Example 4: To copy files directly from /home into a new directory called /tmp/home.bkp.
# find /home | cpio -pvd /tmp/home.bkp /tmp/home.bkp/home/geek/ubuntu /tmp/home.bkp/home/geek/.grive-last-sync.log /tmp/home.bkp/home/geek/Desktop /tmp/home.bkp/home/geek/Desktop/mysql-classroom.doc /tmp/home.bkp/home/geek/Desktop/linux_interview_question.doc /tmp/home.bkp/home/geek/Desktop/July-2014 ............................................
Example 5: Copy only selected files to the home.cpio.
# find . -iname *.php -print | cpio -ov >/tmp/home.cpio ./Desktop/July-2014/brainuse.php ./Desktop/July-2014/news.php ./Desktop/July-2014/developer_section.php ./Desktop/July-2014/mysql1.php ............................
Above Command will copy all files with ‘.php’ extension in home.cpio.
Example 6: Creating ‘.tar’ archive using 'cpio -F'.
# find . -iname *.php -print | cpio -ov -H tar -F /tmp/home.tar ./Desktop/July-2014/brainuse.php ./Desktop/July-2014/news.php ./Desktop/July-2014/developer_section.php ./Desktop/July-2014/mysql1.php ./Desktop/July-2014/index.php ............................................
The above command will create a tar archive ‘home.tar’ of all the files with extension ‘.php’ using “cpio -F”.
Example 7: list the contents of “.tar” file using cpio.
# cpio -it -F /tmp/home.tar Desktop/July-2014/brainuse.php Desktop/July-2014/news.php Desktop/July-2014/developer_section.php Desktop/July-2014/mysql1.php Desktop/July-2014/index.php Desktop/July-2014/linux1.php ..................................
Example 8: Extract “.tar’ archive via cpio.
# cpio -idv -F /tmp/home.tar