How to force cp to overwrite without confirmation

1,330,131

Solution 1

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

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

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

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

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
Share:
1,330,131
Thiyagarajan Varadharaj
Author by

Thiyagarajan Varadharaj

A passionate linux administrator.

Updated on July 20, 2022

Comments

  • Thiyagarajan Varadharaj
    Thiyagarajan Varadharaj almost 2 years

    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.

  • Thiyagarajan Varadharaj
    Thiyagarajan Varadharaj over 12 years
    yes, I did unalias cp -i , now its working.. Thank you for your valuable reply.
  • Thiyagarajan Varadharaj
    Thiyagarajan Varadharaj over 12 years
    yes, I did unalias cp -i , now its working.. Thank you for your valuable reply.
  • Matthew Leingang
    Matthew Leingang over 10 years
    I love that unix provides the yes command. Hilarious. And someday I may use it.
  • Jon
    Jon over 10 years
    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.
  • Hugo Zaragoza
    Hugo Zaragoza over 8 years
    yes it was aliased, nice catch :) You can use \cp to call the original cp, easier than remembering the path /bin/cp
  • Hiwa
    Hiwa over 8 years
    Yep, that's another way to bypass the alias. Kind of a neat hack!
  • plusmancn
    plusmancn over 8 years
    This way is more safe.
  • Chris
    Chris almost 8 years
    @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.
  • Ray Foss
    Ray Foss over 6 years
    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.
  • favoretti
    favoretti over 6 years
    @RayFoss Added a (primarily RH-derivatives) remark :)
  • Krv Perera
    Krv Perera over 5 years
    it is just "unalias cp"
  • jaxarroyo
    jaxarroyo over 5 years
    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.
  • Abandoned Cart
    Abandoned Cart over 5 years
    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.
  • phil294
    phil294 over 4 years
    quick alternative: \cp
  • phil294
    phil294 over 4 years
    As you said, others have already stated this. Why did this get 45 upvotes?
  • Arnold Roa
    Arnold Roa over 4 years
    @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.
  • Matt Pengelly
    Matt Pengelly about 4 years
    also if youre using oh-my-zsh, this the cp -i alias might be coming from common-aliases
  • IC_
    IC_ over 3 years
    It doesn't overwrite the file, just suppresses the message
  • Adam McCormick
    Adam McCormick over 3 years
    @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.
  • IC_
    IC_ over 3 years
    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)
  • IC_
    IC_ over 3 years
    Coreutils v8.31
  • Adam McCormick
    Adam McCormick over 3 years
    Yeah looks like Coreutils works differently than the cp command on my mac. They explicitly ignore -f when -n is used according to gnu.org/software/coreutils/manual/coreutils.html#cp-invocati‌​on you might try --remove-destination instead of -f
  • IC_
    IC_ over 3 years
    it still prompts with --remove-destination and it writes its prompt into stderr
  • Adam McCormick
    Adam McCormick over 3 years
    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
  • Hardoman
    Hardoman about 2 years
    Using 'unalias' saved my hours of hitting the wall. Thanks!
  • Ron Michael
    Ron Michael about 2 years
    Thanks, working with ubuntu 21.10 cp -rf source/source_file.ext target/target_file.ext