cp or copy Command Purpose Copies files. Syntax To Copy a File to another File {cp | copy} [ -h ] [ -i ] [ -p ] [ -r | -R ] [ -- ] SourceFile TargetFile To Copy Files to a File or Directory { cp | copy } [ -h ] [ -i ] [ -p ] [ -r | -R ] [ -- ] SourceFile ... TargetDirectory To Copy a Directory to a Directory { cp | copy } [ -h ] [ -i ] [ -p ] [ -- ] { -r | -R } SourceDirectory ... TargetDirectory Description The cp command creates a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parame- ters into the file or directory specified by the TargetFile or TargetDirectory parameters. If the file specified as the TargetFile exists, the copy writes over the original contents of the file. If you are copying more than one SourceFile, the tar- get must be a directory. To place a copy of the SourceFile into a directory, specify a path to an existing directory for the TargetDirectory parameter. Files maintain their respective names when copied to a directory unless you specify a new file name at the end of the path. The cp command also copies entire directories into other directories if you specify the -r or -R flags. You can also copy special device files. The preferred option for accomplishing this is the -R flag. Specifying -R causes the spe- cial files to be re-created under the new path name. Specifying the -r flag causes the cp command to attempt to copy the special files to regular files. Flags -h Forces the command to follow symbolic links. This flag is useful with the -R option, which does not follow symbolic links by default. -i Prompts you with the names of files that will be overwritten. This occurs if the TargetDirectory or TargetFile parameter con- tains files with the same name as files specified in the SourceFile or SourceDirectory parameter. If you enter y or the locale's equivalent of y, the cp command continues. Any other answer prevents the cp command from overwriting the file. -p Preserves the modification times and modes of the SourceFile and SourceDirectory parameters for the copy. Any access control lists (ACLs) associated with the SourceFile are also preserved. See "Access Control Overview" in AIX Version 3.2 System Manage- ment Guide: Operating System and Devices to learn more about ACLs. -r Copies file hierarchies under the file or directory specified by the SourceFile or SourceDirectory parameter (recursive copy). The -r flag processes special files in the same manner as regular files. Symbolic links are always followed. -R Copies file hierarchies under the regular files and direc- tories from the directory specified by the SourceFile or SourceDirectory parameter to the directory specified by the TargetDirectory parameter. Special file types, such as symbolic links and block and character devices, are recreated instead of being copied. (The -R flag is preferred to the -r flag.) -- Indicates that parameters following the -- (dash, dash) flag are to be interpreted as file names. This null flag allows the specification of file names that start with a - (minus sign). Exit Status This command returns the following exit values: 0 All files were copied successfully. >0 An error occurred. Examples 1. To make a copy of a file in the current directory, enter: cp prog.c prog.bak This copies prog.c to prog.bak. If the prog.bak file does not already exist, then the cp command creates it. If it does exist, then the cp command replaces it with a copy of the prog.c file. 2. To copy a file in your current directory into another directo- ry, enter: cp jones /home/nick/clients This copies the jones file to /home/nick/clients/jones. 3. To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter: cp -p smith smith.jr This copies the smith file to the smith.jr file. Instead of creating the file with the current date and time stamp, the sys- tem gives the smith.jr file the same date and time as the smith file. The smith.jr file also inherits the smith file's access control protection. 4. To copy all the files in a directory to a new directory, enter: cp /home/janet/clients/* /home/nick/customers This copies only the files in the clients directory to the customers directory. 5. To copy a directory, including all its files and subdirec- tories, to another directory, enter: cp -R /home/nick/clients /home/nick/customers This copies the clients directory, including all its files, sub- directories, and the files in those subdirectories, to the customers/clients directory. To copy the contents of the clients directory directly into the customers directory without creating the additional subdirectory level, see example 4. 6. To copy a specific set of files to another directory, enter: cp jones lewis smith /home/nick/clients This copies the jones, lewis, and smith files in your current working directory to the /home/nick/clients directory. 7. To use pattern-matching characters to copy files, enter: cp programs/*.c . This copies the files in the programs directory that end with .c to the current directory, signified by the single . (dot). You must type a space between the c and the final dot. Implementation Specifics These commands are part of Base Operating System (BOS) Runtime. Files /usr/bin/cp Contains the cp command. /usr/bin/copy Contains the copy command. Related Information File Systems and Directories Overview in AIX Version 3.2 System User's Guide: Operating System and Devices National Language Support Overview for System Management in AIX Version 3.2 System Management Guide: Operating System and Dev- ices. The cpio command, link command, ln command, mv command, un- link command.