739

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.

Duncan X Simpson
  • 980
  • 1
  • 12
  • 30
thiyagu114
  • 8,904
  • 5
  • 19
  • 18

16 Answers16

1225

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

favoretti
  • 27,251
  • 4
  • 42
  • 57
  • 3
    Also, be wary -- even if the alias isn't directly written in .bashrc, if *anything* else this file calls ends up calling something else which manipulates the alias for cp, you will run into this behavior. – Jon Mar 10 '14 at 17:42
  • 7
    By "modern systems" he means RHEL/centos/fedora and perhaps something else, Debian/Ubuntu does not alias cp. I prefer RHEL on the server and used Fedora for nearly a decade, but the community support of Ubuntu and the switch back to Gnome wooed me after Nvidia killed my Fedora install. I'm `¯\_(ツ)_/¯` about it. – Ray Foss Jan 21 '18 at 06:35
  • 2
    @RayFoss Added a `(primarily RH-derivatives)` remark :) – favoretti Jan 21 '18 at 11:46
  • after doing `unalias cp` and copying whatever you need to copy, you can set alias back to its default by doing `alias cp='cp -i'`. After which, run `alias cp` so you can verifiy that it's back to default alias. – jaxarroyo Nov 15 '18 at 18:33
345

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
pgl
  • 6,613
  • 2
  • 21
  • 31
136

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.

Chris
  • 39,262
  • 15
  • 126
  • 145
  • 4
    @zhouji The backlash disables the alias, as stated in my answer. So instead of invoking the alias `cp`, `\cp` will invoke the command `cp`. This appears to be the equivalent of running `command cp`. – Chris Jul 21 '16 at 10:28
76

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
codeling
  • 9,289
  • 4
  • 36
  • 61
55

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
Arnold Roa
  • 6,338
  • 5
  • 43
  • 60
  • 1
    As you said, others have already stated this. Why did this get 45 upvotes? – phil294 Dec 27 '19 at 14:44
  • @phil294 I guess this is the easiest way without modifying alias or removing the cp alias which may be useful for other usages. `\` Its very useful here. – Arnold Roa Jan 19 '20 at 13:02
20

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

Avseiytsev Dmitriy
  • 1,040
  • 7
  • 18
16

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

Adam McCormick
  • 1,506
  • 16
  • 22
  • It doesn't overwrite the file, just suppresses the message – Herrgott Oct 06 '20 at 04:24
  • @Herrgott are you saying that `-f` doesn't force an overwrite? If so I would expect a permissions issue. The point of OP was to clear the effect of the `-i` directive making the removal not interactive. – Adam McCormick Oct 07 '20 at 17:59
  • yes, it doesn't work in my case. `*user@pc-1250* /tmp/ttt: cp -f -- a/* b/` `cp: overwrite 'b/0'? `. If i call it with `-nf` it won't ask for overwrite and won't overwrite (only copies missing files) – Herrgott Oct 08 '20 at 06:17
  • Coreutils v8.31 – Herrgott Oct 08 '20 at 06:30
  • Yeah looks like Coreutils works differently than the `cp` command on my mac. They explicitly ignore `-f` when `-n` is used according to https://www.gnu.org/software/coreutils/manual/coreutils.html#cp-invocation you might try `--remove-destination` instead of `-f` – Adam McCormick Oct 08 '20 at 21:00
  • it still prompts with `--remove-destination` and it writes its prompt into stderr – Herrgott Oct 09 '20 at 09:44
  • I mean with `-n` first, just using `--remove-destination` instead of `-f`. `-n` is the only way I can find of clearing the `-i` flag – Adam McCormick Oct 11 '20 at 04:43
9

you can use this command as well:

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

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

sigeje
  • 329
  • 3
  • 7
9

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
jotik
  • 14,982
  • 9
  • 48
  • 106
user5035029
  • 115
  • 1
  • 1
8

I found this

'cp' -rf * /data/danalonso_testing/target/

Source: https://superuser.com/questions/358843/how-to-replace-all-the-contents-from-one-folder-with-another-one/358851

6

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

command cp -rf /zzz/zzz/*

Steve Buzonas
  • 4,245
  • 27
  • 53
6
cp -u ...
cp --update ...

also works.

softwarevamp
  • 607
  • 8
  • 12
4

-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

2

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.

Ankit Bhatnagar
  • 635
  • 5
  • 16
0

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
Maat
  • 1
  • 2
-2

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
Matt
  • 2,910
  • 4
  • 28
  • 35
  • 2
    Others were stating that the user-facing cp was symlinked to `cp -i` by the system, meaning they were trying to overcome the default and **force** an overwrite. It sounds like you may have confused that for being the suggested syntax, but `-n` will **prevent** an overwrite. – Abandoned Cart Dec 16 '18 at 21:38