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
If you encounter the below error while running the cpio command:
cpio: command not found
you may try installing the below package as per your choice of distribution:
OS Distribution | Command |
---|---|
Debian | apt-get install cpio |
Ubuntu | apt-get install cpio |
Alpine | apk add cpio |
Arch Linux | pacman -S cpio |
Kali Linux | apt-get install cpio |
CentOS | yum install cpio |
Fedora | dnf install cpio |
Raspbian | apt-get install cpio |
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