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
Example 3: To restore files from home.cpio.
# cpio -iv
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