Wednesday, December 10, 2014

cpio command – copies, lists & extracts files to and from archives

http://www.nextstep4it.com/linux-cpio-command-examples

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:
root@nextstep4it:~# find /home | cpio -ov > /tmp/home.cpio
/home
/home/nextstep4it
/home/nextstep4it/CentOS-7.0-1406-x86_64-DVD.iso.3
/home/nextstep4it/CentOS-7.0-1406-x86_64-DVD.iso.4
/home/nextstep4it/.profile
/home/nextstep4it/CentOS-7.0-1406-x86_64-DVD.iso.2
/home/nextstep4it/.bashrc
...............................................

Example:2 To list the contents of home.cpio
root@nextstep4it:~# cpio -itv < /tmp/home.cpio
-rw-rw-r--   1 nextstep nextstep     2556 Nov 29 13:03 /home/nextstep4it/ubuntu
-rw-rw-r--   1 nextstep nextstep     2255 Sep  1 14:32 /home/nextstep4it/.grive-last-sync.log
drwxr-xr-x  17 nextstep nextstep        0 Dec  5 15:46 /home/nextstep4it/Desktop
-rw-rw-r--   1 nextstep nextstep   357376 Sep 11 12:23 /home/nextstep4it/Desktop/mysql-classroom.doc
-rw-rw-r--   1 nextstep nextstep    43008 Sep 27 13:16 /home/nextstep4it/Desktop/linux_interview_question.doc
drwxrwxr-x   3 nextstep nextstep        0 Sep 12 13:59 /home/nextstep4it/Desktop/July-2014
-rw-rw-r--   1 nextstep nextstep     8147 Sep 11 13:04 /home/nextstep4it/Desktop/July-2014/brainuse.php
-rw-rw-r--   1 nextstep nextstep    10885 Sep 11 13:16 /home/nextstep4it/Desktop/July-2014/news.php
drwxrwxr-x   2 nextstep nextstep        0 Sep 11 12:34 /home/nextstep4it/Desktop/July-2014/images
-rw-rw-r--   1 nextstep nextstep    13609 Sep 11 12:34 /home/nextstep4it/Desktop/July-2014/images/3.gif
-rw-rw-r--   1 nextstep nextstep   206334 Sep 11 12:34 /home/nextstep4it/Desktop/July-2014/images/July14-header.gif
-rw-rw-r--   1 nextstep nextstep     1736 Sep 11 12:34 /home/nextstep4it/Desktop/July-2014/images/2.gif
..............................................

Example:3 To restore files from home.cpio
root@nextstep4it:~# cpio -iv < /tmp/home.cpio

Example:4 To copy files directly from /home into a new directory called /tmp/home.bkp
root@nextstep4it:~# find /home | cpio -pvd /tmp/home.bkp
/tmp/home.bkp/home/nextstep4it/ubuntu
/tmp/home.bkp/home/nextstep4it/.grive-last-sync.log
/tmp/home.bkp/home/nextstep4it/Desktop
/tmp/home.bkp/home/nextstep4it/Desktop/mysql-classroom.doc
/tmp/home.bkp/home/nextstep4it/Desktop/linux_interview_question.doc
/tmp/home.bkp/home/nextstep4it/Desktop/July-2014
............................................
Example:5 Copy only selected files to the home.cpio
root@nextstep4it:~# 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’
root@nextstep4it:~# 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
............................................
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
root@nextstep4it:~# 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
root@nextstep4it:~# cpio -idv -F /tmp/home.tar

No comments:

Post a Comment