1826420 Members
3386 Online
109692 Solutions
New Discussion

Re: cp command options

 
SOLVED
Go to solution
Shivkumar
Super Advisor

cp command options

Dear Sirs,

What is the option to use while using cp command so that any existing files with the same names are not overwritten ?

Thanks,
Shiv
14 REPLIES 14
Zigor Buruaga
Esteemed Contributor
Solution

Re: cp command options

Hi,

If I recall correctly, "cp -i" will ask you to confirm any replacement.

Regards,
Zigor
Slawomir Gora
Honored Contributor

Re: cp command options

cp -i
Pat Lieberg
Valued Contributor

Re: cp command options

I don't recall an option that automatically skips overwriting files, but you can do a -i for interactive mode, which will prompt you to overwrite or not.
Patrick Wallek
Honored Contributor

Re: cp command options

Have a look at the man page. That gives information on all options available.

# man cp
Jean-Luc Oudart
Honored Contributor

Re: cp command options

May be you should protect the target file from being overwritten (file permissions).

I don't know any option with the cp command that would do what you want.

Regards
Jean-Luc
fiat lux
Ranjith_5
Honored Contributor

Re: cp command options

Hi shiva,

cp -i will do the work. As man page says,

-i (interactive copy) Cause cp to write a prompt to standard error and wait for a response before copying a file that would overwrite an existing file. If the response from the standard input is affirmative, the file is copied if permissions allow the copy. If the -i (interactive) and -f (forced-copy)options are both specified, the -i option is ignored.

Regards,
Syam
Rick Garland
Honored Contributor

Re: cp command options

cp -i will be in interactive mode but if you many (lots of them) to copy, you could be sitting there answering the question for each file for quite some time.

cp itself does not have a protection like you seek.


Protect the files that previously exist. Example: cp the originals off somewhere, do your copy. then cp the originals back.

If you do the cp as root, changing the perms will not protect the originals.
Rick Garland
Honored Contributor

Re: cp command options

Thinking of another possibility, 'cpio' will preserve older files unless to specify the option to overwrite.



Shivkumar
Super Advisor

Re: cp command options

What would be the syntax of "cpio" to use ?

sorry, i am not an expert in unix sys admin world and hence lots of silly questions.

Thanks,
Shiv
Ranjith_5
Honored Contributor

Re: cp command options

Hi Shiva,

Lets go through an example.

Move (copying) /usr
Copy a directory from one location to another. Even if it has "special" files and links in it:
cd /usr
find . -print | cpio -dlmpuvx /usr2
d = Do directories
l = Do links
m = Keep time
p = Pass along
u = Copy unconditionaly
v = verbose
x = Save device special files

for copying data from one disk to another disk.

find . -depth -print | cpio -odmv > newdisk

see complete command ref.

http://docs.hp.com/en/B2355-60105/cpio.1.html



Regards,
Syam
Ranjith_5
Honored Contributor

Re: cp command options

EXAMPLES
===============
Copy the contents of a directory into a tape archive:

ls | cpio -o > /dev/rmt/c0t0d0BEST

Duplicate a directory hierarchy:

cd olddir
find . -depth -print | cpio -pd newdir

The trivial case

find . -depth -print | cpio -oB >/dev/rmt/c0t0d0BEST

can be handled more efficiently by:

find . -cpio /dev/rmt/c0t0d0BEST


Regards,
Syam
John Kittel
Trusted Contributor

Re: cp command options

Many people will define an alias in /etc/profile or ~/.profile so that the cp command always runs with -i. For example, in your .profile put the line:

cp='/usr/bin/cp -i'

Also, a good idea for rm and mv commands:

rm='/usr/bin/rm -i'
mv='/usr/bin/mv -i'

Also, again in .profile, you can prevent shell output redirection from overwriting existing files.

for posix and korn shells:

set -o noclobber

see the man pages for additional explanation.

- John
Arturo Galbiati
Esteemed Contributor

Re: cp command options

cp -i
the best should be to define an alias:
alias cp='cp -i'.
It this way any time you try to cp a file if teh detsination file is not empty teh command asks for your confirmation.

HTH,
Art
MarkSyder
Honored Contributor

Re: cp command options

Run the following command before you start your copy:

set noclobber

This prevents files from being overwritten.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing