如何强制cp覆盖而不确认
我正在尝试使用 cp 命令并强制覆盖。
我已尝试 cp -rf /foo/* /bar,但仍然提示我确认每个覆盖。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
你可以做
yes | cp -rf xxx yyy
,但我的直觉告诉我,如果您以 root 身份执行此操作 - 您的.bashrc
或.profile
的别名为cp
到cp -i
,大多数现代系统(主要是 RH 衍生品)都会对根配置文件执行此操作。您可以通过在命令提示符下运行
alias
来检查现有别名,或者运行which cp
仅检查cp
的别名。如果您确实定义了别名,则运行
unalias cp
将取消当前会话的别名,否则您可以将其从 shell 配置文件中删除。您可以暂时绕过别名并使用命令的非别名版本,方法是在命令前面加上
\
前缀,例如\cp another
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 ofcp
tocp -i
, most modern systems (primarily RH-derivatives) do that to root profiles.You can check existing aliases by running
alias
at the command prompt, orwhich cp
to check aliases only forcp
.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
这可能是由于
cp
已经被别名为cp -i
等内容造成的。直接调用 cp 应该可以:解决此问题的另一种方法是使用 yes 命令:
This is probably caused by
cp
being already aliased to something likecp -i
. Callingcp
directly should work:Another way to get around this is to use the
yes
command:正如其他一些答案所述,您可能在某个地方使用别名将 cp 映射到 cp -i 或类似的东西。您可以通过在命令前面加上反斜杠来运行没有任何别名的命令。对于您的情况,请尝试
反斜杠将暂时禁用您调用的任何别名
cp
。As some of the other answers have stated, you probably use an alias somewhere which maps
cp
tocp -i
or something similar. You can run a command without any aliases by preceding it with a backslash. In your case, tryThe backslash will temporarily disable any aliases you have called
cp
.您可能在某处有一个别名,将 cp 映射到 cp -i ;因为使用默认设置,
cp
不会要求覆盖。检查您的.bashrc
、您的.profile
等。请参阅 cp manpage:只有指定
-i
参数时,cp
才会在覆盖前实际提示。您可以通过
alias
命令进行检查:要取消定义别名,请使用:
You probably have an alias somewhere, mapping
cp
tocp -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 willcp
actually prompt before overwriting.You can check this via the
alias
command:To undefine the alias, use:
正如其他答案所述,如果 cp 是 cp -i 的别名,则可能会发生这种情况。
您可以在
cp
命令前附加\
以在没有别名的情况下使用它。As other answers have stated, this could happend if
cp
is an alias ofcp -i
.You can append a
\
before thecp
command to use it without alias.默认情况下,
cp
的别名为cp -i
。你可以检查一下,输入alias
,你可以看到类似的内容:要解决这个问题,只需使用
/bin/cp /from /to
命令而不是cp /from /到
By default
cp
has aliase tocp -i
. You can check it, typealias
and you can see some like:To solve this problem just use
/bin/cp /from /to
command insteadcp /from /to
所以我经常遇到这种情况,因为我将 cp 别名为 cp -iv ,并且我发现了一个巧妙的技巧。事实证明,虽然
-i
和-n
都取消了之前的覆盖指令,但-f
却没有。但是,如果您使用-nf
,它会增加清除-i
的功能。所以:很整洁吧? /死尸帖
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:Pretty neat huh? /necropost
对我来说最简单的方法:
The simplest way for me:
您也可以使用此命令:
cp -ru /zzz/zzz/* /xxx/xxx
它会使用较新的文件更新您的现有文件。
you can use this command as well:
cp -ru /zzz/zzz/* /xxx/xxx
it would update your existing file with the newer one though.
我找到了这个
来源: https ://superuser.com/questions/358843/how-to-replace-all-the-contents-from-one-folder-with-another-one/358851
I found this
Source: https://superuser.com/questions/358843/how-to-replace-all-the-contents-from-one-folder-with-another-one/358851
<code>cp 通常是这样的别名,然后使用:
如果您确定要进行覆盖,则
cp
is usually aliased like thisif you are sure that you want to do the overwrite then use this:
也有效。
also works.
不使用别名调用命令的另一种方法是使用 bash 中的内置命令。
命令 cp -rf /zzz/zzz/*
Another way to call the command without the alias is to use the
command
builtin in bash.command cp -rf /zzz/zzz/*
-n 是“不覆盖”,但他的问题与您的回答完全相反。
为了避免这种确认,您可以简单地使用绝对路径运行 cp 命令,它将避免别名。
/bin/cp 源文件目标
-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
如果您想在全局级别保持别名不变并且只想更改您的脚本。
只需使用:
alias cp=cp
,然后编写后续命令。
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.
对我来说,越简单越好。这种方式不显示STDOUT:
For me, the simpler the better. This way does not show STDOUT:
我只是使用 unalias 删除“cp -i”别名,然后进行复制,然后设置回别名。 :
不是最漂亮的代码,但易于设置且高效。我还检查别名是否已通过简单的设置恢复
I simply used unalias to remove the "cp -i" alias, then do the copy, then set back the alias. :
Not the most beautiful code, but easy to set and efficient. I also check the alias is already set back with a simple
如果这是一个小文本文件,您也可以考虑这样:
不确定大文件或二进制文件的效率或副作用。
If this is a small text file, you may consider this way too:
Not sure about the efficiency or side effects for large or binary files.
它不是
cp -i
。如果您不想被要求确认,它是
cp -n
;例如:或者在目录/文件夹的情况下是:
It is not
cp -i
. If you do not want to be asked for confirmation,it is
cp -n
; for example:Or in case of directories/folders is: