≡ Menu

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

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.
Add your comment

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

Comments on this entry are closed.

  • Sasikala August 26, 2010, 1:16 am

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

  • Jufa August 26, 2010, 11:37 pm

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

  • Pankaj September 2, 2010, 3:59 am

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

    Is cpio used for specific reason.

  • Sandeep Maurya November 25, 2010, 2:12 am

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

  • Carsten December 26, 2011, 12:04 pm

    why is cpio better than tar? see here

  • kaji Rafiqul islam June 3, 2013, 1:23 am

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

  • sluge September 13, 2013, 5:44 am

    Is any way to copy files via ssh using cpio?

  • F0bos May 30, 2014, 4:34 am

    Nice stuff, but if you want to create cpio archive recursively you need to use “find .” instead of “ls”

  • Drink May 14, 2015, 3:07 am

    @Carsten: this post is from 2001 – since then all these limitations have been fixed I believe, e.g. you can pass a list of files from stdin to tar as well, special files are properly handled, …