linux - 如何在没有确认的情况下强制cp覆盖
我正在尝试使用cp -rf /foo/* /bar命令并强制覆盖。
我试过cp -rf /foo/* /bar,但仍然提示我确认每次覆盖。
15个解决方案
884 votes
你可以做\,但我的gutfeeling说,如果你以root身份执行 - 你的\cp whatever或cp的别名是cp到cp -i,大多数现代系统(主要是RH衍生物)都是这样做的根配置文件。
您可以通过在命令提示符下运行\或\cp whatever来检查现有别名,以仅检查cp的别名。
如果您确实定义了别名,则运行\将取消当前会话的别名,否则您可以从shell配置文件中删除它。
您可以临时绕过别名并使用命名的非别名版本,前缀为\,例如:\cp whatever
favoretti answered 2019-01-25T23:47:46Z
217 votes
这可能是由于cp已经被别名为yes所致。直接调用cp应该可以正常工作:
/bin/cp -rf /zzz/zzz/* /xxx/xxx
解决此问题的另一种方法是使用yes命令:
yes | cp -rf /zzz/zzz/* /xxx/xxx
pgl answered 2019-01-25T23:48:14Z
91 votes
正如其他一些答案所述,您可能会使用某个别名来映射cp到cp -i或类似的东西。 您可以通过在其前面加上反斜杠来运行没有任何别名的命令。 在你的情况下,试试吧
\cp -r /zzz/zzz/* /xxx/xxx
反斜杠将暂时禁用您调用的任何别名cp。
Chris answered 2019-01-25T23:48:43Z
41 votes
你可能在别的地方有一个别名,映射alias到cp; 因为使用默认设置,cp不会要求覆盖。 检查您的.bashrc,您的.profile等。
请参阅cp联机帮助页:仅当指定alias参数时才会覆盖cp实际提示。
您可以通过alias命令查看:
$ alias
alias cp='cp -i'
alias diff='diff -u'
....
要取消定义别名,请使用:
$ unalias cp
codeling answered 2019-01-25T23:49:25Z
35 votes
正如其他答案所述,如果cp是别名\,则可能会发生这种情况。
您可以在cp命令之前附加\以使用它而不使用别名。
\cp -fR source target
Arnold Roa answered 2019-01-25T23:49:53Z
18 votes
默认/bin/cp /from /to具有别名为cp /from /to.您可以检查它,输入alias,你会看到一些像:
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'
要解决此问题,请使用/bin/cp /from /to命令代替cp /from /to
Avseiytsev Dmitriy answered 2019-01-25T23:50:22Z
8 votes
cp通常是这样的别名
alias cp='cp -i' # i.e. ask questions of overwriting
如果您确定要进行覆盖,请使用以下命令:
/bin/cp src dest
user5035029 answered 2019-01-25T23:50:50Z
5 votes
你也可以使用这个命令:
cp -ru /zzz/zzz/* /xxx/xxx
它会用更新的文件更新你现有的文件。
sigeje answered 2019-01-25T23:51:25Z
4 votes
在没有别名的情况下调用命令的另一种方法是使用bash内置的command cp -rf /zzz/zzz/*。
command cp -rf /zzz/zzz/*
Steve Buzonas answered 2019-01-25T23:51:54Z
2 votes
-n是“不要覆盖”,但他的问题完全与你回答的相反。
要避免此确认,您只需使用绝对路径运行cp命令,它将避免别名。
/ bin / cp sourcefile目的地
Rohit Rajput answered 2019-01-25T23:52:28Z
2 votes
如果您希望将别名保持在全局级别,只想更改脚本。
只需使用:
别名cp = cp
然后编写您的后续命令。
Ankit Bhatnagar answered 2019-01-25T23:53:04Z
2 votes
cp -u ...
cp --update ...
也有效。
softwarevamp answered 2019-01-25T23:53:26Z
1 votes
我找到了这个
'cp' -rf * /data/danalonso_testing/target/
资料来源:[https://superuser.com/questions/358843/how-to-replace-all-the-contents-from-one-folder-with-another-one/358851]
Daniel Luevano Alonso answered 2019-01-25T23:53:54Z
0 votes
我只是使用unalias删除“cp -i”别名,然后执行复制,然后设置别名。:
unalias cp
cp -f foo foo.copy
alias cp="cp -i"
不是最漂亮的代码,但易于设置和高效。 我也检查别名已经用简单的方法设置了
alias |grep cp
Maat answered 2019-01-25T23:54:23Z
-2 votes
它不是cp -i.如果您不想被要求确认,它是cp -n; 例如:
cp -n src dest
或者在目录/文件夹的情况下:
cp -nr src_dir dest_dir
mihaitzateo answered 2019-01-25T23:54:52Z