LinuxCommandLibrary

cpio

Archive and extract files

TLDR

Take a list of file names from stdin and add them onto an archive (copy-[o]ut) in cpio's binary forma

$ echo "[path/to/file1 path/to/file2 ...]" | cpio [[-o|--create]] > [archive.cpio]
copy

Copy all files and directories in a directory and add them onto an archive (copy-[o]ut), in verbose mode
$ find [path/to/directory] | cpio [[-ov|--create --verbose]] > [archive.cpio]
copy

Pick all files from an archive (copy-[i]n), generating directories where needed, in verbose mode
$ cpio < [archive.cpio] [[-idv|--extract --make-directories --verbose]]
copy

SYNOPSIS

cpio -o [options] < name-list [> archive]
cpio -i [options] [< archive] [patterns]
cpio -p [options] dest-directory < name-list

PARAMETERS

-o
    Copy-out mode: create archive from stdin file list

-i
    Copy-in mode: extract from archive on stdin

-p
    Copy-pass mode: copy files to dest dir from stdin list

-a
    Reset access times on files (copy-out/in)

-A
    Append to existing archive

-b
    Swap halfwords and bytes when reading/writing

-B
    Use 5120-byte records instead of 512

-c
    Use old portable (ASCII) format headers

-C bytes
    Set I/O block size

-d
    Create leading directories as needed

-E file
    Read file list from file (instead of stdin)

-f
    Rename extracted files to avoid clobbering

-F file
    Use file as archive (instead of stdin/stdout)

-H format
    Use specified format: bin, odc, newc, crc, tar, ustar, hpbin, hpodc

-I file
    Read archive from file (copy-in)

-L
    Follow symlinks (dereference)

-l
    Use hard links when possible (copy-pass)

-m
    Retain mtime when extracting/copying

-M message
    Print message on EOF of remote medium

-n
    Numeric UID/GID in listings

-O file
    Write archive to file (copy-out/copy-pass)

-r
    Interactive renaming (copy-in/pass)

-R [user][:group]
    Set owner of extracted files

-s
    Swap bytes

-S
    Swap halfwords

-t
    Print table of contents (no extract)

-u
    Unconditional extract/copy (overwrite newer files)

-v
    Verbose listing

-V
    Show version info

--null
    Filenames terminated by nulls (use with find -print0)

--sparse
    Handle sparse files efficiently

DESCRIPTION

cpio is a standard Unix utility for creating and extracting archives in the cpio format, an older predecessor to tar used in early Unix systems.

It operates in three primary modes: copy-out (-o) reads a list of filenames from standard input (often piped from find) and writes a cpio archive to standard output; copy-in (-i) extracts files from an archive on standard input to the current directory; and copy-pass (-p) copies files from a list to a specified destination directory without creating an intermediate archive.

cpio supports various archive formats including binary, ASCII, new ASCII, CRC, HP-UX binary, and tar (via options). It's commonly used for initramfs images in Linux, backups on tape, and network transfers. Unlike tar, cpio requires an external list of files and does not support globbing directly, making it powerful with find but less intuitive for casual use.

GNU cpio adds enhancements like sparse file support, remote operations via rsh, and extended format options.

CAVEATS

Filenames cannot contain newlines unless --null is used; absolute paths are preserved unless --no-absolute-filenames; requires stdin file list (pair with find); less intuitive than tar for simple tasks.

COMMON EXAMPLES

Create: find . -print | cpio -o -H newc > archive.cpio
Extract: cpio -idmv < archive.cpio
Copy dir tree: find src -print | cpio -pvd dest
List: cpio -itv < archive.cpio

FORMATS

Supported: newc (default, portable), odc (old ASCII), bin (binary), crc (with checksum), tar, ustar.

HISTORY

Introduced in 1979 with Unix 7th Edition as part of Programmer's Workbench (PWB). Evolved in AT&T System V Unix (1983), became POSIX.1-1988 standard. GNU cpio (1989) by David MacKenzie adds modern features; widely used in Linux for initramfs and backups.

SEE ALSO

tar(1), pax(1), find(1), mt(1), dd(1)

Copied to clipboard