Saturday, 19 December 2015

10 Practical Uses Of Copy(cp) Command In Unix/Linux

In This article we will see the practice uses of copy command which we use on regular basis in our day to day activity work. Here we have a source directory /home/baba which has two text files and a directory. We will perform all the operation in /home/baba source path only.

$ ls -ltr
-rw-r--r--  1 baba None  0 Dec 19 20:23 abc.txt
-rw-r--r--  1 baba None  0 Dec 19 20:24 pqr.txt
drwxr-xr-x+ 1 baba None 0 Dec 19 20:28 Tgt

1. Copy file abc.txt from /home/baba to /home/baba/Tgt folder
$ cp /home/baba/abc.txt /home/baba/Tgt
  
Output: Will copy the file abc.txt at directory /home/baba/Tgt
$ ls -ltr /home/baba/Tgt
-rw-r--r--  1 baba None  0 Dec 19 20:30 abc.txt

Note: During the copy operation file time stamp attributes get changed. You can check the same by using stat command.
  
If yoy are in directory /home/baba, then below will work.
$ cp abc.txt /home/baba/Tgt

2. Copy multiple files from source to destination.
$ cp abc.txt pqr.txt /home/baba/Tgt

Output:
$ ls -ltr /home/baba/Tgt
-rw-r--r-- 1 baba None 0 Dec 19 20:44 abc.txt
-rw-r--r-- 1 baba None 0 Dec 19 20:44 pqr.txt

If you try to copy a file which is not available in source then cp will raise an error:
cp: cannot stat ‘file’: No such file or directory

3. Copy all the .txt files from source to destination
$ cp *.txt /home/baba/Tgt

Output: Will copy all the .txt files in destination directory /home/baba/Tgt
$ ls -ltr /home/baba/Tgt
-rw-r--r-- 1 baba None 0 Dec 19 20:45 abc.txt
-rw-r--r-- 1 baba None 0 Dec 19 20:45 pqr.txt

4. Copy in Verbose Mode

It is always good to see what is going on, what cp is doing. We can see it using -v switch 

$ cp -v *.txt /home/baba/Tgt

Output:
‘abc.txt’ -> ‘/home/baba/Tgt/abc.txt’
‘pqr.txt’ -> ‘/home/baba/Tgt/pqr.txt’

5. How to Copy complete directory to the destination.
Let suppose we want to copy all the files from /home/baba to /home/Destination

$ ls -ltr  /home/baba
-rw-r--r--  1 baba None 0 Dec 19 20:23 abc.txt
-rw-r--r--  1 baba None 0 Dec 19 20:35 pqr.txt
drwxr-xr-x+ 1 baba None 0 Dec 19 20:44 Tgt

$ cp -R * /home/Destination/

Output: Complete directory has been copied to the destination path.

$ ls -ltr /home/Destination
-rw-r--r--  1 baba None 0 Dec 19 20:52 abc.txt
drwxr-xr-x+ 1 baba None 0 Dec 19 20:52 Imp
-rw-r--r--  1 baba None 0 Dec 19 20:52 pqr.txt

6. Validate before overwriting an existing file.
We have a file abc.txt in source directory
-rw-------  1 baba None  0 Dec 20 20:23 abc.txt


And same file is exist in destination directory.
-rw-------  1 baba None  0 Dec 20 20:50 abc.txt


Now you want to copy the file from source to destination but wants shell ask you for your input before overwriting the file. below will be the solution.

$ cp -i abc.txt /home/baba/Tgtcp: overwrite '/home/baba/IMP/abc.txt'? y

If you give [y] it will overwrite the file or if you give [n] it will not copy the file at all.

Output:
ls -ltr /home/baba/Tgt
-rw-------  1 baba None  0 Dec 20 20:55 abc.txt

As mentioned in earlier examples, during the copy operation time stamp of file gets changed.

7. Preserve attributes of file or directory while copying

$ ls -ltr /home/baba/abc.txt
-rw-------  1 baba None  0 Dec 20 20:23 abc.txt


$ cp abc.txt /home/baba/Tgt

$ ls -ltr /home/baba/abc.txt
-rw-------  1 baba None  0 Dec 20 21:10 abc.txt


If you see nothing has changed in the destination file properties except time stamp of the file.let say, we have updated the permission in source file :

$ chmod 777 /home/baba/abc.txt
-rwxrwxrwx  1 baba None  0 Dec 20 20:23 abc.txt

Now lets again perform the copy operation:

$ cp /home/baba/abc.txt /home/baba/Tgt
$ ls -ltr /home/baba/Tgt /abc.txt
-rw-------  1 baba None  0 Dec 20 21:20 abc.txt


The permission of destination is not same as of source, now if you want to preserve the properties of file we should use -p switch of cp command.

$ cp -p abc.txt /home/baba/Tgt
$ ls -ltr /home/baba/Tgt/abc.txt
-rwxrwxrwx  1 baba None  0 Dec 20 20:23 abc.txt

if you observe closely, now source and destination files are having same properties. We can preserve individual properties like mode, ownership, timestamps by using below syntax.

$ cp --preserve=mode abc.txt /home/baba/Tgt
$ man cp
#--* -p     same as --preserve=mode,ownership,timestamps


8. If file exists in destination path do not copy it

Scenario1:


$ ls -ltr /baba/home/abc.txt
-rw-------  1 baba None  0 Dec 20 20:23 abc.txt


$ ls -ltr /baba/home/Tgt/abc.txt
-rw-------  1 baba None  0 Dec 20 21:30 abc.txt


$ cp -n abc.txt /home/baba/Tgt
-rw-------  1 baba None  0 Dec 20 21:30 abc.txt

cp has not performed any operation ( see the file time stamp) as file is already available in destination path

Scenario2:

$ ls -ltr /baba/home/abc.txt
-rw-------  1 baba None  0 Dec 20 20:23 abc.txt


$ cp -n abc.txt /home/baba/Tgt
-rw-------  1 baba None  0 Dec 20 21:30 abc.txt

cp has copied the file with updated time stamp as file is not available in destination path.

9. Copy only when the source files are latest

Scenario1:

We have a file in abc.txt in source directory
-rw-------  1 baba None  0 Dec 20 20:23 abc.txt
and same file is exist in /home/baba/Tgt directory
-rw-------  1 baba None  0 Dec 20 21:30 abc.txt


$ cp -u abc.txt /home/baba/Tgt

Output:
$ ls -ltr /home/baba/Tgt/abc.txt
-rw-------  1 baba None  0 Dec 20 21:30 abc.txt


Scenario2:
Let say, we have files in source and destination with below properties.


Source:
-rw-------  1 baba None  0 Dec 20 21:50 abc.txt 
Destination:
-rw-------  1 baba None  0 Dec 20 21:30 abc.txt

$ cp -u abc.txt /home/baba/Tgt

Output:


$ ls -ltr /home/baba/Tgt/abc.txt
-rw-------  1 baba None  0 Dec 20 21:58 abc.txt



If you see in 2nd scenario, the source file is more recent than destination file, so cp will copy the file abc.txt from source to destination as it is newer than the destination file.


10. Remove destination files before copy from source
$ cp --remove-destination *.txt -v /home/baba/Tgt

Output: it will first remove the file from destination and then copy the source file. If file is not available in destination, it will simply copy the file in destination.

removed ‘/home/baba/Tgt/abc.txt’
‘abc.txt’ -> ‘/home/baba/Tgt/abc.txt’
removed ‘/home/baba/Tgt/pqr.txt’
‘pqr.txt’ -> ‘/home/baba/Tgt/pqr.txt’

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...