cpio command in Linux with Examples
The cpio command, which stands for "copy in, copy out," is a powerful utility in Linux used for processing archive files. It provides functionality to copy files to and from archives, making it an essential tool for system administrators and power users. This article will explore the cpio command in detail, covering its syntax, modes of operation, key options, and practical examples using dummy data.
Syntax
The cpio command has three operational modes; the syntax for each of them is slightly different:
Copy-out Mode
cpio -o > archive
Copy-in Mode
cpio -i
Copy-pass Mode
cpio -p destination-directory
Sample Data Preparation
Create some dummy data that we will use in all our examples:
mkdir cpio_example
cd cpio_example
echo "This is file 1" > file1.txt
echo "This is file 2" > file2.txt
echo "This is file 3" > file3.txt
mkdir subdir
echo "This is a file in a subdirectory" > subdir/file4.txt
This was a simple example with the structure of the directory containing three files in the upper directory and one file in the subdirectory.

Simple Example
Here is how to archive our dummy data with cpio:
find . -name "*.txt" | cpio -ov > archive.cpio
This command finds all the .txt files in the current directory and subdirectories and builds a cpio archive called archive.cpio containing them.

Important Options and Examples
Creating a cpio Archive (Copy-out Mode)
Creating a cpio archive containing some files:
find . -name "*.txt" | cpio -ov > archive.cpio

Extracting from a cpio Archive (Copy-in Mode)
If you want to extract files from a cpio archive:
- Make an empty directory and go into that.
- For example: mkdir extracted; cd extracted;
- To run the cpio -iv command, give the archive name, -id="55"/>archive.cpio
mkdir extracted
cd extracted
sudo cpio -iv < ../archive.cpio

Copying Files (Copy-pass Mode)
Copy multiple files from one directory to another:
mkdir destination
find . -name "*.txt" | cpio -pdm destination

Creating a tar Archive with cpio
The command cpio can also produce tar archives:
find . -name "*.txt" | cpio -ov -H tar > archive.tar

Viewing Contents of an Archive
To see what is inside a cpio or tar archive without extracting it, one does:
cpio -it
Output of cpio command listing archive contents
Advanced Usage
Files to Extract
Extract some files using pattern matching:
cpio -iv "*.txt"
Extracts only the .txt files in this archive
File Attributes
In order to keep ownership and permissions when extracting:
sudo cpio -idvm --no-absolute-filenames
The -m option keeps the modification time on the extracted files, and --no-absolute-filenames ensures the files will be extracted relative to the current directory.
Extraction of a Compressed Archive
You can use cpio with compression tools:
find . -name "*.txt" | cpio -ov | gzip > archive.cpio.gz
This makes a gzip-compressed cpio archive.
Conclusion
The cpio command is one of those generic Linux utilities intended to perform operations related to archives. If you're aware of its modes of operations as well as the amount of options you have at your disposal, that would be pretty handy. This would allow you to deal with file archives, back up your data, and transfer files between systems with the least stress.
Note that man pages (man cpio) or the option --help contain much more information about the command cpio and its usage.