A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/linux-unix/cpio-command-in-linux-with-examples/ below:

cpio command in Linux with Examples

cpio command in Linux with Examples

Last Updated : 01 Nov, 2024

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.

dymmy data 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.

Output of cpio command creating an archive 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
Output of cpio command creating an archive with verbose output Extracting from a cpio Archive (Copy-in Mode)

If you want to extract files from a cpio archive:

mkdir extracted
cd extracted
sudo cpio -iv < ../archive.cpio
Output of cpio command extracting an archive Copying Files (Copy-pass Mode)

Copy multiple files from one directory to another:

mkdir destination
find . -name "*.txt" | cpio -pdm destination
Output of cpio command in copy-pass mode Creating a tar Archive with cpio

The command cpio can also produce tar archives:

find . -name "*.txt" | cpio -ov -H tar > archive.tar
Output of cpio command creating a tar archive 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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4