cpio Command Examples in Linux

The cpio command copies files to and from archives. The cpio command has three operating modes.

Operating Mode Command Used To
Copy-out cpio -o Copy files into an archive. It reads the standard input to obtain a list of file names and then copies those files to the standard output.
Copy-in cpio -i Copy files from an archive. It extracts files from the standard input. This option is used in data recovery.
Copy-pass cpio -p Copy files from one directory tree to another. It reads the standard input to obtain the list of file names that are created and copied into the destination directory.

The syntax of the cpio command depends on its mode. In all modes, the command reads from standard input. The following copy-out example archives all contents of a directory by piping ls to cpio and sending the archive output to dir_arch:

# ls | cpio -o > dir_arch

In copy-in mode, you can extract an archive as follows:

# cpio -i 

In copy-pass mode, you can pipe find to cpio to copy one directory tree to another:

# find . -depth -print | cpio -p new_dir

cpio Command Examples

1. Take a list of file names from standard input and add them [o]nto an archive in cpio's binary format:

# echo "file1 file2 file3" | cpio -o > archive.cpio

2. Copy all files and directories in a directory and add them [o]nto an archive, in [v]erbose mode:

# find path/to/directory | cpio -ov > archive.cpio

3. P[i]ck all files from an archive, generating [d]irectories where needed, in [v]erbose mode:

# cpio -idv 
Related Post