LinuxCommandLibrary

cpio

Archive utility with stdin file lists

TLDR

Create archive

$ find . -depth | cpio -o > [archive.cpio]
copy
Extract archive
$ cpio -i < [archive.cpio]
copy
List archive contents
$ cpio -t < [archive.cpio]
copy
Copy files
$ find . | cpio -pd [/destination]
copy

SYNOPSIS

cpio [options]

DESCRIPTION

cpio (copy in/out) is an archiving utility that reads file lists from stdin and creates or extracts archives. It's commonly used in initramfs images, RPM packages, and for copying directory trees.
The tool operates in three modes: copy-out (create archives), copy-in (extract archives), and pass-through (copy files without archiving). Unlike tar, which accepts file arguments directly, cpio reads filenames from standard input, typically piped from find. This design provides greater flexibility for file selection using find's powerful filtering capabilities.
cpio is the preferred format for Linux initial ramdisk images because of its simplicity and kernel support. RPM packages use cpio internally for storing files. The format supports various archive types and can preserve file metadata including permissions, ownership, and timestamps.

PARAMETERS

-o, --create

Create archive (copy-out mode)
-i, --extract
Extract archive (copy-in mode)
-p, --pass-through
Copy files (pass-through mode)
-t, --list
List archive contents
-v, --verbose
Verbose mode
-d, --make-directories
Create directories as needed
-u, --unconditional
Overwrite files unconditionally
-m, --preserve-modification-time
Preserve modification times

MODES

Copy-out (-o):

Create archive from file list
Copy-in (-i):
Extract from archive
Pass-through (-p):
Copy files without archiving

WORKFLOW

$ # Create archive
find . -name "*.txt" | cpio -o > textfiles.cpio

# Create with verbose output
find . | cpio -ov > backup.cpio

# Extract archive
cpio -i < archive.cpio

# Extract with verbose
cpio -idv < archive.cpio

# List contents
cpio -t < archive.cpio

# Copy directory tree (like cp -r but preserves more)
find /source | cpio -pdm /destination

# Extract specific files
cpio -i "*.conf" < archive.cpio
copy

COMPRESSION

$ # Create compressed archive
find . | cpio -o | gzip > archive.cpio.gz

# Extract compressed archive
gunzip < archive.cpio.gz | cpio -i

# With bzip2
find . | cpio -o | bzip2 > archive.cpio.bz2
zcat archive.cpio.bz2 | cpio -i
copy

COMMON USES

Initramfs extraction:

$ mkdir initramfs
cd initramfs
gunzip -c /boot/initramfs.img | cpio -i
copy
RPM content extraction:
$ rpm2cpio package.rpm | cpio -idmv
copy

CAVEATS

Less intuitive than tar. Requires file list from stdin. No built-in compression. Pathname length limits. Different behavior between GNU and POSIX versions. Not as widely used as tar.

HISTORY

cpio was included in Unix PWB/UNIX in 1977 and became part of POSIX, predating tar and offering more flexible file selection.

SEE ALSO

tar(1), find(1), pax(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community