cpio

file archivingLinux/Unix
The cpio command is one of the most frequently used commands in Linux/Unix-like operating systems. cpio Copy files to and from archives

Quick Reference

Command Name:

cpio

Category:

file archiving

Platform:

Linux/Unix

Basic Usage:

cpio [options] [arguments]

Common Use Cases

  • 1

    File archiving

    Create archives of files and directories with metadata preserved

  • 2

    System backups

    Backup system files while preserving permissions and ownership

  • 3

    Data recovery

    Extract files from archives during system recovery operations

  • 4

    System installation

    Package files for system installation processes like initramfs

Syntax

cpio {-o|--create} [-0acvABLV] [-C bytes] [-H format] [-M message] [-O [[user@]host:]archive] [-F [[user@]host:]archive] [--file=[[user@]host:]archive] [--format=format] [--message=message] [--null] [--reset-access-time] [--verbose] [--dot] [--append] [--block-size=blocks] [--dereference] [--io-size=bytes] [--quiet] [--force-local] [--rsh-command=command] [--help] [--version] < name-list [> archive]

cpio {-i|--extract} [-bcdfmnrtsuvBSV] [-C bytes] [-E file] [-H format] [-M message] [-R [user][:.][group]] [-I [[user@]host:]archive] [-F [[user@]host:]archive] [--file=[[user@]host:]archive] [--make-directories] [--nonmatching] [--preserve-modification-time] [--numeric-uid-gid] [--rename] [--list] [--swap-bytes] [--swap] [--dot] [--unconditional] [--verbose] [--block-size=blocks] [--swap-halfwords] [--io-size=bytes] [--pattern-file=file] [--format=format] [--owner=[user][:.][group]] [--no-preserve-owner] [--message=message] [--force-local] [--no-absolute-filenames] [--sparse] [--only-verify-crc] [--quiet] [--rsh-command=command] [--help] [--version] [pattern...] [< archive]

cpio {-p|--pass-through} [-0adlmuvLV] [-R [user][:.][group]] [--null] [--reset-access-time] [--make-directories] [--link] [--preserve-modification-time] [--unconditional] [--verbose] [--dot] [--dereference] [--owner=[user][:.][group]] [--no-preserve-owner] [--sparse] [--help] [--version] destination-directory < name-list

Options

Option Description
-i, --extract Extract files from an archive
-o, --create Create an archive
-p, --pass-through Copy files from one directory to another
-t, --list Print a table of contents of the input
-v, --verbose Verbosely list the files processed
-m, --preserve-modification-time Retain previous file modification times when creating files
-d, --make-directories Create leading directories where needed
-H, --format=FORMAT Use the specified archive format (bin, odc, newc, crc)
-0, --null Filenames in the list are terminated by null characters instead of newlines
-a, --reset-access-time Reset access times of files after reading them
-u, --unconditional Replace all files unconditionally
-F, --file=ARCHIVE Use the specified file as the archive instead of stdin or stdout
-R, --owner=[USER][:.][GROUP] Set the ownership of all files created to the specified USER and/or GROUP

Examples

How to Use These Examples

The examples below show common ways to use the cpio command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

# Create a cpio archive
find . -name "*.txt" | cpio -ov > archive.cpio
# Extract files from a cpio archive cpio -iv < archive.cpio
# List contents of a cpio archive cpio -it < archive.cpio
# Create a cpio archive with a different format (newc) find . -name "*.txt" | cpio -H newc -o > archive.cpio

Advanced Examples:

# Copy directory structure (pass-through mode)
find source_dir -type d | cpio -pdmv destination_dir/
# Create a compressed cpio archive find . -name "*.txt" | cpio -ov | gzip > archive.cpio.gz
# Extract from a compressed cpio archive gunzip -c archive.cpio.gz | cpio -iv # Create a cpio archive and preserve file permissions find . -name "*.txt" | cpio -ovm > archive.cpio # Extract only files matching a pattern cpio -iv "*.txt" < archive.cpio # Copy directory structure while preserving ownership and permissions find source_dir | cpio -pdmv --preserve-owner destination_dir/ # Create archive with absolute file paths find /etc -name "*.conf" | cpio -oaV > etc_configs.cpio

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

The cpio command is used to create archives from file lists, extract files from archives, or copy files from one directory tree to another. It's particularly useful for backup operations and system installations.

Key points about cpio:

  • cpio operates in three modes: create (-o), extract (-i), and pass-through (-p)
  • Unlike tar, cpio requires a list of filenames as input (usually from find, ls, or a file)
  • cpio preserves file permissions, ownership, and timestamps when requested
  • It can handle special files like device nodes and symbolic links
  • Various archive formats are supported, including traditional binary format and newer formats like newc
  • Often used with other commands like find, sort, gzip

Common archive formats:

  • bin: The traditional binary format
  • odc: The old (POSIX.1) portable format
  • newc: The new (SVR4) portable format, supports files larger than 2GB and device numbers larger than 65536
  • crc: The new (SVR4) portable format with a CRC checksum added
  • tar: The old tar format
  • ustar: The POSIX.1 tar format
  • hpbin: The old binary format used by HPUX
  • hpodc: The portable format used by HPUX

While cpio is less commonly used than tar for general archiving, it's still important in many system operations:

  • It's used in the Linux initial ramdisk (initrd/initramfs) creation process
  • RPM package files contain cpio archives
  • Some Unix backup systems use cpio format
  • It's useful for preserving exact permissions and ownership during system migrations

When using cpio, remember that:

  • Always use full paths with caution to avoid overwriting important system files during extraction
  • The -d option is essential when extracting archives with directory structures
  • Combine with compression tools like gzip for more efficient storage
  • Use -t to preview archive contents before extraction
  • The pass-through mode is excellent for exact copies of directory structures

Tips & Tricks

1

Use the -i option to create an archive index file

2

Use the -o option to extract files to standard output

3

Use the -t option to test the archive without extracting files

4

Use the -d option to create directories as needed

5

Use the -v option to display verbose output

Common Use Cases

File archiving

Create archives of files and directories with metadata preserved

System backups

Backup system files while preserving permissions and ownership

Data recovery

Extract files from archives during system recovery operations

System installation

Package files for system installation processes like initramfs

Data migration

Move files between systems while preserving all file attributes

Related Commands

These commands are frequently used alongside cpio or serve similar purposes:

Use Cases

1

File archiving

Create archives of files and directories with metadata preserved

2

System backups

Backup system files while preserving permissions and ownership

3

Data recovery

Extract files from archives during system recovery operations

4

System installation

Package files for system installation processes like initramfs

5

Data migration

Move files between systems while preserving all file attributes

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the cpio command works in different scenarios.

$ cpio
View All Commands