Build and inspect cpio archives without clobbering files
You will create a GNU cpio archive, inspect it before extraction, and unpack it into a controlled directory. The examples use GNU cpio 2.15, installed here as package version 2.15+dfsg-1ubuntu2.1. Allow about fifteen minutes, plus time to decide which files belong in the archive.
The route
Jump straight to the step you need, or tick off Done means at the end.
You need a shell, the cpio package, a readable source directory and enough space for both the archive and its extracted copy. These examples are ordinary user commands. Use sudo only when the files or destination genuinely require elevated access.
1. Check the installed command
Confirm which executable will run and record its version:
$ command -v cpio
/usr/bin/cpio
$ cpio --version
cpio (GNU cpio) 2.15
...
This guide follows the GNU command documented by the local manual page. The operation mode is not inferred from the archive name: choose copy-out with -o, copy-in with -i, or pass-through with -p.
2. Create an archive from a deliberate file list
Change to the directory whose relative paths you want to store. Supplying a list from find keeps the selection visible and avoids accidentally archiving the archive itself:
$ cd /path/to/source
$ find project -type f -print > /tmp/project-files.txt
$ cpio -o -H newc -F /path/to/project.cpio < /tmp/project-files.txt
42 blocks
-o reads names from standard input and writes the archive. -F chooses the archive file, while -H newc selects the SVR4 format supported by GNU cpio. The block count is normal completion output, not a list of files. The archive now contains names relative to /path/to/source, such as project/config.ini.
Checkpoint: make sure the archive exists and is not empty before doing anything with the source:
$ ls -lh /path/to/project.cpio
$ test -s /path/to/project.cpio && echo 'archive is non-empty'
archive is non-empty
3. Make file lists safe for unusual names
The default input delimiter is a newline. That is unsuitable for a filename containing a newline, and plain shell pipelines can also become difficult to audit when names contain spaces. Use null delimiters when building an archive from find:
$ cd /path/to/source
$ find project -type f -print0 | cpio --null --create --format=newc --file=/path/to/project.cpio
42 blocks
The matching options are --null for cpio and -print0 for GNU find. Keep the input list and the working directory aligned. If you run find from the wrong directory, the archive can still be valid while containing paths you did not intend.
Symbolic links are stored as links by default. Adding --dereference follows them and stores the files they point to instead, which can pull data from outside the source tree. Treat that option as an explicit boundary change.
4. Inspect before extracting
Read the archive table of contents without writing files:
$ cpio --list --verbose --file=/path/to/project.cpio
-rw-r--r-- 1 andy andy 1842 Sep 22 20:10 project/config.ini
-rw-r--r-- 1 andy andy 30112 Sep 22 20:10 project/README.md
42 blocks
-t implies copy-in mode, and -v prints each entry. Check names for unexpected absolute paths, parent-directory components such as ../, device files or files that would overwrite valuable data. A listing is an inspection step, not proof that extraction is harmless: ownership, permissions and links can still affect the result.
To inspect a subset, add a pattern after the options:
$ cpio -itv -F /path/to/project.cpio 'project/*.conf'
-rw-r--r-- 1 andy andy 210 Sep 22 20:10 project/app.conf
1 block
Quote shell wildcards so the shell does not expand them against the current directory before cpio sees them.
5. Extract into a fresh destination
Do not test an unfamiliar archive directly in a live configuration directory. Create an empty destination, then extract there:
$ install -d /tmp/cpio-review
$ cpio --extract --make-directories --verbose --file=/path/to/project.cpio -D /tmp/cpio-review
project/config.ini
project/README.md
42 blocks
$ find /tmp/cpio-review -type f -print
/tmp/cpio-review/project/config.ini
/tmp/cpio-review/project/README.md
--make-directories creates leading directories. -D changes to the destination before processing the archive. The local manual says absolute filenames are the default; use --no-absolute-filenames when you want extracted files forced relative to the destination. That is a useful additional guard, but still review the listing first.
Extraction can overwrite an existing file only when you request unconditional replacement with -u, but do not treat the absence of -u as a complete safety mechanism. A destination may already contain files, and metadata or links deserve review.
6. Verify and recover
Compare the extracted files with the source using a read-only check:
$ diff -ruN /path/to/source/project /tmp/cpio-review/project
$ cmp /path/to/source/project/README.md /tmp/cpio-review/project/README.md && echo identical
identical
A clean diff produces no output. Preserve the source and original archive until this check passes. If extraction created an unwanted review tree, remove only that known temporary directory:
$ rm -rf /tmp/cpio-review
That removal is irreversible for files in the directory. If you extracted into a real destination by mistake, stop writing to it, record what changed, and restore from the destination's backup or version-control history. cpio has no transaction or undo command.
On failure, GNU cpio exits with status 2. Capture it immediately if a script needs to distinguish success:
$ cpio -it -F /path/to/project.cpio >/tmp/cpio-list.txt
$ status=$?
$ printf 'cpio exit status: %s\n' "$status"
cpio exit status: 0
Do not confuse the block count with a verification result. Check the exit status and inspect the files you care about.
Done means
- The installed program is GNU cpio 2.15 or a version whose manual you have checked.
- The archive was created from a reviewed file list, with null delimiters for robust automated input.
- The table of contents was inspected before extraction.
- Extraction happened in a controlled destination, not over a live system tree.
- The extracted files were checked and the original source and archive were kept until verification finished.