- Unix Commands Reference
- Unix Commands - Home
cpio Command in Linux
copy in, copy out, commonly referred to as cpio is a command used in Linux systems to create, list and extract archive files. It operates with files in the .cpio or .tar format.
You can use the cpio command to bundle files together into an archive or extract files from an existing archive, including copy-out (create), copy-in (extract), and copy-pass (copy to a destination directory). It is an effective tool for the users who want to manage their archive files efficiently.
Table of Contents
Here is a comprehensive tutorial to understand the various options available with the cpio command −
Syntax of cpio Command
The cpio command has different modes, and for each mode, the syntax is different too.
Copy-out Mode (Create an Archive)
cpio -o <name-list> archive
Copy-in Mode (Extract from an Archive)
cpio -i <archive>
Copy-pass Mode (Copy Files to Destination Directory)
cpio -p destination-directory <name-list>
cpio Command Options
The cpio command provides different options depending on the operation mode you choose. Whether you are extracting files, creating an archive, or performing other tasks, these options help you in customizing the command according to your specific needs. These options are discussed below −
Main Operation Mode
Option | Description |
---|---|
-i, --extract | Allows you to extract files from an archive. It operates in copy-in mode. |
-o, --create | In this mode, you can create an archive. It runs in a copy-out mode. |
-p, --pass-through | This mode is for copying files without modification. It operates in copy-pass mode. |
-t, --list | Use this mode to print a table of contents from the input archive. |
Operational Modifier that are Valid in any Mode
Option | Description |
---|---|
--blocksize=BLOCK-SIZE | Allows you to set the I/O block size to BLOCK-SIZE multiplied by 512 bytes. |
-B | Set the I/O block size to 5120 bytes. |
-c | When specified, it indicates the use of old portable (ASCII) archive format. |
-C, --io-size=Number | Set the I/O block size to a specific number of bytes. |
-D, --directory=DIR | Change the current working directory to the specified DIR. |
--force-local | Use it if the archive file is local, no matter if its name contains colons. |
-H, --format=FORMAT | Specify a particular archive format to use. |
--quiet | Suppress printing the number of blocks copied during the operation. |
-R, --owner=[USER][:.][GROUP] | Sets all created files ownership to the specified USER and/or GROUP. |
-v, --verbose | Verbosely lists the files that are processed. |
-V, --dot | Print a dot (.) for each line processed. |
-W, --warning=FLAG | Controls warning display. The available options are ‘none’, ‘truncate’, and ‘all’. Multiple options can accumulate. |
Operational Modifiers that are Valid in copy-in and copy-out Modes
Option | Description |
---|---|
-F, --file=[[User@]HOST:]FILE-NAME | Specify an archive file instead of the standard input (in copy-in mode) or standard output (in copy-out mode). You can also provide optional USER and HOST to handle remote archives. |
-M, --message=STRING | Print a specific STRING once reaching the end of a volume of the backup media. |
--rsh-command=COMMAND | Use the specified COMMAND instead of the default rsh command for remote operation. |
Operational Modifiers that are Valid in copy-in Mode
Option | Description |
---|---|
-b, --swap | Perform a byte and halfword swap on the data, equivalent to using the -sS option. |
-f, --nomatching | Copy only those files that don't match any of the provided patterns. |
-I [[USER@]HOST:]FILE-NAME | Specify an archive filename to use instead of using standard input; optional USER and HOST can be provided for remote archives. |
-n, --numeric-uid-gid | Include numeric User IDs (UID) and Group IDs (GID) in the verbose table of contents listing. |
-r, --rename | Rename files interactively during extraction. |
-s, --swap-bytes | Swap each halfword bytes in the files. |
-S, --swap-halfwords | Swap each word’s halfword (4 bytes) in the files. |
--stdout | Retrieve file contents directly to standard output. |
--only-verify-crc | When examining a CRC format archive, validate the CRCs for each file without performing the actual extraction. |
Operational Modifiers that are Valid Only in copy-out Mode
Option | Description |
---|---|
-A, --append | Allows you to append to an existing archive file. |
--device-independent, --reproducible | Creates device-independent (reproducible) archives. |
--ignore-devno | Avoids storing device numbers. |
--ignore-dirlink | Ignores the number of links of a directory and always assumes 2 links. |
-0 [[USER@]HOST:]FILE-NAME | Provide the filename of an archive to use instead of using standard output. Optionally, you can also specify USER and HOST for handling remote archives. |
--renumber-inodes | Renumber nodes during the archive creation process. |
Operational Modifiers that are Valid Only in copy-pass Mode
Option | Description |
---|---|
-l, --link | Enables you to create links to files rather than copying them, whenever it is feasible. |
Operational Modifiers that are Valid in Both copy-in and copy-out Modes
Option | Description |
---|---|
--absolute-filenames | When this option is used, it retains the file system prefix components in the file names. |
--no-absolute-filenames | Generates files with paths relative to the current directory. |
Operational Modifiers that are Valid in copy-out and copy-pass Modes
Option | Description |
---|---|
-0, --null | File names in the list are separated by null characters instead of newlines. |
-a, --reset-access-time | Resets the access times for files after reading them. |
-L, --dereference | When dereferencing symbolic links, the file they point to is copied instead of copying the links themselves. |
Operational Modifiers that are Valid in copy-in and copy-pass Modes
Option | Description |
---|---|
-d, --make-directories | Creates leading directories when needed during the archive creation process. |
-m, --preserve-modification-time | Preserves the original file modification times when creating new files. |
--no-preserve-owner | Ensures that the ownership of the files is not changed. |
--sparse | Writes files with large blocks of zero as sparse files. |
-u, --unconditional | Replaces all files unconditionally. |
--help | Displays a list of available commands and their descriptions. |
--usage | Provides a brief summary of how to use the program. |
--version | Prints the program’s version number. |
Examples of cpio Command in Linux
Let’s explore a few basic examples of cpio command in Linux system −
- Create a .cpio Archive File
- List the Content of a .cpio File
- Extract Files from a .cpio Archive File
- Creates a Tar Archive File
- Copy Files from One Directory Tree to Another
- Append Files to an Existing Archive File
Create a .cpio Archive File
One of the basic functions of the cpio command on Linux is to create a .cpio archive file. It can be possible by using the -o or --create option with the command. For example, in the following command, we use the output of the ls command (which lists files in the current directory) as input for cpio and creates a .cpio archive file with the name archive.cpio.
ls | cpio -o > archive.cpio
The output of the ls command is piped (|) to cpio -o, which creates a .cpio archive.
The -o option specifies the creation mode for cpio. The resulting archive is saved to a file named archive.cpio.
You can also specify files according to your choice using the wildcard entry option * and then create your archive file with the .cpio extension. The command is provided below that creates an archive file which includes all text files at the current location −
ls *.txt | cpio -o > archive.cpio
List the Content of a .cpio File
If you have created an archive file with the .cpio format and want to list the content of that files, you can simply run the below-given command −
cpio -t < archive.cpio
The above command will display the contents of the specified .cpio archive, allowing you to see which files are included. Make sure to specify the file name according to the one you have created using the cpio command.
Extract Files from a .cpio Archive File
You can also extract the files from a .cpio archive file by using the -i option, the command is given below −
cpio -i < archive.cpio
Replace archive.cpio with the actual path to your .cpio archive. The above command will extract the files stored in the archive into the current working directory.
Creates a Tar Archive File
Apart from creating a .cpio archive file, you can also use the cpio command to create an archive file in the .tar format. You can do this by using the -o and -H option followed by the redirection symbol with the filename in the end.
ls | cpio -o -H > archive.tar
Note − You can use similar commands to interact with .tar archive files as you did with .cpio archives, such as viewing and extracting the archive file content.
Copy Files from One Directory Tree to Another
The cpio command can also be used to copy files from one directory tree to another, this can be possible once you use the -p option. This option will allow us to specify a destination path.
For example, suppose we want to copy the output of the ls command (which lists files from the current directory) to a new location at /home/ubuntu/newdire/. The command would be as follows −
ls | cpio -p > /home/ubuntu/newdire
Append Files to an Existing Archive File
You can also use -A option with the cpio command to append additional files to an existing archive file. For example, the command given below will append to an existing archive.cpio with all files and folders in the /home/ubuntu/Downloads directory −
find /home/ubuntu/Downloads | cpio -oA -F archive.cpio
In the above command, the -oA flags indicate that we are creating an archive and appending to it, while the -F archive.cpio specifies the existing archive file (archive.cpio) to which we want to add files.
That’s how you can use the cpio command to create a list and extract files in your Linux system. For more details about using other options, you can get help by opening the cpio manual on the terminal using the following command −
man cpio
Conclusion
The cpio command is a versatile and powerful tool that is used for creating, listing, and extracting archive files on Linux systems. It supports various archive formats and provides options for managing files efficiently, as described in the above section of this guide.
Whether you are working with .cpio or .tar archives, the cpio command simplifies file manipulation tasks. You can explore its capabilities further and adapt commands to your specific needs if required.