03 January 2012

"cp" Command in Linux/Unix

Leave a Comment
"cp" Command in Linux/Unix


                                                       The cp command copies file or group of files. It creates exact image on disk with different name.


Syntax       


                  cp [option]  source   destination


*In above syntax source is copied to destination


Example 


                           cp  file1 file2


$ ls -l file2
ls: cannot access file2: No such file or directory
$ cat file1
sandeep kumar singh

$ cp file1 file2

$ cat file2
sandeep kumar singh
$
                
Some points Regarding above Example


* if file2 doesn't exist then cp created file2 then copy the content of file1 to file2
* if file2 already exist then  cp overwrites the content of file2.
* if there is only one file to be copied then destination can be file or a directory.


-> File can be copied to directory with same name or changed name.


Example 


                 cp    file1    cpdir                       //file1 copy with same name
                 cp    file1    cpdir/newfile1       //file1 name change to newfile1


$ cat file1
my name is xyz
iam a good boy
$ cd css
$ pwd
/home/sandeep/css
$ ls
index.html                                   //only index.html in css directory
$ cd ..
$ cp file1 css/mewfile                //file1 copied in css directory with different name
$ cp file1 css                              //file1 copied in css directory with same name
$ cd css
$ ls
file1 index.html   mewfile                       
$




* In above screenshot file1 copied to css directory with same name and with different name.




-> cp can also used to copy multiple file to a destination in single invocation. In this case destination must  be directory , it cannot be a file.


Example


                     cp  file1  file2  file3   cpdir


$ ls
abc.lst     def.lst         examples.desktop       js

$ mkdir newdir
$ cd newdir
$ ls                                             //no file in newdir directory
$ cd ..
$ cp abc.lst def.lst newdir
$ cd newdir
$ ls
abc.lst def.lst



-> Options used with cp command


1. -i : The (-i) interactive options warn the user before overwriting the destination file. If destination file exist then only , cp prompts for response .


Example


                      cp  -i  abc.lst file1


$ cat pqr.lst
my name is pqr
iam also a good person

$ cat abc.lst
my name is xyz
iam a good boy

$ cp -i abc.lst pqr.lst
cp: overwrite `pqr.lst'? y

$ cat pqr.lst
my name is xyz
iam a good boy
$




2. -R : Recursive copy , this command is used to copy one directory to another directory with its entire sub-directories and files if exist.


Example


                     cp -R  cpdir  cpdir1        //cpdir1 must not exist


$ cd shell1
cd: 13: can't cd to shell1

$ ls shell
abc.lst                    customer_report    fact.sh      shell1.sh   shell3.sh   shell5.sh
customer1_report    cut1                     prime.sh   shell2.sh   shell4.sh    xyz.lst

$ cp -R shell shell1

$ cd shell1
$ ls
abc.lst                       customer_report   fact.sh     shell1.sh    shell3.sh   shell5.sh
customer1_report      cut1                     prime.sh    shell2.sh   shell4.sh   xyz.lst
$


Important points about above option


* if cpdir1 doesn't exist then cp creates it along with the associated sub-directories and files. (like in above example shell1 created and all the files of shell is copied into it)
* But if  cpdir1 already exist then cpdir become a sub-directory  under cpdir1.  


Example




* Sometime it is not possible to copy a file , this can happen if it's read -protected or the destination file or directory is write protected.

If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment