Saturday, 28 January 2012

cp

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.

Example1)

To copy file1 to file2

/home/justin$cpsl_ind.ctlsql_ind2.ctl

This copies the contents of the file sl_ind.ctlto 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.

assumesql_ind2.ctlalready exists.

/home/justin$ cp-isql_ind.ctlsql_ind2.ctl

This will ask you to whether to overwrite or not.

You can also use wildcards like ? and *along with parametersthatdefine a group ofsource filenames.

Example 2)

when you copy files fromsource toa different directory ,you do not have to mention the

filenamein 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$ cpJlibs.all.csv/home/raja/libs# this will placeJlibs.all.csvin /home/raja/libs.

/home/justin$ cpJlibs.all.csv/home/raja/libs/rlibs.all.csv#This will

Create rlibs.all.csvin /home/raja/libsdirectory

Example 3)

cphas option –p for preserving the file attributes like modification time,without this option, cp by defaultputs new time stamp for destination files.

/home/justin$ls-lrtflower?.jpg

-rw-r--r--1 justinAdmin59254Jan03 12:14 flower3.jpg

-rw-r--r--1 justinAdmin59256 Dec 29 17:22 flower2.jpg

/home/justin$cp-pflower3.jpgflower2.jpg

/home/justin$ls-lrtflower?.jpg

-rw-r--r--1 justinAdmin59254 Jan 03 12:14 flower2.jpg

-rw-r--r--1 justinAdmin59254Jan03 12:14 flower3.jpg

Example 4)

you want to completely copy a file systemwith all the files and directories in itand create