cpio program

cpio (copy in/out) is a file archiver utility that uses the result of the ls or find command to generate files to be archived. cpio does not compress any content, but resulting archives are often compressed using gzip or other compression utilities.

cpio has three operating modes:

  • copy-out mode – creates an archive from the output of the ls or find command.
  • copy-in mode – extracts files from an archive.
  • copy-pass mode – copies files from one directory to another.

Copy-out mode

The copy-out mode is used with the -o or –create option to create archives by accepting the output of the ls or find command as the input for the archive. For example, to archive all files in our current directory, we use the following command:

linux cpio create archive

The -v option shows which files are being archived.

 

Here is how we can archive the directory /home/bob/example_dir using the find command’s results:

linux cpio create archive using the find command

Copy-in mode

The copy-in mode is used to extract archives. The cpio command uses the standard input redirection symbol (<) to extract an archive. The -i option is used to extract files from a cpio archive.

Here is how we can extact files from the archive we’ve created in the previous step:

linux cpio extract archive

Copy-pass mode

The copy-pass mode is activated by the -p or –pass-through option. You can copy files from one directory and paste them in another directory without actually creating an archive. The benefit of using this mode instead of the cp command is that, unlike the cp command, cpio preserves modification times and ownership.

Here is an example:

linux cpio copy files

In the example above you can see that we’ve copied all the files from the current directory (/home/bob/example_dir) to the /home/bob/new_directory directory. Note that the original files were preserved.

Geek University 2022