Home > Archive/Compression > cpio

cpio: Create and Extract File Archives

cpio is a utility used to copy files into or extract files from an archive. It primarily processes file lists received via pipes and is used for backups, restores, and copying directory structures. It can perform various archiving tasks through its three main modes: copy-out (-o), copy-in (-i), and copy-pass (-p).

Overview

cpio operates as a stream-oriented archiver, reading file lists from standard input or writing archives to standard output. This provides powerful flexibility when used with other commands (e.g., find) via pipes.

Key Features

  • Three main operating modes: copy-out (-o), copy-in (-i), copy-pass (-p)
  • Flexible file list processing via pipes
  • Support for various archive formats
  • Used for backups, restores, and copying directory structures

Main Options

cpio requires an option to specify the operating mode and offers various auxiliary options.

Operating Modes

General Options

Generated command:

Try combining the commands.

Description:

`cpio` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various usage examples utilizing cpio's three main modes.

Archive all files in the current directory

find . -print0 | cpio -ov0 > archive.cpio

Generates a list of all files in the current directory using the find command and archives them into archive.cpio using cpio -ov. The -0 option ensures safe handling of filenames with spaces.

Extract files from an archive

cpio -ivd < archive.cpio

Extracts all files from the created archive.cpio into the current directory. The -d option automatically creates necessary directories.

Copy directory structure to another location

find . -depth -print0 | cpio -pd0v /new/directory

Copies all files and directory structures from the current directory to the /new/directory path. The -p mode copies directly without creating an archive, -d creates directories, and -v provides verbose output.

Extract only files matching a specific pattern from an archive

cpio -ivd 'report*' < archive.cpio

Extracts only files starting with 'report' from archive.cpio. Wildcards can be used.

Tips & Precautions

cpio is a powerful tool, but it's important to understand its correct usage.

Useful Tips for Using cpio

  • Use with the `find` command: Since `cpio` receives file lists from standard input, it's common to use the `find` command to generate a file list based on complex criteria and then pipe it to `cpio`.
  • Utilize the `-0` option: When filenames may contain spaces or special characters, it's crucial to use `find -print0` in conjunction with `cpio -0` to ensure filenames are processed correctly.
  • Importance of mode selection: The three modes `-o`, `-i`, and `-p` have distinct purposes, so selecting the correct mode for your task is essential.
  • Absolute vs. Relative Paths: `cpio` uses the paths stored in the archive as they are. If you use relative paths like `find .` when creating an archive, files will be extracted relative to the current directory. Archiving with absolute paths will result in extraction using absolute paths.

Same category commands