cpio Command in Linux (original) (raw)

Last Updated : 6 Feb, 2026

The cpio command in Linux is used to create, extract, and copy archive files. The name cpio stands for copy in, copy out. It gets file lists from standard input (often via find and a pipe) rather than direct arguments, and then performs the selected archive or copy operation.

Examples

Before using the cpio command, we need a list of files. This list is usually generated using the find command and passed to cpio through a pipe (|).

Example 1: Create a cpio Archive from Files

This example shows how to create a simple cpio archive containing all .txt files from the current directory and its subdirectories.

**Step 1: Create Sample Files (for understanding)

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.

2024-10-23_14-40

dymmy data

**Step 2: Create the Archive

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.

cio-ov

Output of cpio command creating an archive

This example demonstrates extracting files from a cpio archive.

mkdir extracted
cd extracted
sudo cpio -iv < ../archive.cpio

2024-10-23_14-50

Output of cpio command extracting an archive

Example 3: Copy Files Between Directories (Copy-pass Mode)

This example shows how to copy multiple files from one directory to another using cpio.

mkdir destination
find . -name "*.txt" | cpio -pdm destination

2024-10-23_14-52

Output of cpio command in copy-pass mode

Example 4: Create a tar Archive Using cpio

This example shows how the cpio command can be used to create a tar-format archive.

find . -name "*.txt" | cpio -ov -H tar > archive.tar

2024-10-23_14-54

Output of cpio command creating a tar archive

Example 5: View Contents of an Archive Without Extracting

This example demonstrates how to list the contents of a cpio or tar archive without extracting files. This is useful for quickly inspecting archives.

cpio -it < archive.tar

cio-it-tar

Viewing contents of an archive without extracting

Syntax

The cpio command works in three different modes. Each mode has a slightly different syntax and purpose.

1. Copy-out Mode (Create Archive)

This mode is used to create a cpio archive from a list of files.

cpio -o [options] > archive_file

This mode is used to extract files from a cpio archive.

cpio -i [options] < archive_file

3. Copy-pass Mode (Copy Files Between Directories)

This mode copies files directly from one directory to another without creating an archive.

cpio -p [options] destination_directory

Commonly Used Options in cpio Command

The cpio command provides several options to control how files are archived, extracted, or copied. Below are the most commonly used options.

1. -v: Verbose Mode

The -v option displays the name of each file as it is processed. This helps users see what cpio is doing step by step.

**Example: Create an Archive with Verbose Output

Creating a cpio archive containing some files:

find . -name "*.txt" | cpio -ov > archive.cpio

cio-ov

Output of cpio command creating an archive with verbose output

By default, cpio does not create missing directories during extraction. The -d option ensures required directories are created automatically.

**Example: Extract Archive and Create Directories

cpio -id < ../archive.cpio

cio-id

Extracting archive and create directories

3. -m: Preserve File Modification Time

The -m option keeps the original modification timestamps of files when extracting or copying.

**Example: Extract Files While Preserving Timestamps

cpio -idm < archive.cpio

cio-idm

Extract files while preserving timestamps

4. -p: Pass-through Mode (Copy Files)

This option is used to copy files from one directory to another without creating an archive.

**Example: Copy Files to Another Directory

find . -name "*.txt" | cpio -pvd destination

cio-pvd

Copy files to another directory

5. --no-absolute-filenames: Avoid Absolute Paths

This option prevents files with absolute paths from being extracted outside the current directory.

**Example: Safe Extraction

Files are extracted relative to the current directory, recommended when extracting unknown archives

cpio -idvm --no-absolute-filenames < archive.cpio

cio-idvm

Advanced Usage of cpio Command

The cpio command provides advanced features that help in selective extraction, preserving file attributes, and working with compressed archives. These use cases are especially useful in backups, system recovery, and automation scripts.

cpio -iv "*.txt" < archive.cpio

**Expected Output

./file1.txt
./file2.txt
./file3.txt
128
128
128

2. Preserve File Ownership, Permissions, and Timestamps

This example demonstrates how to restore files exactly as they were archived, including:

**Command

sudo cpio -idvm --no-absolute-filenames < archive.cpio

**Expected Output

./file1.txt
./file2.txt
./file3.txt
./subdir/file4.txt
128
128
128
160

3. Create a Compressed cpio Archive Using gzip

**Command

find . -name "*.txt" | cpio -ov | gzip > archive.cpio.gz

**Expected Output

./file1.txt
./file2.txt
./file3.txt
./subdir/file4.txt