When dealing with massive tape backups or legacy Unix software repositories, you will often encounter .cpio archives instead of the more common .tar files. The cpio (copy in, copy out) utility is a deeply historical archiving tool designed specifically to read a raw list of file paths from the standard input and stream them directly into a single, contiguous archive file. Because it relies heavily on standard input and output streams rather than command-line arguments, its syntax can feel highly unconventional to modern Linux administrators.
How the cpio Command Works
Unlike tar, which accepts a list of files as direct arguments (e.g., tar -cf archive.tar file1 file2), the cpio command expects a stream of file paths. Therefore, you must almost always pair it with the find or ls commands using a bash pipe to generate the list of files you want to archive.
The command operates in three primary modes: Copy-Out (to create an archive), Copy-In (to extract an archive), and Copy-Pass (to copy files to a new directory).
Creating an Archive (Copy-Out Mode)
To create a new archive containing all the files in your current directory, you must use the -o (copy-out) flag. First, use ls to list the files, then pipe that list directly into cpio.
ls | cpio -o > my_backup.cpio
Because cpio writes the actual binary archive data to the standard output, you must use standard bash redirection (>) to push that binary stream into a physical file on your disk (my_backup.cpio).
If you need to archive a complex directory tree recursively, use the find command instead of ls:
find /var/log -type f | cpio -o > log_backup.cpio
Extracting an Archive (Copy-In Mode)
To extract the contents of an existing archive back onto your hard drive, you must use the -i (copy-in) flag. Just as you redirected data out to create the file, you must redirect data in (<) to read it.
cpio -i < my_backup.cpio
Crucial Warning: By default, if you extract a cpio archive and the files already exist on your disk with newer modification timestamps, cpio will refuse to overwrite them. If you absolutely must overwrite the existing files, you must add the -u (unconditional) flag:
cpio -iu < my_backup.cpio
Creating Directories on Extraction
If you archived a complex directory structure using the find command, the archive contains relative paths (like var/log/apache2/error.log). By default, if those directories do not currently exist in your extraction location, cpio will fail. To force it to automatically create any missing directories as it extracts the files, use the -d (make directories) flag.
cpio -id < log_backup.cpio