$title = "Copying files and directories - cp"; $area = "Unix Support"; $metadata = "unix, commands, files, directory, copy, cp"; $pfloc = ""; require '/usr/local/wwwdocs/ucs/fragments/header.phtml'; require '/usr/local/wwwdocs/ucs/fragments/header-bc.phtml'; ?>
The cp command is used to copy files or directories. The general form of the command is
cp [-option] source destination
scp is used to copy files between hosts.
To copy a directory use the command:
cp -r directory1 directory2
This copies directory1 and everything that it contains to directory2. The directory is created if it does not exist. If directory2 does exist then directory1 is created as a subdirectory within it.
To create a new copy of a directory:
cp -r draft presentation
This copies all the files and subdirectories in the directory draft into a new directory presentation.
You try to create a copy of a directory using the name of directory that already exists:
cp -r planA projects
If the directory projects already exists, then a copy of the directory planA will be created in this directory.
cp [-option] source destination
The source is the name of the file to be copied; the destination is the name of the file in which the copy is to be placed.
Take care what name you give to the file copy. If you choose the name of a file that already exists it will be overwritten by the new copy! To avoid this always use the cp command together with its -i option. This will prompt you for confirmation whenever the copy would overwrite an existing file.
To copy a single file in the current directory:
cp notes sect3.txt
This makes a copy of the file notes in the file sect3.txt.
To copy a file using the -i option:
cp -i notes part2.txt
This makes a copy of the file notes in the file part2.txt. If this file exists, the message
part2.txt: File exists
is displayed. The file is not copied.
You can use special "wildcard" characters whenever you want to copy several files that have similar filenames.
Instead of entering the cp command followed by several filenames you can use a single filename that contains one or more wildcards.
To copy files that match on several characters:
cp *.txt chapt1
This copies all the files in the current working directory with the extension ".txt" to the sub-directory chapt1.
To copy files that match on a single character:
cp sect?b partA
This copies any file with the name sect[1-9]b to the sub-directory partA.
To copy all the files within a specified range:
cp sect[1-4]c partB
This copies files sect1c, 2c, 3c and sect4c to the sub-directory partB.
To copy a file to another directory from your current directory give name of the source file followed by the pathname to the destination file.
cp source path_to_destination
For the destination file to have the same name as the source file use:
cp source path_to_destination_directory
To copy a file from your current working directory to a subdirectory:
cp fig2 part2/figure2
This copies the file fig2 from your current working directory to the file figure2 in the subdirectory part2.
To copy a file to the parent directory:
cp mail.txt ..
This copies the file mail.txt to the directory immediately above the current working directory. The .. (dot dot) is shorthand for the parent directory.
To copy a file from a subdirectory to a directory elsewhere in the file system:
cp docs/vi.Z /tmp
This copies the file vi.Z in the subdirectory docs to the directory /tmp.
To copy a file to your home directory:
cp /usr/local/doc/ue.txt $HOME
This copies the file ue.txt to your home directory. $HOME is the environment variable that contains the value for your home directory.
To copy a file from another directory to your current directory give the pathname to the source file followed by the name of the destination file.
cp path_to_source_file destination
For the destination file to have the same name as the source file use:
cp path_to_source_file .
The . (dot) is shorthand for the current working directory.
To copy a file from a subdirectory to the current working directory:
cp notes/note3 sect3.txt
This copies the file note3 from the subdirectory notes to the file sect3 in the current working directory. A relative pathname notes/note3 is used to define the source file.
To copy a file from another directory to the current working directory, preserving the file name:
cp /usr/lib/more.help .
This creates a copy of the file more.help in the current working directory. A full pathname /usr/lib/more.help is used to define the source file.
To copy a file from another user's directory using a relative pathname:
cp ~helper/tmp/for_john tmp/comments
This copies the file for_john from the directory tmp in which is in the home directory of the user helper, to a directory with the same name in the user's home directory. The name of this new file is comments.
Notice that a relative pathname ~helper/tmp/for_john is used to define the source file.
To copy a file belonging to another user you must:
To copy a file from another user's home directory:
cp ~helper/file1 file2
This copies file1 from the home directory of user helper to file2 in your current working directory.
To copy a file from a subdirectory in the user's home directory:
cp ~helper/book/part2/file1 file2
This copies file1 from the subdirectory book/part2/ in the home directory of user helper to file2 in your current working directory.
cp: file pathname: Permission denied
Either the access permissions for this file do not permit you to copy it or the access permissions for the destination directory do not allow you to copy files into it.
cp: file_pathname: Is a directory (not copied).
You have tried to copy a directory.
cp: file pathname: No such file or directory
The file (or directory) that you have tried to copy does not exist.
Usage: cp [-ip] f1 f2; or: cp [-ipr] f1 ... fn d2
You have forgotten to specify the destination file (or directory).
require '/usr/local/wwwdocs/ucs/fragments/footer.phtml'; ?>