10+ cpio command examples in Linux [Cheat Sheet]


Written By - Rohan Timalsina
Advertisement

Introduction to cpio command

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.

 

Different supported modes with cpio command

There are three modes in cpio command. They are:

a. Copy-out mode: In this mode, cpio copies files into an archive with the -o or --create option. It reads a list of filenames from the standard input and writes to an archive file.

Syntax:

$ cpio -o [option] < name-list > archive

 

b. Copy-in mode: cpio copies files out of an archive or prints the archive contents. In this mode, cpio uses the -i or --extract option to read the archives from the standard input.

Syntax:

$ cpio -i [option] < archive

 

c. Copy-pass mode: cpio copies files from one directory tree to another in this mode. It does not create an archive file.

Advertisement

Syntax:

$ cpio -p [option] destination-directory < name-list

 

Some important options in the cpio command are:

  • -o, --create: create a .cpio archive file
  • -i, --extract: extract files from an archive file
  • -p, --pass-through: copies files from one directory tree to another
  • -v, --verbose: display verbose information
  • -t: List input files

The options -o, -i, and -p must be specified in the cpio command.

 

Different examples to use cpio command

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:

Advertisement

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:

Advertisement

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

 

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 either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment