Copying files and directories in linux / Unix

Copying files and directories in linux / Unix

 

Copying files and directories in linux

 

In this post i am going to explain, How does files & directories are copied in linux using cp command.

If you are going to copy a single file to a folder

Syntax:
#cp <Options> file destination

If you want to copy multiple files to a folder

Syntax
#cp <Options> file1 file2 destination

Few interesting things to be known about cp command:

1. If the destination is a directory, the copy is placed there.
2. If the destination is a file, the copy overwrites the destination.
3. If the destination does not exist, the copy is renamed.

Most frequently used options with explanation:

-R : Copy files recursively.
-p : Preserver mode, ownership and timestamps.
-a – R+p : It do both recursive, mode, ownership and timestamps.
-i : Interactive, It prompts before over writing a file.
-f : Fore copy a file

Examples of cp command

To copy single file
#cp /etc/passwd /root/Desktop/

To copy multiple file
#cp /etc/passwd /etc/shadow /root/Desktop/

To copy a entire folder
#cp -R /tmp /root/Desktop




Leave a Reply

Your email address will not be published. Required fields are marked *