Linux Command Reference ยท Archives and Compression
Linux cpio command
Reference for the Linux cpio command: create, list and extract archives from a file list, copy directory trees with pass-through mode, and read initramfs images.
cpio copies files into and out of an archive. Unlike tar, it does not walk directories by itself: in copy-out mode it reads a list of file names from standard input and writes an archive to standard output. That makes it a natural partner for find, and it is why you still meet it in RPM packages and in the initramfs images the kernel unpacks at boot.
There are three modes: -o (copy-out, create), -i (copy-in, list or extract) and -p (pass-through, copy a tree without making an archive).
find . -type f | cpio -o > archive.cpio # create cpio -i < archive.cpio # extract cpio -t < archive.cpio # list find src | cpio -pdm /backup # copy a tree
Try it
cpio is not installed in our sandbox, so this example checks for it and falls back to showing the file list that cpio -o would read. The list is the part you control, and it is where most mistakes happen.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
cpio -o [-H FORMAT] [-0] [-v] [-A | -O ARCHIVE] < NAME-LIST > ARCHIVE cpio -i [-d] [-m] [-u] [-v] [-t] [-F ARCHIVE] [PATTERN...] < ARCHIVE cpio -p [-d] [-m] [-l] [-u] [-v] DESTINATION-DIR < NAME-LIST
| Option | Description |
|---|---|
-o, --create |
Copy-out: read names from stdin, write an archive. |
-i, --extract |
Copy-in: extract files from an archive on stdin. |
-p, --pass-through |
Copy the listed files into a directory. |
-t, --list |
With -i, list the contents without extracting. |
-d, --make-directories |
Create leading directories when needed. |
-m, --preserve-modification-time |
Keep file times on extraction. |
-u, --unconditional |
Overwrite existing files, even newer ones. |
-v, --verbose |
Print each name. |
-0, --null |
Names in the list are separated by NUL (pair with find -print0). |
-H FORMAT |
Archive format; newc is the one used for initramfs images. |
-F FILE, -O FILE |
Read or write this file instead of stdin/stdout. |
-l |
In pass-through mode, hard-link instead of copying when possible. |
Returns: 0 on success, non-zero on errors; it prints a block count such as 2 blocks to standard error.
Usage notes
- Names come from stdin. Feed it relative paths (
find .) to get relocatable archives; absolute paths are stored as given and may be refused or stripped on extraction. - Extraction does not overwrite newer files. Without
-u,cpio -iskips files that are not older than the archive copy and says so. - Create directories explicitly. Add
-dwhen extracting, or nested files fail with "No such file or directory". - Filter on extraction by giving shell patterns after the options:
cpio -id '*.c' < archive.cpio. Quote them so your shell does not expand them first. - Pass-through (
find . | cpio -pdm /dest) preserves hard links and special files, which makes it a precise tree copier. For ordinary copies,cp -ais simpler. - Initramfs images:
zcat initrd.img | cpio -idmunpacks one into the current directory; repack withfind . | cpio -o -H newc | gzip > new.img. Do this in an empty scratch directory. - Why tar usually wins:
tarhandles directories, compression flags and is ubiquitous. Pickcpiofor list-driven backups, RPM payloads and initramfs work.