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
Copy all files and directories in a directory and add them onto an archive (copy-[o]ut), in verbose mode
Pick all files from an archive (copy-[i]n), generating directories where needed, in verbose mode
SYNOPSIS
cpio -o [options] < name-list > archive
cpio -i [options] [pattern...] < archive
cpio -p [options] directory < name-list
PARAMETERS
-o
Create archive mode. Reads filenames from stdin; writes archive to stdout (or file specified by -F).
-i
Extract archive mode. Reads archive from stdin (or file via -F); extracts files. Optional pattern arguments filter extracted files.
-p
Pass-through mode. Reads filenames from stdin; copies them to the specified directory without intermediate archive creation.
-v, --verbose
Verbose mode. Lists processed files, showing detailed information in create/extract modes or names in pass-through mode.
-d, --make-directories
Create leading directories as needed during extraction or pass-through.
-u, --unconditional
Unconditionally copy/extract files, replacing newer files with older ones.
-m, --preserve-modification-time
Preserve original modification times of files during extraction/copying.
-a, --reset-access-time
Reset access times of input files after reading.
-L, --dereference
Follow symbolic links; archive/copy the files they point to, not the links themselves.
-H format, --format=format
Specify the archive format, e.g., newc, crc, bin, odc, ascii.
-F archive, --file=archive
Use the specified archive file instead of standard input/output.
-R owner[:group], --owner=owner[:group]
Set ownership of created files. Requires root privileges.
--block-size=BLOCK_SIZE
Set I/O block size (BLOCK_SIZE * 512 bytes). Default is 10 blocks (5120 bytes).
--null
Filenames are null-terminated in -o or -p mode. Essential when piping from find -print0.
--no-absolute-filenames
Convert absolute filenames to relative ones during extraction to prevent accidental system file overwrites.
DESCRIPTION
cpio (copy in/out) is a command-line utility for archiving files and directories. It copies files into or out of a cpio archive, or copies files from one directory tree to another. Unlike tar, cpio typically reads a list of filenames from standard input (often piped from find), providing flexible file selection. It supports various archive formats, notably newc, which is widely used for Linux initramfs images. It operates in three main modes: creating archives (-o), extracting from archives (-i), and pass-through copying (-p).
CAVEATS
When extracting archives, be cautious with absolute filenames (e.g., paths starting with /) if the archive was created with them. Using --no-absolute-filenames is recommended to prevent accidental overwrites of system files. cpio typically expects a list of null-terminated filenames when piped from commands like find -print0, especially for filenames containing spaces or special characters.
COMMON USAGE WITH <I>FIND</I>
A very common pattern for creating a cpio archive is to pipe the output of the find command into cpio. To handle filenames with spaces or special characters correctly, it is crucial to use find -print0 and cpio --null.
Example:
find . -print0 | cpio -o --null -H newc > myarchive.cpio
This command finds all files and directories starting from the current directory, prints their names separated by null characters, and pipes this list to cpio, which then creates a newc format archive named myarchive.cpio.
ARCHIVE FORMATS
cpio supports various archive formats. The most commonly used in modern Linux environments are:
- newc: The "new" portable format, widely used for Linux initramfs images.
- crc: Similar to newc but includes a checksum for data integrity.
- bin: The obsolete binary format.
- odc: The old ASCII format, also known as 'old character'.
- ascii: A more portable ASCII format.
HISTORY
cpio is one of the oldest archiving utilities in Unix-like systems, tracing its origins back to the PWB/UNIX (Programmer's Workbench) systems of the 1970s. It was designed to copy file archives between different formats. While tar later became more dominant for general-purpose archiving, cpio maintained its niche, particularly due to its ability to read filenames from standard input, making it highly flexible when combined with other tools like find. Its 'newc' format is notably used for Linux initramfs images.