Skip to main content
In this lesson, we’ll dive into the cpio utility, a classic UNIX tool for creating and extracting archives. Whether you’re bundling application files or unpacking backup archives, cpio offers a straightforward, scriptable interface.

What Is cpio?

CPIO stands for Copy In, Copy Out. Unlike tar, which handles device archives too, cpio reads and writes archives strictly via standard input and standard output. You typically pipe a file list into cpio to create an archive, or feed an archive into cpio to extract its contents.

Common Use Cases

How cpio Works

  1. Generate a file list (e.g., via find or ls).
  2. Pipe that list to cpio.
  3. Specify mode:
    • -o for output (create archive)
    • -i for input (extract archive)
cpio does not infer the archive format from file extensions. You control its behavior exclusively with command-line options.

1. Creating an Archive

To package all files in the current directory into archive.cpio:
Explanation:
  • ls lists filenames (one per line).
  • The pipe (|) directs that list into cpio.
  • cpio -o writes an archive to stdout.
  • > archive.cpio redirects stdout into your archive file.
You can also use find to include subdirectories:

2. Extracting an Archive

Unpack the contents of archive.cpio, creating directories as needed:
Breakdown:
  • cpio -i reads an archive from stdin.
  • -d ensures that all required directories are created.
  • < archive.cpio feeds the archive into cpio.
By default, cpio preserves file ownership and permissions. Run as root only if you trust the archive contents to avoid overwriting critical system files.

Quick Reference: cpio Options


Watch Video