| Tested on | Ubuntu 25.04 (Plucky Puffin) |
|---|---|
| Package | cpio (GNU cpio) 2.15cpio (GNU cpio) 2.15 |
| Applies to | Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Oracle Linux, CentOS Stream, Fedora, Arch Linux, SUSE, openSUSE, Alpine |
| Privilege | sudo or root |
| Man page | cpio(1) |
| Scope | 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. |
| Related guides | gzip command cheat sheet Linux commands |
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):
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-listcpio 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:
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.cpioSample output (stderr):
1 blockThe 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:
cpio -itv < /tmp/cpio-lab/archive.cpioSample output:
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.txtNo 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:
mkdir -p /tmp/cpio-lab/extract && cd /tmp/cpio-lab/extract
cpio -idv < /tmp/cpio-lab/archive.cpioSample output:
.
link1
file1.txt
sub
sub/file2.txt
1 blockVerify the tree:
find . -type fSample output:
./file1.txt
./sub/file2.txtPaths 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:
rm -rf /tmp/cpio-lab/dest
mkdir -p /tmp/cpio-lab/dest
cd /tmp/cpio-lab/src
find . -print | cpio -pd /tmp/cpio-lab/destSample output:
1 blockCheck the destination:
find /tmp/cpio-lab/dest -type fSample output:
/tmp/cpio-lab/dest/file1.txt
/tmp/cpio-lab/dest/sub/file2.txtNo .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:
cd /tmp/cpio-lab/src
find . -print | cpio -o 2>/dev/null | gzip > /tmp/cpio-lab/archive.cpio.gzList members without extracting:
gzip -dc /tmp/cpio-lab/archive.cpio.gz | cpio -itSample output:
1 block
.
link1
file1.txt
sub
sub/file2.txtThe 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:
rm -rf /tmp/cpio-lab/extract3
mkdir /tmp/cpio-lab/extract3
cpio -idD /tmp/cpio-lab/extract3 < /tmp/cpio-lab/archive.cpioSample output:
.
link1
file1.txt
sub
sub/file2.txt
1 blockConfirm files landed under the chosen directory:
ls -R /tmp/cpio-lab/extract3Sample output:
/tmp/cpio-lab/extract3:
file1.txt
link1
sub
/tmp/cpio-lab/extract3/sub:
file2.txtCommon 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:
mkdir -p /tmp/cpio-lab/single && cd /tmp/cpio-lab/single
cpio -iv file1.txt < /tmp/cpio-lab/archive.cpioSample output:
file1.txt
1 blockVerify only the requested file exists:
ls -laSample output:
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.txtAdvanced Archive symlink targets with -L
By default, cpio stores symlinks as links. Use -L in copy-out mode when you need the referent file bytes in the archive (some packaging pipelines require this).
Run the command:
cd /tmp/cpio-lab/src
find . -print | cpio -oL > /tmp/cpio-lab/resolved.cpio
cpio -it < /tmp/cpio-lab/resolved.cpioSample output:
1 block
.
link1
file1.txt
sub
sub/file2.txtCompare with a normal archive that records link1 as a symlink in cpio -itv long listing. With -L, the archive contains the target file data instead of a link entry.
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:
cd /tmp/cpio-lab/src
find . -print | cpio -o -H tar -F /tmp/cpio-lab/archive.tarList it:
cpio -it < /tmp/cpio-lab/archive.tarSample output:
8 blocks
./
link1
file1.txt
sub/
sub/file2.txtYou 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 |
|---|---|
|
|
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.
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:
find . -print | cpio -o > backup.cpio # archive
cpio -i < backup.cpio # extract
find . -print | cpio -p /other/path # direct copyA 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:
find /etc -name '*.conf' -print | cpio -o > configs.cpioinitramfs 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:
find . -print | cpio -o | gzip > archive.cpio.gz
gzip -dc archive.cpio.gz | cpio -idA strong answer is:
"cpio doesn't compress — I pipe to gzip or xz on create and through the matching decompressor on extract."
How does cpio handle symbolic links?
By default it stores symlink entries as links. Add -L in copy-out mode to follow symlinks and archive the target file content instead — common in packaging when the consumer does not need link metadata.
A strong answer is:
"Default cpio keeps symlinks as links; -L dereferences and archives the referent file — I pick based on whether the archive must preserve link structure."
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
- Linux initramfs documentation (kernel.org)
