Cp (Unix) 792859 213213581 2008-05-18T09:13:30Z 87.14.27.198 Added link to italian page {{Refimprove|date=July 2007}}{{context}} {{Nofootnotes|date=February 2008}} {{lowercase}} {{wikibookspar||Guide to UNIX commands: File system utilities#cp}} '''cp''' is the [[command (computing)|command]] entered in a [[Unix shell]] to copy a [[computer file|file]] from one place to another, possibly on a different [[filesystem]]. The original file remains unchanged, and the new file may have the same or a different name. This document describes the [[POSIX]] system command. [http://www.linuxmanpages.com/man1/cp.1.php cp] on a [[Linux]] system has additional options. ==Usage== To Copy a File to another File cp [ -f ] [ -H ] [ -i ] [ -p ][ -- ] SourceFile TargetFile To Copy a File to a Directory cp [ -f ] [ -H ] [ -i ] [ -p ] [ -r | -R ] [ -- ] SourceFile ... TargetDirectory To Copy a Directory to a Directory cp [ -f ] [ -H ] [ -i ] [ -p ] [ -- ] { -r | -R } SourceDirectory ... TargetDirectory ==Flags== '''-f''' (force) – specifies removal of the target file if it cannot be opened for write operations. The removal precedes any copying performed by the cp command. '''-P''' – makes the cp command copy symbolic links. The default is to follow symbolic links, that is, to copy files to which symbolic links point. '''-i''' (interactive) – prompts you with the name of a file to be overwritten. This occurs if the TargetDirectory or TargetFile parameter contains a file with the same name as a file 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''' (preserve) – duplicates the following characteristics of each SourceFile/SourceDirectory in the corresponding TargetFile and/or TargetDirectory: *The time of the '''last data modification''' and the '''time of the last access'''. *The '''user ID''' and '''group ID''' (only if it has permissions to do this) *The '''file permission bits''' and the [[Setuid|SUID]] and [[Setgid|SGID]] bits. '''-R''' (recursive) – copy directories (recursively copying all the contents) ==Examples== 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, the cp command creates it. If it does exist, the cp command replaces it with a copy of the prog.c file. To copy a file in your current directory into another directory, enter: cp jones /home/nick/clients This copies the jones file to /home/nick/clients/jones. 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 system 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. 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. To copy a directory, including all its files and subdirectories, to another directory, enter: cp -R /home/nick/clients /home/nick/customers This copies the clients directory, including all its files, subdirectories, and the files in those subdirectories, to the customers/clients directory. 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. 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. ==Related Unix commands== *[[cpio]] – copy an entire directory structure from one place to another *[[tar (Unix)|tar]] – create an archive of files *[[link (Unix)|link]] – system call to create a link to a file or directory *[[ln (Unix)|ln]] – create a link to a file or directory *[[mv]] – move a file or directory *[[Rm (Unix)|rm]] – remove a file or directory *[[unlink (Unix)|unlink]] – system call to remove a file or directory *[[chmod]] – change permissions on a file or directory *[[chown]] – change ownership on a file or directory *[[chgrp]] – change group on a file or directory *[[uucp]] – unix to unix copy *[[Secure copy|scp]] – secure copy over SSH ==See also== *[[List of Unix programs]] ==External links== [[Linux]] system [http://www.linuxmanpages.com/man1/cp.1.php cp] has additional options. [[Category:Unix software]][[Category:Standard Unix programs]] [[Category:File copy utility]] {{Unix commands}} [[ar:Cp (يونكس)]] [[cs:Cp]] [[de:Cp (Unix)]] [[es:Cp]] [[eu:Cp]] [[fr:Cp (Unix)]] [[it:Cp (Unix)]] [[nl:Cp]] [[pl:Cp (Unix)]] [[pt:Cp (Unix)]] [[ru:Cp]] [[fi:Unix-työkaluohjelmat]]