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.
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.
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.
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.
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.
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.
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’.
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.
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 |
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
To create a hard link instead of copying with the cp
command pass the -l
option. 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.
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
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.
In my previous article I have given details about application support engineer day to day…
In my previous articles I have given the roles and responsibilities of L1,L2 and L3…
In my previous articles i have given the hierarchy of production support in real company…
In this article i would like to provide information about production support organization structure or…
In my previous article I have given roles for L1 and L2 support engineer with…
I have started this new series of how to become application support engineer. This article…