Copy command in Linux – Copy files and directories

Copy command in Linux - Copy files and directories

Copy command in Linux - Copy files and directories Intro

Copy command in Linux (CP)  is used to copy files and directories in Linux.This tutorial will guide you through how to use 'cp' command  to copy files and directories. CP command is used to copy one file to another file, copy files to another directory, copy one directory to another directory. We will also learn about various options we can use with the cp. This tutorial can be useful both for beginners and advance users.

 

 1. Copy file in Linux

To copy any files or directories, see the syntax given below. 'Source' is the path of the file from where it would be copied and the 'destination' is the place where it will be copied.

# cp  <source>  <destination>                               
# cp /directory1/file1.txt /directory2/file2.txt

The above command will copy  'file1.txt'  to the destination 'directory2'  with a new name as 'file2.txt'

2. Copy multiple files in Linux

The below copy command example will illustrate how to copy multiple files in Linux.

# cp  file1.txt file2.txt file3.txt /directory2/

In the above we are copying multiple files to the destination folder 'directory2'.  Similarly, you can copy multiple directories to another directory.

 

3. Copy files recusively in Linux

The below command will copy files recursively present inside directory and its sub-directories to the destination folder. See the below example.

# cp -R /directory1/directory2/ /directoryR/

The above command will copy the content of  'directory1'  that is its sub directory  'directory2'  and all the files inside it to the 'directoryR'.

 

4. Copy files preserve permissions

If you want you can preserver permissions of files and directories while copying. This is important when you need to copy number of files at a time. Because you wouldn't want to waste your time resetting the attributes again. lets see by an example. You can check the file permissions by the below command.

# ls -l file1.txt

Now, to copy file with the same permission, use the (cp) copy command as given below.

# cp -p file1.txt /directory1/

Adding 'p' option with 'cp' command allows you to keep the same permissions (read, write or execute) and ownership of the file.

 

5. Linux copy files preserve links

Some files have symbolic links, and we do not want these symbolic links to be removed when copying files.
What is symbolic links?  Symbolic link is a name of any file which contains references of any other files or directories as absolute or relative path. To see if the file has any symbolic links, use the below command.

# ls -l
Copy command in Linux - Copy files and directories
Copy command in Linux - Showing symbolic link
# cp -d file1.txt /directory2/

Hence, While copying you may find that there are hard links or soft links present in the file. Therefore, Its a good idea to keep the links preserve. The option '-d' is used to preserve permissions.

 

6. Backup when copying

While copying if there is already a file exist in the directory with the same name, then the  '--backup'  option allows you to take the backup of the file before over writing it.

# cp  --backup file3.txt /directory2/
cp: overwrite '/directory2/file3.txt'? y

Let's see if the backup is created or not with the 'ls' command.

# ls /directory2/
file3.txt   file3.txt~

In the above example 'file3.txt~'  is a backup of  'file3.txt' 

 

7. Copy without overwrite

If you do not want to overwrite the file which is already exists in the destination, use the option 'n' with (cp) copy command. The option 'n' avoids overwriting of files.

# cp -n file1.txt /directory2/

 

8. Just create the hard link or soft link to a file

Before I go deep into the hard link and soft link example, let me first explain you what is hard link and soft link.
What is Soft link? Have you ever seen any short cut of any application in Windows OS?  Yes, that is also a soft link (Symbolic link). In Linux when you create soft link, it creates a file which contains the pointer to the location of the destination file. A new inode is created in soft link. The option 's' is used to create soft link of file instead of copying.

# cp -s file1.txt file2.txt

What is Hard link? Hard link is a bit different from the soft link. When you create any hard link only an entry into the directory structure is created. There is no new inode creation happens in hard link. The option 'l' is used to create hard link of file instead of copying. See the below example.

# cp -l file1.txt /directory1

 

9. Prompt before overwriting

The '-i' option is interactive. It is used to take the confirmation from the users before overwriting any file.

# cp -i file1.txt /directory2/
cp: overwrite ‘/directory2/file1.txt’? y


10. Force copy command to overwrite

To force 'cp' copy command in Linux to overwrite, you need to use 'r' and 'f' with cp.

# cp -rf file4.txt /directory/

If the above command doesn't work and still prompt before overwriting, that means cp is lready being aliased to 'cp -i'.
To check alias type command 'alias'.

# alias
alias cp='cp -i' 
alias egrep='egrep --color=auto' 
alias fgrep='fgrep --color=auto

You can retry the cp command as given below.

#/bin/cp -rf file4.txt /directory/

Or you can use the command in the given way. The option 'u' is used to update the file.

# cp -ru file4.txt /directory/


11. CP preserve SELinux

Sometimes you may want to preserve SELinux context while copying files. SELinux stands for Security Enhanced Linux and it is very important part of security in Linux these days. The below command is used for preserving SELinux context while copying files.

# cp --preserve=context  /source-file  /destination-directory/

To display SELinux context of services file inside the '/etc' directory, use the below command.

# ls -Z services 
-rw-r--r--. root root system_u:object_r:etc_t:s0 services


12. Copy with verbose

To see what is being done while copying or backing up your file, use the 'verbose' option with copy command in Linux.

# cp --verbose /source  /destination/


13. Man cp

To get more details about copy command in Linux, please see the man page of 'cp' command.

# man cp

 


This is it with the tutorial Copy command in Linux - Copy files and directories. If you like this tutorial, please don't forget to share.

Download Our Free eBook now

Linux and UNIX Shell scripting ebook

Be the first to comment

Leave a Reply

Your email address will not be published.


*


shares