cpio

cpio

The cpio command in Unix and Linux is used for creating and extracting archives or backups. It is a versatile tool that works well with streams of data, making it suitable for tasks like backup creation and file transfer.

Basic Usage

The basic syntax for the cpio command is:

cpio [options]

Examples

Creating an Archive

To create a cpio archive from a list of files:

find . -depth -print | cpio -o > archive.cpio
  • find . -depth -print: Generates a list of files to include in the archive.

  • cpio -o: Creates an archive (-o for output).

Extracting an Archive

To extract files from a cpio archive:

cpio -i < archive.cpio
  • cpio -i: Extracts files from the specified archive (-i for input).

Verbose Output

To see verbose output during extraction:

cpio -iv < archive.cpio
  • -v: Verbose mode, displays filenames as they are processed.

Options

Archive Creation Options

  • -o: Create an archive (-i for input).

  • -F file: Use file as the archive filename (instead of stdin or stdout).

  • -H format: Specify the archive format (bin, crc, newc, odc, tar).

Extraction Options

  • -i: Extract files from an archive (-o for output).

  • -d: Create leading directories where needed.

  • -t: List the contents of the archive without extracting.

Other Options

  • -v: Verbose mode, show filenames as they are processed.

  • -C directory: Change to directory before performing any operations.

Practical Use Cases

Creating Backups

cpio is useful for creating backups of directories or entire filesystems:

find /path/to/dir -depth -print | cpio -o > backup.cpio

Transferring Files

cpio can transfer files between systems using pipes or redirection:

find /path/to/dir -depth -print | cpio -o | ssh user@remote "cpio -i"

Working with Incremental Backups

cpio supports incremental backups with the -o option, allowing you to back up only changed files since the last backup.

Summary

The cpio command is a flexible tool for creating, listing, and extracting archives in Unix and Linux environments. It provides options for various archive formats, verbose output, and integration with other commands for efficient data handling tasks like backups and file transfers. Understanding its usage and options can help you effectively manage file archives and backups on your system.

help

Last updated