How to force cp to overwrite without confirmation

LinuxCommand LineOverwriteCp

Linux Problem Overview


I'm trying to use the cp command and force an overwrite.

I have tried cp -rf /foo/* /bar, but I am still prompted to confirm each overwrite.

Linux Solutions


Solution 1 - Linux

You can do yes | cp -rf xxx yyy, but my gutfeeling says that if you do it as root - your .bashrc or .profile has an alias of cp to cp -i, most modern systems (primarily RH-derivatives) do that to root profiles.

You can check existing aliases by running alias at the command prompt, or which cp to check aliases only for cp.

If you do have an alias defined, running unalias cp will abolish that for the current session, otherwise you can just remove it from your shell profile.

You can temporarily bypass an alias and use the non-aliased version of a command by prefixing it with \, e.g. \cp whatever

Solution 2 - Linux

This is probably caused by cp being already aliased to something like cp -i. Calling cp directly should work:

/bin/cp -rf /zzz/zzz/* /xxx/xxx

Another way to get around this is to use the yes command:

yes | cp -rf /zzz/zzz/* /xxx/xxx

Solution 3 - Linux

As some of the other answers have stated, you probably use an alias somewhere which maps cp to cp -i or something similar. You can run a command without any aliases by preceding it with a backslash. In your case, try

\cp -r /zzz/zzz/* /xxx/xxx

The backslash will temporarily disable any aliases you have called cp.

Solution 4 - Linux

You probably have an alias somewhere, mapping cp to cp -i; because with the default settings, cp won't ask to overwrite. Check your .bashrc, your .profile etc.

See cp manpage: Only when -i parameter is specified will cp actually prompt before overwriting.

You can check this via the alias command:

$ alias
alias cp='cp -i'
alias diff='diff -u'
....

To undefine the alias, use:

$ unalias cp

Solution 5 - Linux

As other answers have stated, this could happend if cp is an alias of cp -i.

You can append a \ before the cp command to use it without alias.

\cp -fR source target

Solution 6 - Linux

So I run into this a lot because I keep cp aliased to cp -iv, and I found a neat trick. It turns out that while -i and -n both cancel previous overwrite directives, -f does not. However, if you use -nf it adds the ability to clear the -i. So:

cp -f /foo/* /bar  <-- Prompt
cp -nf /foo/* /bar <-- No Prompt

Pretty neat huh? /necropost

Solution 7 - Linux

By default cp has aliase to cp -i. You can check it, type alias and you can see some like:

alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'

To solve this problem just use /bin/cp /from /to command instead cp /from /to

Solution 8 - Linux

The simplest way for me:

yes | cp source destination

Solution 9 - Linux

you can use this command as well:

cp -ru /zzz/zzz/* /xxx/xxx

it would update your existing file with the newer one though.

Solution 10 - Linux

cp is usually aliased like this

alias cp='cp -i'   # i.e. ask questions of overwriting

if you are sure that you want to do the overwrite then use this:

/bin/cp <arguments here> src dest

Solution 11 - Linux

Solution 12 - Linux

cp -u ...
cp --update ...

also works.

Solution 13 - Linux

Another way to call the command without the alias is to use the command builtin in bash.

command cp -rf /zzz/zzz/*

Solution 14 - Linux

-n is "not to overwrite" but his question is totally opposite what you replied for.

To avoid this confirmation you can simply run the cp command wiht absolute path, it will avoid the alias.

/bin/cp sourcefile destination

Solution 15 - Linux

If you want to keep alias at the global level as is and just want to change for your script.

Just use:

alias cp=cp

and then write your follow up commands.

Solution 16 - Linux

I simply used unalias to remove the "cp -i" alias, then do the copy, then set back the alias. :

unalias cp  
cp -f foo foo.copy  
alias cp="cp -i"  

Not the most beautiful code, but easy to set and efficient. I also check the alias is already set back with a simple

alias |grep cp

Solution 17 - Linux

It is not cp -i. If you do not want to be asked for confirmation, it is cp -n; for example:

cp -n src dest

Or in case of directories/folders is:

cp -nr src_dir dest_dir

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThiyagarajan VaradharajView Question on Stackoverflow
Solution 1 - LinuxfavorettiView Answer on Stackoverflow
Solution 2 - LinuxpglView Answer on Stackoverflow
Solution 3 - LinuxChrisView Answer on Stackoverflow
Solution 4 - LinuxcodelingView Answer on Stackoverflow
Solution 5 - LinuxArnold RoaView Answer on Stackoverflow
Solution 6 - LinuxAdam McCormickView Answer on Stackoverflow
Solution 7 - LinuxAvseiytsev DmitriyView Answer on Stackoverflow
Solution 8 - LinuxmalcolmView Answer on Stackoverflow
Solution 9 - LinuxsigejeView Answer on Stackoverflow
Solution 10 - Linuxuser5035029View Answer on Stackoverflow
Solution 11 - LinuxDaniel Luevano AlonsoView Answer on Stackoverflow
Solution 12 - LinuxsoftwarevampView Answer on Stackoverflow
Solution 13 - LinuxSteve BuzonasView Answer on Stackoverflow
Solution 14 - LinuxRohit RajputView Answer on Stackoverflow
Solution 15 - LinuxAnkit BhatnagarView Answer on Stackoverflow
Solution 16 - LinuxMaatView Answer on Stackoverflow
Solution 17 - LinuxmihaitzateoView Answer on Stackoverflow