Using the cpio Program to Create, Extract, and Copy Archives in Linux
Learn cpio copy-out, copy-in, and copy-pass modes, including safe find pipelines, extraction, gzip compression, metadata preservation, and troubleshooting.
cpio is a Unix and Linux file archiving utility. Its name refers to copy in and copy out. It can create an archive, extract an archive, or copy selected files directly into another directory.
Unlike a command such as cp, cpio commonly receives pathnames through standard input instead of receiving every file as an ordinary command-line argument. A typical command uses find to select files and a shell pipeline to send those pathnames to cpio.
An archive is a single data stream or file containing multiple files and directory entries. Archiving and compression are separate operations: cpio packages data but does not necessarily compress it. You can compress the resulting archive with a separate utility such as gzip.
This lesson assumes familiarity with cd, pwd, directories, pipelines, and shell redirection. See Bash shell fundamentals, finding the full path of commands, and other Linux topics as needed.
The Three cpio Operating Modes
cpio has three primary operating modes. The mode determines whether cpio creates an archive, reads one, or bypasses an archive entirely.
| Mode | Primary option | Input | Output | Typical use |
|---|---|---|---|---|
| Copy-out | -o or --create | Pathnames from standard input | Archive data on standard output | Create a cpio archive |
| Copy-in | -i or --extract | Archive data from standard input | Restored files | Extract an archive |
| Copy-pass | -p or --pass-through | Pathnames from standard input | Files in a destination directory | Copy a selected directory tree without an archive |
Generating Pathname Input Safely
find locates files and produces the pathnames that cpio consumes. In this relationship, find selects the input and cpio performs the archive or copy operation:
find . -print | cpio -ov > archive.cpioThe | is a pipeline: it sends find's standard output to cpio's standard input. The -print action emits one pathname per line and is adequate for simple names.
Newlines are valid characters in Linux filenames. Names containing spaces or quotes usually survive a basic newline-delimited pipeline, but names containing newlines do not. For robust pathname handling, use NUL characters as separators:
find /home/bob/example_dir -print0 | cpio --null -ov --format=newc > example_dir.cpiofind -print0 emits a NUL after each pathname, and cpio's --null option tells it to expect that format. Use these matching options when filenames may contain whitespace, quotes, or newlines. Support for long options can vary between implementations, so check cpio --help or the local manual when necessary.
Avoid using ls as a general pathname generator. Its display is intended for people, not for preserving exact filename boundaries in a pipeline. It can split or transform complex names. find with -print or -print0 is the appropriate approach.
Copy-Out Mode: Creating Archives
In copy-out mode, cpio reads pathnames from standard input and writes archive data to standard output. The shell can redirect that archive stream to a file with >.
Archive the Current Directory
find . -print | cpio -ov > archive.cpiofind . -printemits the current directory and entries beneath it.-oselects copy-out mode.-venables verbose output, showing processed names.> archive.cpiosaves cpio's archive stream in the file namedarchive.cpio.
Be aware that verbose output and archive data both use standard output in many cpio implementations. When redirecting output, consult the local implementation if verbose text appears to interfere with the archive. GNU cpio normally writes verbose names to standard error, allowing the archive stream to remain redirectable.
Archive a Named Directory
find /home/bob/example_dir -print | cpio -ov > example_dir.cpioThe archive records the paths emitted by find. Because the output filename is relative, example_dir.cpio is created in the current working directory, not automatically inside /home/bob/example_dir. Use an absolute output path when you want the archive elsewhere.
For a commonly portable format, explicitly select newc:
find /home/bob/example_dir -print0 | cpio --null -ov --format=newc > example_dir.cpioThe newc format is widely used for portable cpio archives, particularly where compatibility between Unix and Linux systems matters.
Copy-In Mode: Extracting Archives
Copy-in mode reads archive data from standard input and restores its members as files. Input redirection with < supplies a saved archive:
cpio -iv < example_dir.cpio-iselects extraction mode.-vprints the names being extracted.< example_dir.cpiomakes the archive file cpio's standard input.
Extraction occurs relative to the current working directory unless you select another directory by changing to it first. For example:
mkdir -p /tmp/example-inspection
cd /tmp/example-inspection
cpio -idv < /path/to/example_dir.cpioThe -d option creates directories as needed. It is commonly required when an archive contains nested paths whose parent directories are not already present.
Overwrite and Path Safety
Extraction can conflict with existing files. Depending on the cpio implementation and options, archive members may replace files with matching paths or fail when conflicts occur. Inspect the archive and extract into a dedicated, empty directory when possible:
mkdir -p /tmp/cpio-check
cd /tmp/cpio-check
cpio -itv < /path/to/example_dir.cpio
cpio -idv < /path/to/example_dir.cpioThe -t option lists archive contents without extracting them. Review the names for unexpected absolute paths, parent-directory components such as ../, or files that would overwrite important data. Never extract an untrusted archive directly into a sensitive directory without inspecting it first.
Copy-Pass Mode: Copying Directory Trees
Copy-pass mode reads a pathname list from standard input and takes a destination directory as its command-line argument. It copies files directly; no archive file is created.
find /home/bob/example_dir -print | cpio -pvd /home/bob/new_directory-pselects pass-through mode.-vdisplays progress.-dcreates destination directories as needed./home/bob/new_directoryis the destination.
The source files remain in place. cpio's pass-through operation is useful when you want to transfer a selected tree while retaining metadata such as file modes, modification times, and directory structure. Ownership can also be retained when the process has permission to assign the original owners. Ordinary users generally cannot set ownership to arbitrary users.
Use the NUL-safe form when source names may be unusual:
find /home/bob/example_dir -print0 | cpio --null -pvd /home/bob/new_directoryCommon cpio Options
| Option | Long form | Purpose | Applicable mode(s) |
|---|---|---|---|
-o | --create | Create an archive from pathname input | Copy-out |
-i | --extract | Read and restore archive members | Copy-in |
-p | --pass-through | Copy pathname input to a destination directory | Copy-pass |
-v | varies | Print processed names | All modes |
-d | varies | Create missing directories | Copy-in and copy-pass |
--null | --null | Read NUL-separated pathnames | Copy-out and copy-pass |
--format=newc | --format=newc | Select the newc archive format | Copy-out |
Archive Compression with gzip
cpio creates the archive layer, while gzip creates the compression layer. A pipeline can send cpio's archive stream directly to gzip:
find /home/bob/example_dir -print0 | cpio --null -o --format=newc | gzip > example_dir.cpio.gzData flows from find to cpio, from cpio to gzip, and from gzip to the file. The .cpio.gz suffix indicates a gzip-compressed cpio archive.
To extract it, decompress to standard output and pipe that data into cpio:
gzip -dc example_dir.cpio.gz | cpio -idvHere, gzip -d decompresses and -c writes the result to standard output. cpio then reads the decompressed archive from its standard input. gzip does not understand cpio members; it only compresses or decompresses the complete archive stream.
| Task | Command component | Data flow |
|---|---|---|
| Create archive | cpio -o | Pathnames into cpio; archive stream out |
| Compress archive | gzip | Archive stream into gzip; compressed stream out |
| Decompress archive | gzip -dc | Compressed file into gzip; cpio archive stream out |
| Extract archive | cpio -i | Archive stream into cpio; files restored |
Safe Usage and Verification
- List archive contents with
cpio -itvbefore extraction when possible. - Use a dedicated empty directory for inspection and extraction.
- Treat archives from untrusted sources as potentially dangerous. Unexpected paths can expose files outside the intended directory, and matching names can overwrite existing files.
- Run with elevated permissions only when necessary, such as restoring protected paths or ownership. Extra privileges increase the consequences of an unsafe archive.
- Use
-print0with--nullfor robust handling of unusual names.
Troubleshooting
| Symptom | Likely cause | Resolution |
|---|---|---|
| Files appear in an unexpected location or extraction fails because directories are missing. | Extraction is relative to the current directory, and parent directories may not exist. | cd to the intended destination first and use -d. |
| Names with spaces, quotes, or newlines are split or omitted. | Pathnames were processed as newline-delimited text. | Use find -print0 with cpio --null. |
| The archive is larger than expected. | cpio archives but does not compress by default. | Pipe the archive through gzip or another compression utility. |
| Copy-pass reports that the destination does not exist. | The target or required nested directories are absent. | Create the destination or include -d. |
| Ownership is not retained. | The user lacks permission to assign the original ownership. | Use appropriate administrative permissions only when ownership preservation is required. |
| Existing files are replaced or conflict with archive members. | The destination already contains matching paths. | Inspect first and extract into a separate directory; consult the local cpio manual for overwrite-related options. |
| The archive format is not recognized. | The file may be compressed or use a different cpio format. | Decompress it first if necessary and use a compatible cpio implementation or explicitly select the required format. |