Linux Professional Institute LPIC-1 Exam 101

GNU and Unix Commands

Perform Basic File Management Part 2 cpio

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

Taskcpio OptionDescription
Create an archive-oCopy out files into a new archive
Extract files from an archive-iCopy in files from an existing archive
Create directories as needed-dAutomatically make parent directories on extract

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)

Note

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:

ls | cpio -o > 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:

find . -depth | cpio -o > project.cpio

2. Extracting an Archive

Unpack the contents of archive.cpio, creating directories as needed:

cpio -id < archive.cpio

Breakdown:

  • cpio -i reads an archive from stdin.
  • -d ensures that all required directories are created.
  • < archive.cpio feeds the archive into cpio.

Warning

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

OptionDescription
-oCreate an archive (Copy Out)
-iExtract from an archive (Copy In)
-dCreate directories as needed when extracting
-vVerbose mode (list files processed)
-H fmtSelect archive format (e.g., newc, odc)

Watch Video

Watch video content

Previous
Perform Basic File Management Part 2 Regular expressions 2