Andrew Mercer
on this page

cpio

Format: .cpio · Type: archiver · First released: 1977 (PWB/UNIX)

cpio ("copy in, copy out") is the other classic Unix archiver. Unlike tar, it reads the list of files from stdin, which makes it a natural partner for find. You'll mostly meet it inside the Linux boot process (initramfs images are cpio archives) and in RPM packages, whose payload is a compressed cpio archive.

Modes

Flag Mode Meaning
-o copy-out Create an archive from a file list on stdin
-i copy-in Extract an archive from stdin
-p pass-through Copy a file tree to another directory
-t (with -i) List contents

Common modifiers: -d create directories, -m preserve mtimes, -v verbose, -u overwrite unconditionally, -H FORMAT archive format, --null/-0 NUL-separated input.

Creating

# newc is the format the kernel uses for initramfs
find . -print0 | cpio --null -o -H newc > ../archive.cpio
find . -print0 | cpio --null -o -H newc | gzip > ../archive.cpio.gz
find . -print0 | cpio --null -o -H newc | zstd -T0 > ../archive.cpio.zst

Extracting and listing

cpio -idmv < archive.cpio
zcat archive.cpio.gz | cpio -idmv
zstd -dc archive.cpio.zst | cpio -idmv
zcat archive.cpio.gz | cpio -t          # list
cpio -idmv '*.conf' < archive.cpio      # only matching files

Pass-through copy

Copy a tree while preserving metadata, similar to cp -a:

find /srv/data -depth -print0 | cpio --null -pdm /backup/

Unpacking an RPM without installing it

An RPM is a header followed by a compressed cpio payload (gzip, xz or zstd depending on the distro and age):

rpm2cpio package.rpm | cpio -idmv
rpm2cpio package.rpm | cpio -t           # list

# Newer RPM versions can also produce a tarball
rpm2archive package.rpm                  # -> package.rpm.tgz

Inspecting initramfs images

Modern initramfs files may be several concatenated cpio archives (for example an uncompressed microcode archive followed by a compressed main archive), so use the distro tools:

# Fedora / RHEL / other dracut systems
lsinitrd /boot/initramfs-$(uname -r).img
lsinitrd --unpack /boot/initramfs-$(uname -r).img   # (newer dracut) or use skipcpio below

# Debian / Ubuntu
lsinitramfs /boot/initrd.img-$(uname -r)
unmkinitramfs /boot/initrd.img-$(uname -r) /tmp/initrd

# Manual, dracut: skip the early microcode archive, then decompress the rest
/usr/lib/dracut/skipcpio /boot/initramfs-$(uname -r).img | zstd -dc | cpio -idmv

See also