[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
Commands Reference, Volume 1

cpio Command

Purpose

Copies files into and out of archive storage and directories.

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

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 Pattern parameter 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.
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 li -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, and li command.

The cpio file format, rmt special file.

The Backup Overview for System Management in AIX Version 4.3 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 Version 4.3 System User's Guide: Operating System and Devices explains working with directories and path names.

The File Systems Overview for System Management in AIX Version 4.3 System Management Concepts: Operating System and Devices explains file system types, management, structure, and maintenance.

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


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]