Linux cpio Examples: How to Create and Extract cpio Archives (and tar archives)

by Balakrishnan Mariyappan on August 26, 2010

cpio command is used to process archive files (for example, *.cpio or *.tar files).

cpio stands for “copy in, copy out”.

cpio performs the following three operations.

  • Copying files to an archive
  • Extracting files from an archive
  • Passing files to another directory tree

cpio takes the list of files from the standard input while creating an archive, and sends the output to the standard output.

1. Create *.cpio Archive File

You can create a *.cpio archive that contains files and directories using cpio -ov

$ cd objects

$ ls
file1.o file2.o file3.o

$ ls | cpio -ov > /tmp/object.cpio

As seen above, the ls command passes the three object filenames to cpio command and cpio generates the object.cpio archive.

2. Extract *.cpio Archive File

cpio extract: To extract a given *.cpio file, use cpio -iv as shown below.

$ mkdir output

$ cd output

$ cpio -idv < /tmp/object.cpio

3. Create *.cpio Archive with Selected Files

The following example creates a *.cpio archive only with *.c files.

$ find . -iname *.c -print | cpio -ov >/tmp/c_files.cpio

4. Create *.tar Archive File using cpio -F

We already know how to use the tar command effectively.

Did you know that you can also use cpio command to create tar files as shown below?

$ ls | cpio -ov -H tar -F sample.tar

As seen above, instead of redirecting the standard output you can mention the output archive filename with the option -F.

5. Extract *.tar Archive File using cpio command

You can also extract a tar file using cpio command as shown below.

$ cpio -idv -F sample.tar

6. View the content of *.tar Archive File

To view the content of *.tar file, do the following.

$ cpio -it -F sample.tar

7. Create a *.cpio Archive with the Original files that a Symbolic Link Points

cpio archive can be created with the original files that a symbolic link is referring to as shown below.

$ ls | cpio -oLv >/tmp/test.cpio

8. Preserve the File Modification Time while restoring *.cpio

The modification time of the files can be preserved when we are restoring the cpio archive files as shown below.

$ ls | cpio -omv >/tmp/test.cpio

9. Manipulate Linux and Kernel image files using cpio

How to View, Modify and Recreate initrd.img – As we discussed a while back, we can also use cpio command to manipulate initrd.img file.

10. Copy Directory Tree from One to Another

cpio allows you to copy one directory contents into another directory without creating an intermediate archive. It reads the file list from the standard input and pass it to the target directory.

The example below copies the files and sub-directories of objects directory into /mnt/out directory.

$ mkdir /mnt/out

$ cd objects

$ find . -depth | cpio -pmdv /mnt/out

In the above example:

  • cpio option -p makes cpio to use pass through mode. Its like piping cpio -o into cpio -i.
  • cpio option -d creates leading directories as needed in the target directory.

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 7 comments… read them below or add one }

1 Sasikala August 26, 2010 at 1:16 am

cpio – an intelligent and useful command..
Thanks for such a great article..

2 Jufa August 26, 2010 at 11:37 pm

Great examples. How about examples of sending the archive to remote tape drive in one line?

3 Pankaj September 2, 2010 at 3:59 am

why we use ‘cpio’ when we have ‘tar’ to archive the files and folders,

Is cpio used for specific reason.

4 Sandeep Maurya November 25, 2010 at 2:12 am

Thanks a lot of thanks for doc of CPIO. its good for begineers…

5 Carsten December 26, 2011 at 12:04 pm

why is cpio better than tar? see here

6 kaji Rafiqul islam June 3, 2013 at 1:23 am

Is there any reason to used CPIO and not to used TAR ?

7 sluge September 13, 2013 at 5:44 am

Is any way to copy files via ssh using cpio?

Leave a Comment

Previous post:

Next post: