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

Advertisements

About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 15, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: