How do I use cpio command under Linux?

Q. Can you explain how do I use cpio under Linux / UNIX?

ADVERTISEMENTS

A. GNU cpio is a tool for creating and extracting archives, or copying files from one place to another. It handles a number of cpio formats as well as reading and writing tar files. cpio command works just like tar, only better.

As I said earlier, cpio works like tar but it can read input from the “find” command. This is nifty feature. For example you can find out all *.c files and backup with cpio command.# find / -name "*.c" | cpio -o --format=tar > c-file.backup.tar
# find / -iname "*.pl" | cpio -o -H tar > perl-files.tar
You can also specify file name using -F option:# find / -iname "*.pl" | cpio -o -H tar -F perl-files.tarWhere,

  • -o: Create archive
  • -F: Archive filename to use instead of standard input or output. To use a tape drive on another machine as the archive.
  • -H format: Specify file format to use.
  • -i: Restore archive

You can extract archive with the following command:# cpio -i -F perl-files.tarYou can list file inside archive i.e. list contents of the cpio file with following command:# cpio -it -F perl-files.tarYou can write archive of /home to tape (drive /dev/nst0), type the following command:# find /home | cpio -o -H tar -F /dev/nst0Restore backup using following command:# cpio -i -F /dev/nst0Backup /home dir, to remote system tape drive:# find /home | cpio -o -H tar -F user@backup.nixcraft.in:/dev/nst0 --rsh-command=/usr/bin/ssh

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
7 comments… add one
  • ashwani May 29, 2009 @ 14:18

    Very nice info
    can u pls tell how can i use bzip2 compression with it?

    Thanks

  • rajputh Aug 26, 2011 @ 18:41

    Hi all,

    i’m new to unix can any help me full and incremental back scripts for Solaris 10 and RHEL 6 .

    Thnaks in advance.

  • felipe1982 Sep 21, 2011 @ 1:59

    `pax` aims to replace both `tar` and `cpio` as it can read from both, and write to both. In can read files from `find`, too, and can operate in pass-through mode, like `cpio`

  • ronmulin Dec 1, 2011 @ 20:44

    Nicely done. Very helpful. You can also redirect the cpio output to thumb drive
    find /home/jsmtih cpio -ov > media//backup.cpio

  • Hermelinda Poitevint Jan 14, 2012 @ 16:28

    You are awesome! Just wanted to make your day better :)

  • manoj Mar 16, 2012 @ 6:20

    is it possible to add a file to a tar file using cpio command?

  • Alexander Becker Mar 13, 2013 @ 11:37

    # cpio -it -F perl-files.tar

    works

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.