Categories: Unix Tutorials

How do you use Unix CP Command | Unix CP Command with Examples

Unix CP Command :

I have given the different useful unix commands with its real examples in my previous articles. I have explained each and every unix command thoroughly.In this article i would like to explain one another very important command which is Unix CP Command. I will try to explain Unix CP Command with Real Examples so that everyone will get idea about it.

CP Command Syntax :

The CP command is most important command in unix which is used to copy the file from one location to another location. In windows systems we have direct GUI to copy the files and folders. But in unix there is no such GUI provided. Unix has the command named ‘CP’ which will copy the files from source and paste it to destination or target.

The Unix CP Command is used to copy the files,folders from Source to Target

Syntax of Unix CP Command :

cp SOURCE Target
cp SOURCE DIRECTORY or FOLDER
cp file1 file2
cp file1 new-file2
cp [options] file1 new-file2

There are multiple syntax of unix file where just the options are changed accordingly.The first syntax gives the Source and target.The second syntax shows that the target is any directory name or folder name.User can copy the one file and give the another name to that file. So third syntax makes the copy of file1 as file2.User can paste the content of file in to newly created file also.Fourth syntax explains about the same. The fifth syntax is CP command is with using multiple options of linux.

CP Command Examples and Industry usages :

In this section i will try to different CP Commands with real industry examples. If we are using windows system we are simply use copy and paste command. In linux system we need to use CP command to copy and paste the file or folder.In this section i will explain one option of CP command and its example so that user will get idea about how those options are used in industry.

Scenario 1: Copy the File from current directory and paste it to another directory

This is most common scenario where user wants to copy the file from current directory and paste the file to another directory. User used to use this scenario where user needs to copy the file from source location and paste it to the target.

Lets say if you want to copy the file Amit.txt and paste it to tmp directory,

CP Amit.txt /tmp

The above command will copy the file from source and paste it to target which is /tmp.

Scenario 2: Copy the same file and paste it in to same directory

There are multiple scenarios where user wants the copy of the file before making the changes in to the file. User needs to take back up of same file and need to place it in same folder. In windows it is simple to make the copy;whereas in unix user needs to use the CP command to perform that operation.

CP Amit.txt Amit_copy.txt

The above CP command will make the copy of Amit.txt in same working directory.

Scenario 3: Copy the same file in other location

There are some scenarios where user wants to copy the file on which he is working needs to be saved or pasted in other folder. This scenario is possible in unix using following command:

CP Amit/Files/Amit.txt Amit/Files/Backup/Amit.txt

While working with this scenario user needs to give correct source path as well as destination path.The copy of file will be stored in the Backup folder in above example.These scenario is used in many real world examples to take the backup of file.

Scenario 4 : Copy All files from one directory and paste it using Unix CP Command

This is also most commonly used scenario. We need to copy multiple files from one folder and paste it in to another folder.

CP Amit/*.* Amit/Backup/

The above command will copy all the files from folder named Amit and paste it to the backup folder. This command is used to make the copy of the project. Here *.* means all files in folder named ‘Amit’.

Scenario 5: Copy Specific type of set of files and paste it.

This scenario is also most important scenario. In most of the projects user needs a specific set of file and need to process that set of files.In this scenario we need to use the asterisk(*). If we want to copy all jpg files from one directory and paste it on another directory then we need to use following command:

CP ~/Amit/*.jpg ~/Backup

The above command will copy all the jpeg files from folder named Amit and paste it to the Backup folder.

Scenario 6: Copy the file recursively using Unix CP Command

You need to pass the -r or -R option (i.e., recursive option). It allows directories including all of their contents to be copied:

cp -r dir1 dir2

To see copy progress pass -v option to cp command:

cp -v file1 file2

Sample outputs:

`file1' -> `file2'

The -v cause cp to be verbose, showing files as they are copied. You can pass the -v option along with other options:

cp -v -r file1 /tmp
cp -v -r dir1 dir2

Scenario 7 : If you want to overwrite the files

If user wants to overwrite the files in specific manner then user needs to use -i option in linux systems.After using the -i option the system will ask you for file overwriting. If you want to overwrite the file press y or if you dont want to overwrite the file press ‘n’.

cp -i /etc/amit.conf /tmp

Sample outputs:

cp: overwrite `/tmp/amit.conf’? y

ou need to enter the letter y (both lower case or upper case with work) in response to the prompt causes the command to continue. Any other answer prevents the command from overwriting the file called /tmp/amit.conf. Some user put the following alias in ksh startup file called $HOME/.kshrc:
$ vi ~/.kshrc
Append the following alias:

## prvent overwriting by default for cp command
alias cp='cp -i'

Save and close the file. Source the shell startup file to have the changes take immediate effect, enter:

. .kshrc

Scenario 7 : Taking the backup of the file before copying it.

It is always needed to take a backup before copying the files. -b option of linux is useful to take a back up of the file before copying it.

$ls 

Amit.txt Pradnya.txt 

$cp -b Amit.txt Pradnya.txt 

$ls

 Amit.txt  Pradnya.txt  Pradnya.txt~

User can give .bak extension to backup file. To specify the name for backup file user needs to be used -S option.

$ls

Amit.txt Pradnya.txt

$cp -S Amit.txt Pradnya.txt

$ls

Amit.txt  Pradnya.txt  Pradnya.txt.bkp

Scenario 8 : Creating hard link instead of using Unix CP Command

To create a hard link instead of copying with the cp command pass the -loption. Instead of copying the file a new file will be created that is a hard link to the data on disk. Here’s a primer on hard and symbolic or soft links.

$ls  Amit.txt
 $cat Amit.txt
Amit text $cp -l Amit.txt Pradnya.txt
 $echo 'Pradnya text' > Pradnya.txt
 $cat Amit.txt
 Pradnya text

The above statemet has directly created hardlink instead of using the copy.

Scenario 9 : Preserving File Attributes using Unix CP Command

Sometimes there are scenarios where user needs to preserve the attributes of file.Attributes means the permissions,User Group and ownership.To preserve file attributes (permissions, group and user owernship) pass the --preserve option along with the attributes to be preserved. By default mode, ownership and timestamps will be preserved.

ls -la
-rw------- 1 Amit users 0 Aug 13 09:14 Amit.txt
cp --preserve Amit.txt Pradnya.txt
-rw-------  1 Amit users     0 Aug 13 09:14 Amit.txt
-rw-------  1 Amit users     0 Aug 13 09:14 Pradnya.txt

Scenario 10 : Check for copied files using Unix CP Command

User can be able to check the file name which are copied.To show files that are being copied pass the -v option to the cp. This prints the files and folders that are being copied to standard output. In the following example the directory foo is copied as Pradnya along with the contents.

cp -R -v Amu Pradnya

'Amu' -> 'Pradnya' 

'Amit/Amu.txt' -> 'Pradnya/Amit.txt'

 'Amit/Pradnya.txt' -> 'Pradnya/Pradnya.txt'

These are above some most important scenarios while using the CP command of unix.If You want further information related to Unix CP command kindly comment it in to comment section.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Share
Published by
Amit S

Recent Posts

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

2 days ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

5 days ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

5 days ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

5 days ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

5 days ago

What are Roles and Responsibilities of L1 Support Engineer with real industry examples?

In this series we are starting with Roles and responsibilities of L1 support engineer .…

5 days ago