How to force cp to overwrite without confirmation
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
![Thiyagarajan Varadharaj](https://i.stack.imgur.com/boOG4.jpg?s=256&g=1)
Comments
-
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 over 12 yearsyes, I did unalias cp -i , now its working.. Thank you for your valuable reply.
-
Thiyagarajan Varadharaj over 12 yearsyes, I did unalias cp -i , now its working.. Thank you for your valuable reply.
-
Matthew Leingang over 10 yearsI love that unix provides the
yes
command. Hilarious. And someday I may use it. -
Jon over 10 yearsAlso, 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 over 8 yearsyes it was aliased, nice catch :) You can use \cp to call the original cp, easier than remembering the path /bin/cp
-
Hiwa over 8 yearsYep, that's another way to bypass the alias. Kind of a neat hack!
-
plusmancn over 8 yearsThis way is more safe.
-
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 commandcp
. This appears to be the equivalent of runningcommand cp
. -
Ray Foss over 6 yearsBy "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 over 6 years@RayFoss Added a
(primarily RH-derivatives)
remark :) -
Krv Perera over 5 yearsit is just "unalias cp"
-
jaxarroyo over 5 yearsafter doing
unalias cp
and copying whatever you need to copy, you can set alias back to its default by doingalias cp='cp -i'
. After which, runalias cp
so you can verifiy that it's back to default alias. -
Abandoned Cart over 5 yearsOthers 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 over 4 yearsquick alternative:
\cp
-
phil294 over 4 yearsAs you said, others have already stated this. Why did this get 45 upvotes?
-
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 about 4 yearsalso if youre using oh-my-zsh, this the cp -i alias might be coming from common-aliases
-
IC_ over 3 yearsIt doesn't overwrite the file, just suppresses the message
-
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_ over 3 yearsyes, 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_ over 3 yearsCoreutils v8.31
-
Adam McCormick over 3 yearsYeah 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-invocation you might try--remove-destination
instead of-f
-
IC_ over 3 yearsit still prompts with
--remove-destination
and it writes its prompt into stderr -
Adam McCormick over 3 yearsI 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 about 2 yearsUsing 'unalias' saved my hours of hitting the wall. Thanks!
-
Ron Michael about 2 yearsThanks, working with ubuntu 21.10
cp -rf source/source_file.ext target/target_file.ext