cpio Command in Linux: Archive, Extract & Pass-Through (Ubuntu)

The cpio command archives, lists, and extracts files on Linux. It reads file names from standard input, so it pairs naturally with find for flexible backups, initramfs images, and directory copies.

Published

Updated

Read time 10 min read

Reviewed byDeepak Prasad

cpio Command in Linux: Archive, Extract & Pass-Through (Ubuntu)
About The cpio command archives, lists, and extracts files on Linux. It reads file names from standard input, so it pairs naturally with find for flexible backups, initramfs images, and directory copies.
Tested on Ubuntu 25.04 (Plucky Puffin); cpio (GNU cpio) 2.15; kernel 7.0.0-27-generic
Package cpio (apt/deb) · cpio (dnf/rpm)
Man page cpio(1)
Privilege file owner / root
Distros

GNU cpio on Linux and BSD (options may differ).

Simple fixed file lists: tar.

Related guide

cpio — quick reference

Copy-out — create archives (-o)

Read path names from stdin and write an archive to stdout or -F.

When to use Command
Create a cpio archive from paths find prints find DIR -print | cpio -o > archive.cpio
Verbose create — print each path as it is archived find DIR -print | cpio -ov > archive.cpio
Write the archive to a named file instead of stdout find DIR -print | cpio -o -F archive.cpio
Append paths to an existing archive find NEW -print | cpio -oA -F archive.cpio
Follow symlinks and archive the target files (-L) find DIR -print | cpio -oL > archive.cpio
Use tar archive format instead of default binary cpio find DIR -print | cpio -o -H tar -F archive.tar
Pipe through gzip for compression find DIR -print | cpio -o | gzip > archive.cpio.gz

Copy-in — extract and list (-i)

Read an archive from stdin or -F / -I.

When to use Command
Extract all members into the current directory cpio -i < archive.cpio
Extract and create missing directories (-d) cpio -id < archive.cpio
Extract with verbose path listing cpio -idv < archive.cpio
List archive contents without extracting (-t) cpio -it < archive.cpio
Verbose table of contents cpio -itv < archive.cpio
Extract only named members cpio -iv PATTERN < archive.cpio
Extract into another directory (-D) cpio -idD /target/dir < archive.cpio
Preserve modification times from the archive (-m) cpio -im < archive.cpio
Overwrite existing files unconditionally (-u) cpio -iu < archive.cpio
Read archive from a file cpio -i -F archive.cpio
Decompress gzip archive on the fly gzip -dc archive.cpio.gz | cpio -id

Pass-through — copy trees (-p)

Copy files from stdin paths directly to a destination directory without a separate archive file.

When to use Command
Copy a directory tree to another path find SRC -print | cpio -pd DEST
Pass-through with verbose output find SRC -print | cpio -pdv DEST
Create leading directories as needed find SRC -print | cpio -pd DEST

Help and version

When to use Command
Show brief usage cpio --help
Show package version cpio --version

cpio — command syntax

Synopsis from cpio --help on Ubuntu 25.04 (cpio GNU cpio 2.15):

text
cpio [OPTION...] [destination-directory]

Examples:
  # Copy files named in name-list to the archive
  cpio -o < name-list [> archive]
  # Extract files from the archive
  cpio -i [< archive]
  # Copy files named in name-list to destination-directory
  cpio -p destination-directory < name-list

cpio runs in one main mode per invocation: copy-out (-o), copy-in (-i), or pass-through (-p). File names almost always come from a pipe — typically find … -print.


cpio — command examples

Essential Create an archive with find and copy-out mode

cpio does not take file arguments on the command line. Pipe path names from find into copy-out mode (-o) and redirect stdout to an archive file.

Prepare a small tree and archive it:

bash
mkdir -p /tmp/cpio-lab/src/sub
echo hello > /tmp/cpio-lab/src/file1.txt
echo world > /tmp/cpio-lab/src/sub/file2.txt
cd /tmp/cpio-lab/src
find . -print | cpio -o > /tmp/cpio-lab/archive.cpio

Sample output (stderr):

text
1 block

The block count goes to stderr; the archive bytes go to stdout. Keep them separate when scripting.

Essential List archive contents before extracting

Always inspect unfamiliar archives with -it (list) before -i (extract) so you know which paths will land on disk.

Run the command:

bash
cpio -itv < /tmp/cpio-lab/archive.cpio

Sample output:

text
1 block
drwxr-xr-x   3 root     root            0 Jul  1 17:48 .
lrwxrwxrwx   1 root     root            9 Jul  1 17:48 link1 -> file1.txt
-rw-r--r--   1 root     root            6 Jul  1 17:48 file1.txt
drwxr-xr-x   2 root     root            0 Jul  1 17:48 sub
-rw-r--r--   1 root     root            6 Jul  1 17:48 sub/file2.txt

No files are written — this is a safe preview of members and metadata.

Essential Extract an archive into the current directory

Copy-in mode with -i reads the archive from stdin. Add -d so missing parent directories are created automatically.

Run the command:

bash
mkdir -p /tmp/cpio-lab/extract && cd /tmp/cpio-lab/extract
cpio -idv < /tmp/cpio-lab/archive.cpio

Sample output:

text
.
link1
file1.txt
sub
sub/file2.txt
1 block

Verify the tree:

bash
find . -type f

Sample output:

text
./file1.txt
./sub/file2.txt

Paths from the archive are recreated relative to your current directory.

Common Copy a tree without an intermediate archive

Pass-through mode (-p) copies files straight to a destination directory — useful when you want cp -a-like behaviour with find filtering in the middle.

Run the command:

bash
rm -rf /tmp/cpio-lab/dest
mkdir -p /tmp/cpio-lab/dest
cd /tmp/cpio-lab/src
find . -print | cpio -pd /tmp/cpio-lab/dest

Sample output:

text
1 block

Check the destination:

bash
find /tmp/cpio-lab/dest -type f

Sample output:

text
/tmp/cpio-lab/dest/file1.txt
/tmp/cpio-lab/dest/sub/file2.txt

No .cpio file was created — data went directly from source paths to dest.

Common Compress an archive with gzip

cpio does not compress by itself. Pipe copy-out stdout through gzip and reverse the pipeline on extract.

Create a compressed archive:

bash
cd /tmp/cpio-lab/src
find . -print | cpio -o 2>/dev/null | gzip > /tmp/cpio-lab/archive.cpio.gz

List members without extracting:

bash
gzip -dc /tmp/cpio-lab/archive.cpio.gz | cpio -it

Sample output:

text
1 block
.
link1
file1.txt
sub
sub/file2.txt

The same pattern works with xz or zstd if those tools are installed.

Common Extract into a fixed directory with -D

When you should not cd into the target path first, -D sets the extraction root explicitly.

Run the command:

bash
rm -rf /tmp/cpio-lab/extract3
mkdir /tmp/cpio-lab/extract3
cpio -idD /tmp/cpio-lab/extract3 < /tmp/cpio-lab/archive.cpio

Sample output:

text
.
link1
file1.txt
sub
sub/file2.txt
1 block

Confirm files landed under the chosen directory:

bash
ls -R /tmp/cpio-lab/extract3

Sample output:

text
/tmp/cpio-lab/extract3:
file1.txt
link1
sub

/tmp/cpio-lab/extract3/sub:
file2.txt
Common Extract only matching members

Pass member path names as arguments to copy-in mode to pull a subset from a large archive.

Run the command:

bash
mkdir -p /tmp/cpio-lab/single && cd /tmp/cpio-lab/single
cpio -iv file1.txt < /tmp/cpio-lab/archive.cpio

Sample output:

text
file1.txt
1 block

Verify only the requested file exists:

bash
ls -la

Sample output:

text
total 4
drwxr-xr-x 2 root root  60 Jul  1 17:48 .
drwxr-xr-x 8 root root 280 Jul  1 17:48 ..
-rw-r--r-- 1 root root   6 Jul  1 17:48 file1.txt
Advanced Write or read a tar-format archive with -H tar

GNU cpio can emit and read tar-format archives when another tool in the pipeline expects .tar instead of binary cpio.

Create a tar-format archive:

bash
cd /tmp/cpio-lab/src
find . -print | cpio -o -H tar -F /tmp/cpio-lab/archive.tar

List it:

bash
cpio -it < /tmp/cpio-lab/archive.tar

Sample output:

text
8 blocks
./
link1
file1.txt
sub/
sub/file2.txt

You can extract with cpio -i -F archive.tar or hand the file to tar if that fits the workflow better.


cpio — when to use / when not

Use cpio when Use something else when
  • File selection comes from find rules, not a fixed argument list
  • You are building or inspecting initramfs images on Linux
  • You need pass-through copy (-p) with pipeline filtering in the middle
  • You want tar-format output but already have a find | cpio pipeline
  • You have a short, known file list → tar
  • You want one command with built-in compression → tar -czf
  • You need random access or updating single members in a huge archive → tar or dedicated backup tools
  • You only need a quick directory sync → rsync or cp -a

cpio vs tar

cpio tar
Input Path names on stdin File arguments or -T list
File selection Excellent with find Best for known paths
initramfs Native on Linux kernels Uncommon
Compression External pipe (gzip, xz) Built-in -z, -J, etc.
Learning curve Steeper Gentler for beginners

See the tar command cheat sheet for everyday archiving.


Command One line
cpio Pipeline-friendly archiver (this page)
tar Default archiver for simple file lists
gzip Compress cpio stdout
dd Block-level copy (different job than file archive)

Browse the full index in our Linux commands reference.


cpio — interview corner

What is the cpio command used for?

cpio (copy in / copy out) archives, lists, and extracts files. Unlike tar, it reads file names from standard input instead of argv — so admins pair it with find for rule-based backups.

Linux initramfs images are often built with find . | cpio -o -H newc. That pipeline pattern is why cpio stays relevant even when tar is easier for day-to-day archives.

A strong answer is:

"cpio is a pipeline archiver — find selects paths, cpio packs or unpacks them. I see it for initramfs, filtered backups, and pass-through copies."

What are the three cpio modes?

Each run uses exactly one main mode:

Flag Name Job
-o copy-out Build an archive from stdin path list
-i copy-in Extract or list from an archive
-p pass-through Copy stdin paths to a destination dir

Example mental model:

text
find . -print | cpio -o > backup.cpio    # archive
cpio -i < backup.cpio                    # extract
find . -print | cpio -p /other/path      # direct copy

A strong answer is:

"copy-out creates, copy-in extracts or lists, pass-through copies without a middle file — all fed by path names on stdin."

When would you pick cpio over tar?

Use tar when you already know the files: tar -czf backup.tar.gz dir/.

Use cpio when selection is dynamic:

bash
find /etc -name '*.conf' -print | cpio -o > configs.cpio

initramfs generation and filtered system backups are the classic cpio wins.

A strong answer is:

"tar for simple known sets; cpio when find rules drive membership or when I'm building initramfs-style images."

Does cpio compress archives?

Not by itself. Copy-out writes an uncompressed stream to stdout unless you pipe through gzip or similar:

bash
find . -print | cpio -o | gzip > archive.cpio.gz
gzip -dc archive.cpio.gz | cpio -id

A strong answer is:

"cpio doesn't compress — I pipe to gzip or xz on create and through the matching decompressor on extract."


Troubleshooting

Symptom Likely cause Fix
cpio: Malformed number on extract Archive file contains stderr text or is truncated Recreate with cpio -o > file and redirect stderr separately (2>log)
newer or same version exists Existing file is newer; default is not to overwrite Add -u to overwrite unconditionally
Empty archive or 0 blocks Nothing on stdin Confirm find prints paths; use find … -print not silent filters
Wrong tree layout after extract Ran extract from unexpected cwd Use cpio -idD /target/dir or cd to the intended root first
cpio: … not created: truncated or corrupt Incomplete download or broken pipe Verify archive size; regenerate from source

References

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.