cancel
Showing results for
Did you mean:

SOLVED
Go to solution

How to copy with "overwrite" option

Hi,
I am using RedHat 5.0.
I want to copy some files into a directory and want to overwrite the files with the same name. Is there any option that suppress the messages "File exists, do you want to overwrite (Y/N)?" for each file? I tried to use cp -f option but it does not suppress the messages.

Thank you.
9 REPLIES
Honored Contributor

Re: How to copy with "overwrite" option

hi,

in this case i would recommend to use rsync or try to use : cp -avf

mikap
Honored Contributor

Re: How to copy with "overwrite" option

Michal is right,

but in most any general case where you want to answer a bunch of yes's to a series of questions, regardless of program that you're running it with, you can run the "yes" program.

$> yes | cp -f Of course, arg1, arg2 would be whatever it is you're trying to copy. We are the people our parents warned us about --Jimmy Buffett Honored Contributor Solution Re: How to copy with "overwrite" option When you're running as root, RedHat's default environment includes some "safety net" aliases, like: alias cp='cp -i' alias mv='mv -i' alias rm='rm -i' To get cp with the traditional Unix behavior of not asking unless you explicitly use the -i option, run "unalias cp" to remove the alias definition. If you always want it to behave as traditional, comment out the alias definition in your .bash_profile, .bashrc or /etc/profile (wherever it might be on your RedHat version). NOTE: do this only if you're *really* sure you won't *ever* make typing mistakes when running as root. MK MK Honored Contributor Re: How to copy with "overwrite" option > [...] "unalias cp" to remove the alias > definition. Or simply bypass the alias by specifying a path to the "cp" program. I don't know where Red Hat actually keeps it, but I'd expect "/bin/cp" to work. > NOTE: do this only if [...] Removing annoyances like this is one of the first things I normally do when installing a UNIX(-like) OS. I find that so-called safety features like these tend to get bypassed so regularly that they become ineffective. I prefer to know what I'm doing, and to specify "-i" when I choose to. Aliases which add options to normal commands (programs) are the first things which I get rid of. I've been doing this stuff for a while, and I have yet to do anything like, say: cd / rm -r * But I have read reports... Trusted Contributor Re: How to copy with "overwrite" option Alternately, if you only want to do it for a single command, and assuming your shell is bash, you can prefix the line with a backslash: --$ >a
$ls a$ alias cp='echo "haha, sucker"'
$cp a b haha, sucker a b$ \cp a b
$ls a b$
--

This bypasses the alias on a case-by-case basis.
Honored Contributor

Re: How to copy with "overwrite" option

The culprit are those arguable "safety" settings of alii in root's ~/.bashrc that the RH intallation provided.

As already mentioned by others you can either unalias them (type "unalias rm" in your shell, or comment out the "alias rm='rm -i' entry in ~root/.bashrc altogether and open a new subshell (just issue bash) or login again.
Other options are to give the full path to the rm command, prepend (the unpathed) rm with the word "command" (e.g. "command rm /some/file") or simply with a backslash (e.g. "\rm /some/file").

Honored Contributor

Re: How to copy with "overwrite" option

or you shouldn't be using root, but use sudo instead and then there will be no pesky 'y/n' questions asked.