10+ cpio command examples in Linux [Cheat Sheet]


CheatSheet

Reviewer: Deepak Prasad

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:

  1. Copy-out mode (-o or --create): Creates an archive from a list of files.
  2. Copy-in mode (-i or --extract): Extracts files from an archive.
  3. 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:

cpio command to create cpio archive file

 

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:

cpio command to list contents in cpio file

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:

create cpio files with specific files only

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:

cpio command create cpio file with specific files only

 

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:

extract a cpio file with cpio command

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:

cpio command to extract a cpio file to another directory

 

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:

cpio command to create tar archive file

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:

list the content of a tar archive file

 

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:

cpio command to extract tar archive file

 

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:

cpio command to copy entire directory tree

 

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:

cpio command to append to an existing archive

 

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

man page for cpio command

 

Rohan Timalsina

Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment