Here’s a concise guide on archiving files with cpio in Linux, presented in notes format with a command summary.
Archiving Files with cpio in Linux
cpiostands for Copy In and Out.- It is used to archive files, copy directories, or extract archives.
- Reads file names from standard input, not from command-line arguments like
tar.
Common Use Cases
- Create an archive
- Extract files from an archive
- List archive contents
File Selection: find + cpio
cpio doesn’t take file names directly — it expects input via pipe or redirect. Usually used with find.
Modes of Operation
| Mode | Flag | Description |
|---|---|---|
| Copy-out | -o | Create archive (write files) |
| Copy-in | -i | Extract or list from archive |
| Copy-pass | -p | Copy files from one dir to another |
Creating an Archive (Copy-out mode)
find . -type f | cpio -ov > archive.cpio-o: copy-out (create archive)-v: verbose>: write archive to file
Extracting an Archive (Copy-in mode)
cpio -idv < archive.cpio-i: copy-in (extract)-d: create directories if needed-v: verbose<: read archive from file
Listing Archive Contents
cpio -it < archive.cpio-t: list contents (table of contents)
Copy Files from One Directory to Another (Copy-pass mode)
find . -type f | cpio -pvd /destination/path-p: copy-pass-v: verbose-d: create directories
Compression with gzip
find . -type f | cpio -ov | gzip > archive.cpio.gzTo extract:
gunzip < archive.cpio.gz | cpio -idvSummary of Common cpio Flags
| Flag | Description |
|---|---|
-o | Copy-out mode (create archive) |
-i | Copy-in mode (extract/archive view) |
-t | List archive contents |
-d | Create directories as needed |
-v | Verbose output |
-p | Copy-pass mode |
-u | Replace newer files during extract |
-m | Preserve modification times |
Last updated on