“The “cpio” command, options, and usage in Linux are explained in this article with examples. The cpio archiving tool is still used, for example, to produce initramfs images on Linux and for rpm packages, even though it is used less frequently than other archiving programs like tar these days. This tutorial demonstrates using the GNU cpio function to build and extract cpio archives as well as how to get a list of the files they contain.”
What is the Cpio Command?
Copy in and copy out are referred to as cpio. In Linux/UNIX, it is a command-line tool to generate, list down, and extract archive files. A cpio archive file is produced by it. The entire file system can be copied to an archive file.
From the standard input, cpio reads the file list and sends the result to the standard output. It creates an archive while executing this process.
In order to choose the files to copy, the cpio command is typically used in conjunction with “ls” or “find.” The “ls” or “find” command output is piped to the cpio command to do this.
Modes That Work With cpio Command
The cpio command supports the following modes.
Mode | Description |
---|---|
Copy-out mode | With the -o or –create option, cpio copies files into an archive in this mode. |
Copy-in mode | It either prints the contents of an archive or copies files out of the archive. Using the -i option, cpio reads the archives from the standard input. The –extract option can also be used for doing this. |
Copy-pass mode | This method does not create any archive files; it just moves files from one directory tree to another. |
Some important options available in the cpio command are given below.
Option | Description |
---|---|
-o, –create | This option generates a .cpio archive file. |
-i, –extract | This option extracts files to see the contents inside. |
-p, –pass-through | From one directory tree to another, files are copied through this option. |
-v, –verbose | It displays verbose information. |
-t | It lists input files. |
How to Create a *.cpio File?
We will explain how to generate *.cpio files that contain files and directories in this section. The cpio command will be used to help us accomplish this. The syntax of the cpio command is attached below.
You can see that we used the “ls” command to list the files first in the image provided below. Files with the name “file”, “folder1”, and “myfile” are listed in the second line, as you can see.
In the command, you can see -ov and -v options. The -ov option will create the new archive, and the -v option will go down the files processed.
When we execute the above command, the .cpio file is created. You can see the created file with the cpio command in the given screenshot.
How to Extract a *.cpio File?
We can also extract *.cpio files with the cpio command. No matter if these files contain more files and directories. This example will discuss this concept.
Firstly, we have listed down all the files with the ls command. The -i or –extract option is used here. This option will extract a .cpio file. The command in the third line of the code extracts the file.cpio file in the current working directory.
Cpio won’t extract and display the error message if the location already has the latest or identical version.
How to Create a *.tar Archive File Using cpio?
Here we will show you how you can create a .tar archive file with the cpio command. But first, let’s see what a tar file is.
The TAR file format, which stands for Tape Archive, is used to store many files in a single file. It is a commonly used method of archiving as well as transferring many files over the internet, for example, files for software downloads. A TAR file must be opened using an application or command that can open archives.
Let’s get back to the main topic, which is to create a .tar archive file.
In the command attached below, you can see “-H.” The -H option used in the command will specify the format for the archive file. A tar file named “file1.tar” is created.
Here you can see that the above command created the tar file with the name file1.tar.
How to Extract the *.tar Archive File?
The following example extracts a file1.tar file to another folder named “newfolder.” This folder is present in the home directory of the current user. Here you can see that the command extracted the contents of the file. The command for extracting the tar file and its output is attached below.
How to Only View the *.tar Archive File by Using the cpio Command?
The cpio helps to view *.tar files containing files and directories. Here we have used the “cpio -it -F /home/linux/file1.tar” command to only view the .tar archive files.
Upon execution of this command, you can see the listing of all the files as well directories that are present in that location.
How to Show the Tar File Contents?
By using the given command in the following screenshot, you can print a .tar’s contents. Use this short command to quickly list every item in a .tar file. You can see that we wrote the command “cpio -itv -F file1.tar” here. The file from which we are displaying the content is the “file1.tar” file.
The attached screenshot contains the command and its output. Here you can see all the file contents with additional details such as the directory, group they belong to, and creation date and time.
Conclusion
This post taught us how to utilize the Linux cpio command. Although it is used less frequently than tar these days, it is nevertheless necessary to understand how it operates because it is used to make rpm software packages, among other things. We learned how to construct an archive, extract data from it, and then list the information inside. To better comprehend the complete concept, make sure you understand the commands used in the screenshots that are attached.