How To Use Cpio Command In Linux.
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.
With the -o or –create option, cpio copies files into an archive in this 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.
This method does not create any archive files; it just moves files from one directory tree to another.
This option generates a .cpio archive file.
This option extracts files to see the contents inside.
From one directory tree to another, files are copied through this option.
It displays verbose information.
It lists input files.
The basic syntax of the cpio command is as follows:
Common options used with cpio include:
-i or –extract: Extract files from an archive.
-o or –create: Create an archive.
-t or –list: List the contents of an archive.
Using cpio to Create an Archive
To create an archive with cpio, you can use the -o option. For example, to create an archive named “my_archive.cpio” from a list of files and directories, use the following command:
In this example, we use the find command to generate a list of files and directories in the specified path and pipe the list to cpio. The -o option instructs cpio to create an archive, and the output is redirected to the file “my_archive.cpio.”
Using cpio to Extract Files
To extract files from an archive, use the -i option. For instance, to extract the contents of “my_archive.cpio” to a specific directory, use the following command:
In this command, the -i option tells cpio to extract files, and -d is used to create directories as needed. The -v option is for verbose output, so you can see the list of files as they are extracted.
Listing Archive Contents
To list the contents of an archive, use the -t option. For example:
This command will display a list of files and directories contained in the “my_archive.cpio” archive.
Combining cpio with Other Commands
Cpio can be combined with other commands to perform more advanced tasks. For instance, to create an archive of files modified within the last 7 days, you can use the find command in conjunction with cpio like this:
This command will create an archive of files modified in the last 7 days.
Working with Different Archive Formats: Cpio can work with various archive formats, such as the traditional ‘odc’ (old character) format, ‘newc’ (new character) format, and the ‘crc’ (CRC-16) format. You can specify the format using the -H option. For example, to create an archive in ‘newc’ format, use the following command:
Preserving File Permissions: By default, cpio preserves file permissions when creating or extracting an archive. This is essential for maintaining the integrity of the files. You can use the -p option to specify the mode and ownership. For example:
This command will copy files from the source to the destination path while preserving permissions and ownership.
Filtering Files: You can use the find command in conjunction with cpio to filter files before creating an archive. For example, to create an archive containing only text files, use the following command:
Extracting Specific Files: When extracting files from an archive, you can specify which files to extract by providing a pattern. For instance, to extract only files ending with “.jpg” from an archive, you can use the -pattern option like this:
Compression with cpio: You can combine cpio with compression tools to create compressed archives. For instance, to create a gzipped archive, you can use the following command:
Remote Archiving: Cpio can also be used for remote archiving. To create an archive on a remote server and transfer it to the local machine, you can use SSH and cpio together. For example:
Renaming Files During Extraction: Cpio allows you to rename files during extraction using the –rename option. For example, to extract files and rename them with a “new_” prefix, you can use the following command:
These advanced features of the cpio command demonstrate its versatility and usefulness in various scenarios. Whether you need to create archives, filter files, or work with different archive formats, cpio is a powerful tool that can help you efficiently manage your data on a Linux system.
The cpio command is a powerful tool for creating, extracting, and manipulating archive files in Linux. Its flexibility and compatibility with various archive formats make it a valuable addition to the Linux command-line toolkit. By mastering the cpio command and its options, you can efficiently manage your files and directories, whether for backups or data distribution.