Overview
The cpio command is a tool used to create archive files or extract files from them. It works by receiving a list of file paths from standard input.
While it is similar to the tar command, cpio is a filter-type command that requires you to pipe a file list into it. It is still commonly used for tasks such as extracting RPM packages, managing initramfs (initial RAM disk) images, and efficiently copying directory trees (pass-through).
Specifications (Arguments and Options)
Syntax
The cpio command operates in one of three main modes:
cpio -o [options] > [archive_file]cpio -i [options] < [archive_file]cpio -p [options] [destination_directory] < [file_list]
Operating Modes
| Mode | Description |
| -o | Copy-Out mode. Takes a file list as input and creates (outputs) an archive. |
| -i | Copy-In mode. Takes an archive as input and extracts files from it. |
| -p | Pass-Through mode. Copies files directly to another directory without creating an archive file. |
Main Options
| Option | Description |
| -d | Automatically creates directories during extraction if they do not exist. |
| -v | Displays the names of processed files (Verbose). |
| -t | Lists the contents of the archive without extracting them. |
| -u | Overwrites existing files unconditionally. |
| -m | Keeps the original file modification times instead of the extraction time. |
| –no-preserve-owner | Sets the owner of extracted files to the current user instead of the original owner. |
| –null / -0 | Treats NULL characters as separators for the input list (use with find -print0). |
| -F [file] | Uses the specified archive file instead of standard input/output. |
Basic Usage
To use cpio, you first create a file list and then pipe it into the command. Below is an example of creating an archive and then checking its contents.
# List all files and create an archive named backup.cpio
find . -depth | cpio -ov > backup.cpio
# Check the contents of the archive without extracting it
cpio -it < backup.cpio
Example Output:
.
./config.json
./assets
./assets/logo.png
2 blocks
Practical Commands
Extracting an Archive (Maintaining Directory Structure)
When extracting an archive, use the -d option. This ensures that subdirectories are created automatically if they do not exist.
# Extract backup.cpio to the current directory
cpio -ivd < backup.cpio
Extracting RPM Packages (Source)
RPM files used in Red Hat-based systems are actually cpio archives. You can extract their contents without installing them by using rpm2cpio.
# Extract the contents of a src.rpm to the current directory
rpm2cpio nginx-1.20.1-1.el8.src.rpm | cpio -idv
Copying a Directory Tree (Pass-Through)
You can copy a directory structure directly to another location without creating an archive file. This is often more flexible than the cp command.
# Copy everything from src_dir to dest_dir
# Using find -print0 handles filenames with spaces correctly
cd src_dir
find . -type f -print0 | cpio --null -pvd ../dest_dir
Custom Tips
Handling Filenames with Spaces (–null)
If your file names contain spaces or newlines, the standard list format might fail. Always use find -print0 to create a NULL-separated list and use the cpio --null option to process it.
find . -print0 | cpio --null -ov > archive.cpio
Changing Ownership (–no-preserve-owner)
If you extract a backup created by another user, you might encounter permission errors. This option changes the owner of the extracted files to the user running the command.
cpio -ivd --no-preserve-owner < system_backup.cpio
Important Notes
- Remember the -d Option: If you forget
-dduring extraction, the command will fail if the required subdirectories do not exist. - Input List is Required: Unlike
tar, you cannot simply pointcpioat a directory. You must always provide a file list via a pipe or redirection. - Overwrite Behavior: By default,
cpiowill not overwrite a newer file with an older one from the archive. Use-uif you want to force an overwrite.
Advanced Usage
Checking and Editing initramfs Images
The initramfs file used during the Linux boot process is a compressed cpio archive. You can use cpio to view or modify its contents.
# Extract initramfs contents for inspection
mkdir initramfs_work
cd initramfs_work
zcat /boot/initramfs-linux.img | cpio -idv
Conclusion
The cpio command is most effective when used alongside find or rpm2cpio.
While tar is the standard for general backups, cpio is essential for specialized tasks like inspecting RPM contents, copying complex directory structures, or editing boot images. The pass-through mode (-p) is particularly useful for fast and reliable directory replication.
