Linux cpio Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux cpio command, a powerful tool for creating and extracting archives, as well as performing selective archiving and extraction. The lab covers the purpose and syntax of the cpio command, and provides practical examples of creating and extracting archives using the command. Additionally, you will learn how to perform selective archiving and extraction with cpio. The lab is designed to help you develop skills in compression and archiving, which are essential for managing and organizing files and directories in a Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") subgraph Lab Skills linux/cat -.-> lab-422616{{"`Linux cpio Command with Practical Examples`"}} linux/cd -.-> lab-422616{{"`Linux cpio Command with Practical Examples`"}} linux/find -.-> lab-422616{{"`Linux cpio Command with Practical Examples`"}} linux/ls -.-> lab-422616{{"`Linux cpio Command with Practical Examples`"}} linux/cp -.-> lab-422616{{"`Linux cpio Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the cpio Command

In this step, you will learn about the purpose and syntax of the cpio command in Linux. The cpio command is a powerful tool used for creating and extracting archives, as well as performing selective archiving and extraction.

The cpio command can be used in three different modes:

  1. Copy-out mode: Used to create an archive from a list of files.
  2. Copy-in mode: Used to extract files from an archive.
  3. Copy-pass mode: Used to copy files from one directory to another.

To understand the basic syntax of the cpio command, let's look at the following examples:

Create an archive using cpio:

cd ~/project
ls -l | cpio -o > archive.cpio

Example output:

1234 blocks

Extract an archive using cpio:

cd ~/project
cpio -i < archive.cpio

Example output:

1234 blocks

The -o option is used to create an archive, and the -i option is used to extract an archive. The < and > symbols are used to redirect the input and output, respectively.

You can also use the --help option to view the complete list of available options for the cpio command.

Create and Extract Archives Using the cpio Command

In this step, you will learn how to create and extract archives using the cpio command.

Create an Archive
To create an archive using cpio, we'll first create some sample files and directories in the ~/project directory:

cd ~/project
mkdir sample_dir
touch sample_dir/file1.txt sample_dir/file2.txt

Now, let's create an archive of the sample_dir directory:

cd ~/project
ls -1 sample_dir | cpio -o > archive.cpio

Example output:

3 blocks

The ls -1 sample_dir command lists the files in the sample_dir directory, and the cpio -o command creates the archive file archive.cpio.

Extract an Archive
To extract the files from the archive.cpio archive, use the following command:

cd ~/project
cpio -i < archive.cpio

Example output:

3 blocks

The cpio -i command extracts the files from the archive.cpio archive.

You can verify the extracted files by listing the contents of the sample_dir directory:

cd ~/project
ls -l sample_dir

Example output:

total 0
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file1.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file2.txt

Perform Selective Archiving and Extraction with cpio

In this step, you will learn how to perform selective archiving and extraction using the cpio command.

Selective Archiving
Suppose you have the following files and directories in the ~/project directory:

cd ~/project
mkdir sample_dir
touch sample_dir/file1.txt sample_dir/file2.txt sample_dir/file3.txt
touch file4.txt file5.txt

To create an archive that includes only the files in the sample_dir directory, you can use the following command:

cd ~/project
find sample_dir -type f | cpio -o > sample_archive.cpio

Example output:

3 blocks

The find sample_dir -type f command finds all regular files (not directories) in the sample_dir directory, and the cpio -o command creates the sample_archive.cpio archive.

Selective Extraction
To extract only the files from the sample_archive.cpio archive that have the .txt extension, you can use the following command:

cd ~/project
cpio -i --include="*.txt" < sample_archive.cpio

Example output:

3 blocks

The --include="*.txt" option tells cpio to extract only the files with a .txt extension.

You can verify the extracted files by listing the contents of the sample_dir directory:

cd ~/project
ls -l sample_dir

Example output:

total 0
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file1.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file2.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file3.txt

Summary

In this lab, you first learned about the purpose and syntax of the cpio command in Linux. The cpio command is a powerful tool used for creating and extracting archives, as well as performing selective archiving and extraction. You explored the three different modes of the cpio command: copy-out mode for creating archives, copy-in mode for extracting archives, and copy-pass mode for copying files from one directory to another. You also learned the basic syntax for creating and extracting archives using cpio.

Next, you practiced creating and extracting archives using the cpio command. You created a sample directory and files, then used the cpio -o command to create an archive. To extract the files from the archive, you used the cpio -i command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like