Saturday, 28 January 2012


cp is used to copy files with a name to a different name in the same path or to a different path.It can also be used to recursively copy files in a directory structure to a different directory.

To copy file1 to file2

/home/justin$cp   sl_ind.ctl  sql_ind2.ctl  

This  copies the contents of the file sl_ind.ctl   to    sql_ind2.ctl  . If there is already a file named     sql_ind2.ctl in the current directory, then the   file will be over written ,if it does not exist, the new file will be created.

If you do not want files to be overwritten  , use –i option. give y or n as reply.
assume    sql_ind2.ctl    already exists.

/home/justin$ cp  -i  sql_ind.ctl    sql_ind2.ctl  
This will ask you to whether to overwrite or not.

You can also use wildcards like ? and *   along  with  parameters  that  define a group of  source filenames.

Example 2)
 when you copy files from  source to  a different directory ,you do not have to mention the
filename  in the destination path if you want to keep the name of the file same. Do it
only  when  destination filename  is to be different.

Suppose /home/raja/libs is a directory,

/home/justin$ cp  Jlibs.all.csv    /home/raja/libs   # this will place  Jlibs.all.csv   in /home/raja/libs.

/home/justin$ cp  Jlibs.all.csv    /home/raja/libs/rlibs.all.csv    #This will
Create rlibs.all.csv     in /home/raja/libs  directory

Example 3)
cp  has option –p for preserving the file attributes like modification time,without this option, cp by default  puts new time stamp for destination files.

/home/justin$ls   -lrt  flower?.jpg
-rw-r--r--    1 justin  Admin    59254  Jan  03 12:14 flower3.jpg
-rw-r--r--    1 justin  Admin    59256 Dec 29 17:22 flower2.jpg

/home/justin$cp  -p   flower3.jpg    flower2.jpg
/home/justin$ls   -lrt  flower?.jpg
-rw-r--r--    1 justin  Admin    59254 Jan 03 12:14 flower2.jpg
-rw-r--r--    1 justin  Admin    59254  Jan  03 12:14 flower3.jpg

Example 4)
you want to completely copy a file system  with all the files and directories in it  and create
the same directory structure, use cp with –R option.
/home$ cp  -R  justin  /backup_justin
/home$cd  /backup_justin
/backup_justin$find  .  –type f
Justin/ flower3.jpg   
Justin/ sql_ind.ctl   
Justin/ wildlife/tiger/video3.mpg
Justin/ client-file/voucher1.txt

No comments:

Post a Comment