Linux/Unix – Copying Files and Directories…
Copying Files and Directories
Use cp (“copy”) to copy files. It takes two arguments: the source file, which is the existing file to copy, and the target file, which is the file name for the new copy. cp then makes an identical copy of the source file, giving it the specified target name. If a file with the target name already exists, cp overwrites it. It does not alter the source file. To copy the file `my−copy’ to the file `neighbor−copy’, type:
$ cp my−copy neighbor−copy
This command creates a new file called `neighbor−copy’ that is identical to `my−copy’ in every respect except for its name, owner, group, and timestamp−−the new file has a timestamp that shows the time when it was copied. The file `my−copy’ is not altered.
Use the `−p’ (“preserve”) option to preserve all attributes of the original file, including its timestamp, owner, group, and permissions. To copy the file `my−copy’ to the file `neighbor−copy’, preserving all of the attributes of the source file in the target file, type:
$ cp −p my−copy neighbor−copy
This command copies the file `my−copy’ to a new file called `neighbor−copy’ that is identical to `my−copy’ in every respect except for its name.
To copy a directory along with the files and subdirectories it contains, use the −R option−−it makes a recursive copy of the specified directory and its entire contents.
To copy the directory `public_html’, and all of its files and subdirectories, to a new directory
called `private_html’, type:
$ cp −R public_html private_html
The `−R’ option does not copy files that are symbolic links and it does not retain all original permissions. To recursively copy a directory including links, and retain all of its permissions, use the `−a’ (“archive”) option. This is useful for making a backup copy of a large directory tree.
To make an archive copy of the directory tree `public_html’ to the directory `private_html’, type:
$ cp −a public_html private_html