cpio Command

Purpose

Copies files into and out of archive storage and directories. This document describes the AIX® cpio command and the System V cpio command.

Syntax

cpio -oa ] [  c ] [ -E{force|ignore|warn} ] [ -U ] [  v ] [  B | C Value ] [ -Z ] <FileName >Output

cpio -ib ] [  c ] [  d ] [ -E{force|ignore|warn} ] [ f ] [  m ] [ M ] [ r ] [ s ] [ t ] [ -U ] [  u ] [ v ] [  S ] [ 6 ] [  B | C Value ] [ -Z ] [  Pattern... ] <Input

cpio -pa ] [  d ] [ -E{force|ignore|warn} ] [  l ] [  m ] [  M ] [ -U ] [  u ] [  v ] [ -Z ] Directory <FileName

Description

Attention: If you redirect the output from the cpio command to a special file (device), you should redirect it to the raw device and not the block device. Because writing to a block device is done asynchronously, there is no way to know if the end of the device is reached.
Note:
  1. The cpio command is not enabled for files greater than 2GB in size due to limitations imposed by XPG/4 and POSIX.2 standards.
  2. cpio does not preserve the sparse nature of any file that is sparsely allocated. Any file that was originally sparse before the restoration will have all space allocated within the filesystem for the size of the file.
  3. You cannot use the System V cpio command for Encrypted File Systems.

cpio -o Command

The cpio -o command reads file path names from standard input and copies these files to standard output, along with path names and status information. Avoid giving the cpio command path names made up of many uniquely linked files, as it may not have enough memory to keep track of them and would lose linking information.

cpio -i Command

The cpio -i command reads from standard input an archive file created by the cpio -o command and copies from it the files with names that match the Pattern parameter. These files are copied into the current directory tree. You can list more than one Pattern parameter, using the file name notation described in the ksh command. Note that in this application the special characters * (asterisk), ? (question mark), and [...] (brackets and ellipses) match the / (slash) in path names, in addition to their use as described in the ksh command. The default for the Pattern parameter is an * (asterisk), selecting all files in the Input. In an expression such as [a-z], the minus sign means through according to the current collating sequence.

A collating sequence can define equivalence classes for use in character ranges.

cpio -p Command

The cpio -p command reads file path names from standard input and copies these files into the directory named by the Directory parameter. The specified directory must already exist. If these path names include directory names that do not already exist, you must use the d flag to cause the specified directory to be created.
Note: You can copy special files only if you have root user authority.

cpio -U command

For AIX 5.3, the cpio command will ignore extended attributes by default. The -U option informs cpio to archive or restore attributes, which includes ACLs.

A new record type is required for extended attribute entries in cpio archive files. A new record type is also required for ACL entries in cpio archive files.

Each object in the cpio archive contains a cpio header followed by the data for the specified object.

The following table describes the cpio header:
Name of field Size (number of bytes) Use
h_magic 2 Magic number for identifying header.
h_dev 2 Device that contains a directory entry for this file.
h_ino 2 Inode number that identifies the input file to the file system.
h_mode 2 Mode of the input file, as defined in the mode.h file. The POSIX standard has 0130000, 0150000 - 0170000 available for file types that are not to be transported to other systems.
h_uid 2 User ID of the owner of the input file.
h_gid 2 Group ID of the owner of the input file.
h_nlink 2 Number of links that are connected to the input file.
h_rdev 2 ID of the remote device from which the input file is taken.
h_mtime 4 Time when data was last modified.
h_namesize 2 Length of the pathname including the NULL.
h_filesize 4 Length of the file in bytes.
h_name PATH_MAX Null-terminated pathname.
Each file which has an ACL will have a <header,data> object immediately preceding the object itself which describes the ACL as follows:
Header for ACL
The h_mode field set to 0130000 indicates the header describes an ACL. Additionally, the h_mode bits are set to indicate who can write the ACL. All other fields in the cpio header are set as for the inode of the file owning the ACL.
Data
The data will be the ACL itself. The first 64-bits of the data will be the ACL type. It will be immediately followed by the ACL value.
Each extended attribute will have a single <header,data> object in the archive which completely describes the extended attribute as follows:
Header for EA
The h_mode field set to 0150000 indicates an extended attribute header. All fields in the cpio header are set as for the inode of the extended attribute. Except the h_name field is set to <NULL><EAName><NULL>
Data:
This is formatted to describe the owner of the extended attribute as well as the data for the extended attribute. There is a eaHeader followed by the pathname of the owner of the extended attribute, followed by the extended attribute data.
struct eaHeader {
	char	pathLen[12];
	char	dataLen[12];
};

Parameters

Item Description
Directory Specifies the directory.
<FileName Specifies a list of file names for the cpio command to use as input.
>Output Specifies the output device such as a diskette or file. For more information on using tape devices see the rmt special file.
<Input Specifies the input device (where Input is the Output file created by the cpio -o command). For more information on using tape devices, see the rmt special file.
Pattern Specifies the pattern (as described in the ksh command) to be used with the command. The default for the Patternparameter is an * (asterisk), selecting all the files in the Input.

Flags

All flags must be listed together, without any blanks between them. Not all of the following flags can be used with each of the -o, -i, and -p flags.

Item Description
a Resets the access times of the source files to their previous times.
b Swaps both bytes and halfwords.
Note: If there is an odd number of bytes or halfwords in the file being processed, data can be lost.
B Performs block input and output using 512 bytes to a record.
Note: When using the B or C options to extract or create a tape archive, the blocking factor must be a multiple of the physical block size for that tape device.

When using the B or C options to extract an archive from tape, the blocking factor should not be larger than the size of the archive as it exists on the tape.

The B flag and the C flag are mutually exclusive. If you list both, the cpio command uses the last one it encounters in the flag list.

c Reads and writes header information in ASCII character form. If a cpio archive was created using the c flag, it must be extracted with c flag.
C Value Performs block input and output using the Value parameter times 512 bytes to a record. For instance, a -C2 flag changes the block input and output sizes to 1024 bytes to a record.
d Creates directories as needed.
-E The -E option requires one of the following arguments. If you omit the -E option, warn is the default cpio behavior.
force
Fails the extract or copy operation on a file if the file's extent attributes cannot be preserved.
ignore
Ignores any errors in preserving extent attributes.
warn
Issues a warning if the space reservation or the fixed extent size of the file cannot be preserved. This is the default behavior.
f Copies all files except those matching the Pattern parameter.
l Links files rather than copying them, whenever possible. This flag can only be used with the cpio -p command.
m Retains previous file modification time. This flag does not work when copying directories.
M Retains previous file modification time even when directories are copied.
r Renames files interactively. If you do not want to change the file name, enter a single period or press the <Enter> key. In the latter case, the cpio command does not copy the file.
s Swaps bytes. This flag is used only with the cpio -i command.
Note: If there is an odd number of bytes in the file being processed, data can be lost.
S Swaps halfwords. This flag is usable only with the cpio -i command.
Note: If there is an odd number of halfwords in the file being processed, data can be lost.
t Creates a table of contents. This operation does not copy any files.
-U Performs archival and extraction of ACL and Extended Attributes. Attributes include Access control list (ACL) also. If the ACL type is not supported on the Target filesystem then it is converted to the ACL type supported by the Target filesystem. If the EA is not supported on the filesystem then it is not copied.
u Copies unconditionally. An older file now replaces a newer file with the same name.
v Lists file names. If you use this with the t flag, the output looks similar to that of the ls -l command.
6 Processes an old file (for example, one written in UNIX Sixth Edition format). This flag is usable only with the cpio -i command.
-Z Archives the Encrypted File System (EFS) information of encrypted files or directories. The EFS information is extracted. When you specify the -t and -v flags along with the -Z flag, an e indicator is displayed after the file mode for encrypted files and directories that were archived with the -Z flag, and a hyphen (-) is displayed for other files.
Note: Archives created with the -Z flag can be restored only on AIX 6.1 or later releases.

Exit Status

This command returns the following exit values:

Item Description
0 Successful completion.
>0 An error occurred.

Security

Attention RBAC users and Trusted AIX users: This command can perform privileged operations. Only privileged users can run privileged operations. For more information about authorizations and privileges, see Privileged Command Database in Security. For a list of privileges and the authorizations associated with this command, see the lssecattr command or the getcmdattr subcommand.

Examples

  1. To copy files onto diskette, enter:
    cpio -ov <filenames >/dev/rfd0
    This copies the files with path names listed in the filenames file in a compact form onto the diskette (>/dev/rfd0). The v flag causes the cpio command to display the name of each file as it is copied. This command is useful for making backup copies of files. The diskette must already be formatted, but it must not contain a file system or be mounted.
    Note: Files with uid's and gid's greater than 65535 cannot be archived using the cpio command. In such instances, the user should use backup and restore.
  2. To copy files in the current directory onto diskette, enter:
    ls *.c | cpio -ov >/dev/rfd0
    This copies all the files in the current directory whose names end with .c
  3. To copy the current directory and all subdirectories onto diskette, enter:
    find . -print | cpio -ov >/dev/rfd0
    This saves the directory tree that starts with the current directory (.) and includes all of its subdirectories and files. Do this faster by entering:
    find . -cpio /dev/rfd0 -print
    The -print entry displays the name of each file as it is copied.
  4. To list the files that have been saved onto a diskette with the cpio command, enter:
    cpio -itv </dev/rfd0
    This displays the table of contents of the data previously saved onto the /dev/rfd0 file in the cpio command format. The listing is similar to the long directory listing produced by the ls -l command. To list only the file path names, use only the -it flags.
  5. To copy the files previously saved with the cpio command from a diskette, enter:
    cpio -idmv </dev/rfd0
    This copies the files previously saved onto the /dev/rfd0 file by the cpio command back into the file system (specify the -i flag). The d flag allows the cpio command to create the appropriate directories if a directory tree is saved. The m flag maintains the last modification time in effect when the files are saved. The v flag causes the cpio command to display the name of each file as it is copied.
  6. To copy selected files from diskette, enter:

    cpio -i "*.c" "*.o" </dev/rfd0

    This copies the files that end with .c or .o from diskette. Note that the patterns "*.c" and "*.o" must be enclosed in quotation marks to prevent the shell from treating the * (asterisk) as a pattern-matching character. This is a special case in which the cpio command itself decodes the pattern-matching characters.
  7. To rename files as they are copied from diskette, enter:
    cpio -ir </dev/rfd0
    The -r flag causes the cpio command to ask you whether to rename each file before copying it from diskette. For example, the message:
    Rename <prog.c>
    asks whether to give the file saved as prog.c a new name as it is copied. To rename the file, type the new name and press the Enter key. To keep the same name, you must enter the name again. To avoid copying the file at all, press the Enter key.
  8. To copy a directory and all of its subdirectories, enter:
    mkdir /home/jim/newdir
    find . -print | cpio -pdl /home/jim/newdir
    This duplicates the current directory tree, including the current directory and all of its subdirectories and files. The duplicate is placed in the new /home/jim/newdir directory. The l flag causes the cpio command to link files instead of copying them, when possible.
    Note: The performance of cpio to the 9348 Magnetic Tape Unit Model 12 can be improved by changing the default block size. To change the block size, enter the following at the command line:
    chdev -1 <device_name> -a block_size=32k
  9. To copy files in the current directory onto diskette and preserve the ACL and EA associated with the files, enter:
    ls *.c | cpio -oUv >/dev/rfd0

Files

Item Description
/usr/bin/cpio Contains the cpio command.

System V cpio Command

Purpose

Copies files into and out of archive storage and directories.

Syntax

cpio -i [ -b ] [ -B ] [ -c ] [ -d ] [ -f ] [ -k ] [ -m ] [ -r ] [ -s ] [ -S ] [ -T ] [ -t ] [ -u ] [ -v ] [ -V ] [ –6 ] [ -C bufsize ] [ -E file ] [ -H hdr ] [ -I file [ -M message ] ] [ -R ID ] ] [ Patterns ...]

cpio -o [ -a ] [ -A ] [ -B ] [ -c ] [ -L ] [ -v ] [ -V ] [ -C bufsize ] [ -H hdr ] [ -K mediasize ] [ -O file [ -M message ] ]

cpio -p [ -a ] [ -d ] [ -l ] [ -L ] [ -m ] [ -u ] [ -v ] [ -V ] [ -R ID ] Directory

Description

The cpio command copies files into and out of an archive. The -i, -o and -p options select the action to be performed. The following list describes each of the actions. The -o, -p and -i options are mutually exclusive.

cpio -i (copy in)

cpio -i (copy in) extracts files from the standard input (only if -I is not specified), which is assumed to be the product of a previous cpio -o. Only files with names that match Patterns are selected. Patterns are regular expressions given in the filename generating notation of ksh. In Patterns, meta-characters "?", "*", and "[ . . . ]" match the slash ("/") character, and backslash ("\") is an escape character. A "!" meta-character means not. (For example, the "[!abc]" pattern would exclude all files that begin with either a, b or c.) Multiple patterns may be specified and if no patterns are specified, the default for Patterns is "*" (that is, select all files). Each pattern must be enclosed in double quotes; otherwise, the name of a file in the current directory might be used. Extracted files are conditionally created and copied into the current directory tree based on the options described below.

The cpio -i command reads the standard input of an archive file created that was using the cpio -o command, and copies the files with names that match the pattern parameter. The pattern parameter is a regular expression given with general notation of ksh. These files are copied into the current directory tree. More than one pattern parameter can be used, using the file name notation described in the ksh command. The patterns can be special characters * (asterisk), ? (question mark), and [...] (brackets and ellipses). The default for the pattern parameter is an * (asterisk), selecting all files in the input. In an expression such as [a-z], the minus sign means through according to the current collating sequence.

The permissions of the files will be those of the previous cpio -o. Owner and group permissions will be the same as the current user unless the current user is the root user. If this is true, owner and group permissions will be the same as those resulting from the previous cpio -o. Blocks are reported in 512-byte quantities.

If cpio -i tries to create a file that already exists and the existing file is the same age or younger (newer), cpio will output a warning message and not replace the file. On the other hand if the file being extracted is older than the one in the cpio archive then the existing file will be replaced without any warning from the command.

cpio -o (copy out)

cpio -o reads the standard input to obtain a list of path names and copies those files onto the standard output together with path name and status information.

cpio -p (copy pass)

cpio -p reads the standard input to obtain a list of path names of files and copies these files into the directory named by the Directory parameter. The specified directory must already exist. If these path names include directory names that do not already exist, you must use the d flag to cause the specified directory to be created. By default the Access Control List's (ACL) are transferred [copied] from source file to destination file with this option only.

Flags

Item Description
-a Resets the access time of the source files to their previous times.
-A Appends files to an archive. The -A option requires the -O option. The append option -A is not valid for the rmt special file and diskettes.
-b Reverse the order of the bytes within each word. This option is valid only with the -i option.
-B The default buffer size is 512 bytes when neither this nor the -C option is used. But when -B flag is used the buffer size is set to 5120 bytes block for the Input/Output operations.
-c Read or write header information in ASCII character form for system interoperability and portability. The -c option is mutually exclusive with -H and -6. Either the -c or -H option can be used when the target and destination machines are different types.
-C bufsize The block size for Input/Output operation is set to bufsize, where bufsize indicates the buffer size in positive integer. If used with -K, bufsize must be a multiple of 1K.
-d Creates directories as needed.
-E file Specify an input file (file) that contains a list of file names to be extracted from the archive with one file name per line.
-f Copy in all files except those in Pattern parameter.
-H hdr Read or write header information in hdr format. Either the -h or -c option can be used when the target and the destination machines are different types. This option is mutually exclusive with the -c and -6 options. This format allows system interoperability and portability. The cpio utility supports the archival of files larger than 2 GB in size when using the ASCII (-c ), CRC (-Hcrc ), tar ( -Htar), or ustar (- Hustar) formats. Valid values for hdr are:
crc
Same as CRC. ASCII header with an additional per-file checksum. The crc file format will handle files larger than 2 GB.
ustar
Same as USTAR. IEEE/P1003 Data Interchange Standard header and format.
tar
Same as TAR. Tar header and format. The tar format is provided for compatibility with the tar program.
odc
ASCII header with small fundamental types.
-I file Read the contents of file as an input archive. If file is a character special device, and the current medium has been completely read, replace the medium and press the Enter key to continue to the next medium. This option is valid only with the -i option.
-k Attempt to skip corrupted file headers and I/O errors that may be encountered. This option lets the user read only those files with good headers if files from a medium that is corrupted. This option is valid only with the -i option.
-K mediasize Specify the media size as a multiple of 1K. If used with -C bufsize, then bufsize must be a multiple of 1K.
-l Hard links files rather than copying them, whenever possible. If a file cannot be linked, then it will be copied. This option is valid only with -p option.
-L This option assists in copying the files rather than linking. The content of the link file is copied with the links name. Without -L or -l option, the symbolic links will be maintained as is default with -p.
-m Retain previous file modification time. The modification time and access time of a restored file is set to the modification time of the file when it was backed up. Modification time of directories is not retained.
-M message Define a message to use when switching media. When the -O or -I options are given cpio on a special device, this option can be used to define the message that is printed when you reach the end of the medium. A %d can be placed in message to print the sequence number of the next medium needed to continue.
-O file Direct the output of cpio to file. If file is a special device and the current medium is full, replace the medium and type Enter to continue to the next medium. This option is valid only with the -o option.
-r Renames files interactively. To skip a file, type Enter. To retain the original path name, type . (period). This option is valid only with the -i option.
-R ID Reassigns ownership and group information for each file to a valid user ID. This option is valid only for the root user.
-s Swap bytes within each half word.
Note: The -s and the -S flags are basically for byte sequencing.
-S Swap half words within each word.
Note: The -s and the -S flags are basically for byte sequencing.
-t Creates a table of contents. This operation does not create any files. The -t flag and the -V flag are mutually exclusive.
-T Truncates long file names to 14 characters. This option is valid only with the -i option.
-u Copies unconditionally (normally, an older file will not replace a newer file with the same name).
-v This is the verbose option that causes a list of file names to be printed. When used with the -t option, the table of contents looks like the output of an ls -l command.
-V This is a special verbose option that allows to print a dot for each file read or written. Useful to assure the user that cpio is working without printing out all file names. Note that the -V and -v options are mutually exclusive and whichever occurs earlier in the command line will be processed accordingly ignoring the other.
-6 Processes a UNIX System Sixth Edition archive format file. This option is mutually exclusive with the -c and -H options.

Parameters

Item Description
Directory Specifies the directory.
Patterns Specifies one or more patterns (as described in the ksh command) to be used with the command. The default for the Patterns parameter is an * (asterisk), selecting all the files in the input.

Exit Status

0
The command completed successfully.
>0
An error occurred.

Examples

  1. To copy all the files in the current directory onto tape device /dev/rmt0, enter:
    find . | /usr/sysv/bin/cpio -oc >/dev/rmt0
    The -c option ensures that the file is made portable to other machines. Instead of find you can also use ls, cat, echo and so on to pipe a list of names to cpio. The output could also be redirected to a regular cpio file instead of a device.
  2. To extract an cpio archive file named "arfile" created by cpio command use the following:
    /usr/sysv/bin/cpio -icdI arfile 
    Here all the files are extracted from the cpio archive and the -d option ensures that the required directory paths are created as when required.
  3. A cpio archive file can also be extracted as follows:
    /usr/sysv/bin/cpio -icd < arfile 
    The -d option ensures that all the required directories are created under the current directory. The standard input can be used only if -I flag is not specified.
  4. To extract unconditionally all the files in "arfile" use the following:
    /usr/sysv/bin/cpio -icduI arfile 
  5. To skip any files which corrupted headers, cpio can be used as follows:
    /usr/sysv/bin/cpio -ickudI arfile 
  6. If the access time of the files archived needs to be reset when cpio is used to create an archive, use cpio in the following way:
    ls | /usr/sysv/bin/cpio -oca > arfile 
  7. To extract only the files matching the pattern "a*" from the archive "ar", use the following:
    cat ar | /usr/sysv/bin/cpio -ickud "a*"
    This command extracts all the files starting with letter "a".
  8. To display the list of files archived, use cpio in the following way:
    cat ar | /usr/sysv/bin/cpio -itv
    The verbose option (-v) ensures that the list given by -t option is listed in a very similar way as ls -l command.
  9. The cpio -p command can be used to copy a directory tree to a new path, as follows:
    find . -print | /usr/sysv/bin/cpio -pd /home/user1/newdir 
    The entire directory tree from current directory is copied to /home/user1/newdir. The -d option ensures that directories are created as necessary.
  10. To retain the modification time and access control list while copying the directory tree, use the cpio command as follows:
    find . -name "*.o" -print | /usr/sysv/bin/cpio -pdlmv /home/user1/newdir 
    In this example only the .o files under the directory tree are copied to /home/user1/newdir.
  11. To append a list of files to a cpio archive matching a particular pattern, invoke a command similar to the following:
    ls d* | /usr/sysv/bin/cpio -oAO /tmp/ar 
    In this example, all files starting with "d" in the current directory will be appended to the cpio archive.
  12. To extract only a list of files listed inside a regular file from an cpio archive, use the following command:
    cat ar | /usr/sysv/bin/cpio -i -E Efile 
    In this example, cpio extracts only those files that are listed in the regular file "Efile", provided the specified file name exists in the archive.
  13. To hard link all the files instead of copying them, invoke a command similar to the following:
    ls d* | /usr/sysv/bin/cpio -pdl /home/user2/newdir 
    In this example, the -l flag ensures all the file names starting with the character "d" are hard linked to the /home/user2/newdir, the directory specified. Hard linking across file systems is not allowed, thus the -l option cannot be used when the destination directory is in any other filesystem.

Files

Item Description
/usr/sysv/bin/cpio Contains the System V cpio command.