Topics Summary

 

Definition and purpose:

The primary usage of cp command is to “copy” files or directory from one location to another.

On Linux and UNIX flavor machines cp command is one of the most used commands in the day to day activities.

Syntax:

cp source-file-name target-file-name

 

Example:

copying the file1 information to file2 within the same directory.

cp file1 file2

 

More about cp command and its options, please checks its manuals and help content.

Syntax:

  • man cp
  • cp –help
  • info cp

 

Practical usages:

  • Using the backup of a file or directory during installation and upgradations.
  • Making a copy of a files content to another file.
  • Moving the log files from one location to another location.

 

Example-1:

Copy content of a file to another file in the same directory:

This is the most practical use case of cp command.

Sometimes, we might want to do some changes to any application configuration file, then it’s always a best practice to take a copy of that file content. This will act as a backup file.

Syntax:

Cp Source-file-name target-file-name

 

In the below given example, we want to copy “abc_lab1.txt” file content to “abc_lab1_copy.txt” .

[rreddy@abclearnabclearn_dir1]$cp abc_lab1.txt abc_lab1_copy.txt

[rreddy@abclearnabclearn_dir1]$ cat abc_lab1_copy.txt

this line should replace the existing one

 

Note:

  • Here, an abc_lab1_copy.txt file is created newly. If this file already exists on the directory, then it will be overwritten by default.

 

Example-2:

Copy the file content but don’t override the existing one:

We can use cp command in combination with “-n” option.

This file will copy the source file to destination file without disturbing or overwriting the original content of destination file.

Therefore the final content of the destination file is source file content plus destination file content.

Syntax:

cp -n source-file target-file

 

Let’s observe a practical example.

There is a  file and it is having all ftp user details. We have a copy of ftp user details of another system as well. Now, my task is to combine all the users details in the single file.

In this scenario, we will use this option "-n".

 

Example Output:

[rreddy@abclearnabclearn_dir1]$cp -n abc_ftp1.txt abc_ftp2.txt

cp: overwrite ‘abc_ftp2.txt’? y

[rreddy@abclearnabclearn_dir1]$cat abc_ftp2.txt

 

Example-3:

Copy the file from one directory to another directory:

Syntax:

cp source-file-name Target-directory

 

Let us take a practical case,

As part of version upgrade for an application software, “app1_config.txt” and other configuration files in installation directory are going to be updated to new format.

So we want to take a copy of these files,  in another directory “/opt/bin/App1_Config_backup/” for any roll back activity.

[rreddy@abclearnApp1_Config]$ cpapp1_config.txt /opt/bin/App1_Config_backup/

 

Changing to target directory,

[rreddy@abclearnApp1_Config]$ cd /opt/bin/App1_Config_backup/

[rreddy@abclearnApp1_Config]$ ls

app1_config.txt

 

Note-1:

Here, App1_Config_backupdirectory should be created already. If it doesn’t exist, then will throw an error.

[rreddy@abclearnApp1_Config]$ cp app1_config.txt /home/rreddy/abclearn_dir2/

 

Note-2:

If we miss the last “/” shown in the red mark, instead of copying the content to dir2, there will be a new file created with the name as abclearn_dir2 under rreddy directory.

Also note that, to copy a file or create a new file user should be having proper credentials to that parent directory.

  

Example-4:

Copy entire directory to another directory:

Syntax:

cp -r Source-directory-name targer-directory-name

 

We can use the cp command with option “-r” recursive and copy entire directories and its sub-directories to another directory.

[rreddy@abclearn~]$cp -r abclearn_dir1/ /tmp/

[rreddy@abclearn~]$ cd /tmp

[rreddy@abclearntmp]$ ls

 

Recursive copy means, first sub directories and their files are copied and in the last parent directory is copied.

 

Example-5:

Forcing the copy operation:

While copying the file from one location to the another location it will ask for the confirmation by using the cp command with '-f' option won’t prompt the confirmation.

Syntax:

cp -f source_file Target _directory

 

[rreddy@abclearnabclearn_dir1]$cp  -f abc_lab1.txt  /tmp

[rreddy@abclearn~]$ cd /tmp

[rreddy@abclearntmp]$ ls

 

Example-6:

Prompting when overwriting a file:
Whenever we are copying the content of one file to the another file there is a chance of losing the important data in the destination file.

By using the cp command with "-i" option we get confirmation alert while overwriting the destination file with the source file.

Syntax:

cp -i source_file Destination_file

 

[rreddy@abclearnabclearn_dir1]$cp -i abc_lab1.txt abc_lab1_copy.txt

cp: overwrite `abc_lab1_copy.txt'? y

[rreddy@abclearnabclearn_dir1]$cat abc_lab1_copy.txt

 

Example -7:

Copying the file to the directory without changing the attributes of the file:

While copying the file from one location to the another location the attributes like timestamp of the file in the target location will change.

Without disturbing the attributes of the file we can copy the file from source to target using "-p" option.

Syntax:

cp  -p source_file Destination_directory

 

Follow the following scenario to understand more.

[ rreddy@abclearn ~]# cat abc_lab1.txt

 

[rreddy@abclearn~]# ls -l abc_lab1.txt

-rw-r--r--. 1 rreddy rreddy 0 Mar 21 19:32 abc_lab1.txt

 

[rreddy@abclearn~]# cp abc_lab1.txt /tmp

[rreddy@abclearn~]# cd /tmp

[rreddy@abclearn~]# ls -l abc_lab1.txt

-rw-r--r--. 1 rreddy rreddy 0 Jul 26 15:21 abc_lab1.txt

 

More to practice:

-u, --update copy only when the SOURCE file is newer

then the destination file or when the

destination file is missing

-s, --symbolic-link make symbolic links instead of copying

-H follow command-line symbolic links in SOURCE

-l, --link hard link files instead of copying

-L, --dereference always follow symbolic links in SOURCE

-a, --archive same as -dR --preserve=all

--attributes-only don't copy the file data, just the attributes

--backup[=CONTROL] make a backup of each existing destination file

-b like --backup but does not accept an argument