How to use cpio command in Linux.

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.

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.

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.

 

Basic Syntax

The basic syntax of the cpio command is as follows:

cpio [options] < archive

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:

find /path/to/files -depth | cpio -o > my_archive.cpio

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:

cpio -i < my_archive.cpio -d -v

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:

cpio -t < my_archive.cpio

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:

find /path/to/files -type f -mtime -7 | cpio -o > recent_files.cpio

This command will create an archive of files modified in the last 7 days.

  1. 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:

    find /path/to/files -depth | cpio -o -H newc > newc_format.cpio
  2. 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:

    find /path/to/files -depth | cpio -o -pumv /destination/path

    This command will copy files from the source to the destination path while preserving permissions and ownership.

  3. 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:

    find /path/to/files -type f -name "*.txt" | cpio -o > text_files.cpio
  4. 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:

    cpio -i --pattern='*.jpg' < my_archive.cpio
  5. 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:

    find /path/to/files -depth | cpio -o | gzip > my_archive.cpio.gz

    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:

    ssh user@remote_server "find /path/to/files -depth | cpio -o" | cpio -i
  6. 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:

    cpio -i --rename=new_ < my_archive.cpio

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.

 

Conclusion

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.