cpio refers to copy in and copy out. It is a command-line utility in Linux/UNIX for creating, listing, and extracting archive files. It creates a .cpio
archive file. It can copy the complete file system to an archive file.
cpio reads the name of files from the standard input to perform the operation. So, cpio command is mostly used with ls
or find
command to select the files for copying. It is done by piping the output of ls
or find
command to the cpio command.
Syntax and different supported modes with cpio command
The basic syntax of cpio command is:
cpio [options]
Supported Modes:
- Copy-out mode (
-o
or--create
): Creates an archive from a list of files. - Copy-in mode (
-i
or--extract
): Extracts files from an archive. - Copy-pass mode (
-p
or--pass-through
): Copies files from one location to another.
Copy-out Mode (-o
or --create
)
This mode reads the list of file names from the standard input and writes the archive to the standard output.
find /path/to/files -print | cpio -o > archive.cpio
-v
: Verbose mode. Prints the names of files as they are processed.-O file
: Writes the archive to the specified file.-H format
: Specifies the archive format (e.g.,bin
,odc
,newc
,crc
,tar
,ustar
,hpbin
,hpodc
).
Copy-in Mode (-i
or --extract
)
This mode reads an archive from the standard input and extracts the files to the current directory.
cpio -i < archive.cpio
-d
: Creates leading directories where needed.-v
: Verbose mode. Prints the names of files as they are processed.-t
: Lists the files in the archive.-u
: Replaces existing files without prompting.-F file
: Reads the archive from the specified file.-H format
: Specifies the archive format.
Copy-pass Mode (-p
or --pass-through
)
This mode reads the list of file names from the standard input and copies the files to the specified directory.
find /source/path -print | cpio -p /destination/path
-v
: Verbose mode. Prints the names of files as they are processed.-d
: Creates leading directories where needed.-u
: Replaces existing files without prompting.-m
: Retains the original modification times.
1. Create a .cpio archive file
You can use the -o
or --create
option to create a .cpio archive containing files and directories. This is the copy-in mode in which the selected files are copied to the .cpio archive.
The following command copies the content of the directory /home/golinux/complex
to a /home/golinux/complex.cpio
archive file.
$ find /home/golinux/complex | cpio -ov > /home/golinux/complex.cpio
The -v
option displays the list of files processed by the cpio command.
Sample Output:
2. List the content of .cpio file
The -t
option prints the table of contents and does not copy any files. You can use the -i
and -t
option to list the content of a .cpio
file. If you also use -v
with -t
, it displays the output similar to the ls -l
command.
To get the content of above created complex.cpio
file, run the following command.
$ cpio -itv < /home/golinux/complex.cpio
Sample Output:
As you can see, it contains the /home/golinux/complex
directory and all the files present in it.
3. Create a .cpio file with specific files
You can also create a .cpio
file containing specific files. For example, the following command creates a file textfile.cpio
with only .txt
files present in the current working directory.
$ ls *.txt | cpio -ov > /home/golinux/textfile.cpio
Sample Output:
If you want to copy files from another directory, you have to use the find command. The following example creates a image.cpio
file containing all .png
files from the directory /home/golinux/complex
.
$ find /home/golinux/complex -iname *.png | cpio -ov > /home/golinux/image.cpio
Sample Output:
4. Extract cpio file using cpio command
The -i
or --extract
option extract a .cpio
file containing files and directory. The following command extracts the complex.cpio
file in the current working directory.
$ cpio -iv < /home/golinux/complex.cpio
Sample Output:
If the newer or same version already exists in the location, cpio will not extract and print the error message as shown in the image below.
5. Extract .cpio file in the specified directory
If you want to extract a .cpio
file in another location, you have to specify the directory with -D
option. The following command extracts a textfile.cpio
file to the /home/golinux/record
directory.
$ cpio -ivD /home/golinux/record < /home/golinux/textfile.cpio
Sample Output:
6. Create .tar file using cpio command
The cpio command can also create a .tar
archive file. The -H
option allows you to specify the archive format.
The following command will create a record.tar
file containing all files of the current directory.
$ ls | cpio -ov -H tar > /home/golinux/record.tar
Sample Output:
Or, you can specify the tar filename with -F
flag.
$ ls | cpio -ov -H tar -F /home/golinux/record.tar
7. List the content of a .tar file
You can print the content of a .tar
by running the following command.
$ cpio -itv -F record.tar
Sample Output:
8. Extract a .tar file with cpio command
The following example extracts a record.tar
file in the snap
folder present in the current user's home directory.
$ cpio -iv -D ~/snap -F record.tar
Sample Output:
9. Copy files from one directory tree to another
The -p
option runs in copy-pass mode. It directs cpio to copy files from one directory tree to another. It is the combination of copy-out and copy-in mode without creating an archive.
The following command copies the entire directory tree /home/golinux/computing
to the test
directory.
$ find /home/golinux/computing | cpio -pdv test
The -d
option creates the directory as needed.
Sample Output:
10. Append to an existing archive
You can append files to an existing archive using the -A
option. The following command will append to an existing archive textfile.cpio
with all files and folders in the /home/golinux/test_folder
directory.
$ find /home/golinux/test_folder | cpio -oA -F textfile.cpio
Sample Output:
11. Retain modification time of files when extracting files
By default, the modification time and access time of a file are set to the time when it is extracted from the archive file. With -m
option, you can preserve the previous modification time when creating files.
This command extracts the files from a complex.cpio
with the previous modification time.
$ cpio -imv < /home/golinux/complex.cpio
12. Copy the original file instead of symbolic links
The -L
option helps to copy the original file that the symbolic link points to. It will not copy the symbolic link file.
$ ls | cpio -oLv > /home/golinux/complex.cpio
Conclusion
In this tutorial, you have learned several examples of cpio command to copy files into an archive or out of an archive. It is a useful command for backing up files and directories in Linux. We also showed how you can create a tar archive file using cpio command. If you have any confusion, feel free to ask us in the comment section.
What's Next
15+ tar command examples in Linux [Cheat Sheet]
Linux zip folder | 16 practical Linux zip command examples
Further Reading