Linux Professional Institute LPIC-1 Exam 101
GNU and Unix Commands
Perform Basic File Management Part 2 cpio
In this lesson, we’ll dive into the cpio utility, a classic UNIX tool for creating and extracting archives. Whether you’re bundling application files or unpacking backup archives, cpio offers a straightforward, scriptable interface.
What Is cpio?
CPIO stands for Copy In, Copy Out. Unlike tar, which handles device archives too, cpio reads and writes archives strictly via standard input and standard output. You typically pipe a file list into cpio to create an archive, or feed an archive into cpio to extract its contents.
Common Use Cases
| Task | cpio Option | Description |
|---|---|---|
| Create an archive | -o | Copy out files into a new archive |
| Extract files from an archive | -i | Copy in files from an existing archive |
| Create directories as needed | -d | Automatically make parent directories on extract |
How cpio Works
- Generate a file list (e.g., via
findorls). - Pipe that list to
cpio. - Specify mode:
-ofor output (create archive)-ifor input (extract archive)
Note
cpio does not infer the archive format from file extensions. You control its behavior exclusively with command-line options.
1. Creating an Archive
To package all files in the current directory into archive.cpio:
ls | cpio -o > archive.cpio
Explanation:
lslists filenames (one per line).- The pipe (
|) directs that list intocpio. cpio -owrites an archive to stdout.> archive.cpioredirects stdout into your archive file.
You can also use find to include subdirectories:
find . -depth | cpio -o > project.cpio
2. Extracting an Archive
Unpack the contents of archive.cpio, creating directories as needed:
cpio -id < archive.cpio
Breakdown:
cpio -ireads an archive from stdin.-densures that all required directories are created.< archive.cpiofeeds the archive intocpio.
Warning
By default, cpio preserves file ownership and permissions. Run as root only if you trust the archive contents to avoid overwriting critical system files.
Quick Reference: cpio Options
| Option | Description |
|---|---|
-o | Create an archive (Copy Out) |
-i | Extract from an archive (Copy In) |
-d | Create directories as needed when extracting |
-v | Verbose mode (list files processed) |
-H fmt | Select archive format (e.g., newc, odc) |
Links and References
Watch Video
Watch video content