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.
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:
Here is how we can archive the directory /home/bob/example_dir using the find command’s results:
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:
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:
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.