[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Commands Reference, Volume 1

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 ] [  v ] [  B | C Value ] <FileName >Output

cpio -ib ] [  c ] [  d ] [  f ] [  m ] [  M ] [  r ] [  s ] [  t ] [  u ] [  v ] [  S ] [  6 ] [  B | C Value ] [  Pattern... ] <Input

cpio -pa ] [  d ] [  l ] [  m ] [  M ] [  u ] [  v ] Directory <FileName

Description

The cpio command copies files into and out from a cpio archive. The cpio archive may span multiple volumes. The -i, -o, and -p flags select the action to be performed.

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 2 Gig 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.

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.

Parameters

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.

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 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.

Exit Status

This command returns the following exit values:

0 Successful completion.
>0 An error occurred.

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

Files

/usr/bin/cpio Contains the cpio command.

Related Information

The find command, ksh command, ln command, ls command.

The cpio file format, rmt special file.

The Backup Overview for System Management in AIX 5L Version 5.2 System Management Concepts: Operating System and Devices provides information on different methods of backing up, restoring process, different types of backup media, and guidelines for backup policies.

The Directory Overview in AIX 5L Version 5.2 System User's Guide: Operating System and Devices explains working with directories and path names.

The File Systems in AIX 5L Version 5.2 System Management Concepts: Operating System and Devices explains file system types, management, structure, and maintenance.

The Files Overview in AIX 5L Version 5.2 System User's Guide: Operating System and Devices provides information on working with files.

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)

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. 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

-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 Copy 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 Process a UNIX System Sixth Edition archive format file. This option is mutually exclusive with the -c and -H options.

Parameters

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

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

Related Information

The /usr/bin/cpio command, tar command, dd command, pax command.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]