Linux 如何在没有确认的情况下强制 cp 覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8488253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:37:44  来源:igfitidea点击:

How to force cp to overwrite without confirmation

linuxcommand-lineoverwritecp

提问by thiyagu114

I'm trying to use the cpcommand and force an overwrite.

我正在尝试使用该cp命令并强制覆盖。

I have tried cp -rf /foo/* /bar, but I am still prompted to confirm each overwrite.

我已经尝试过cp -rf /foo/* /bar,但仍然提示我确认每个覆盖。

采纳答案by favoretti

You can do yes | cp -rf xxx yyy, but my gutfeeling says that if you do it as root - your .bashrcor .profilehas an alias of cpto cp -i, most modern systems (primarily RH-derivatives) do that to root profiles.

您可以这样做yes | cp -rf xxx yyy,但我的直觉告诉您,如果您以 root 身份执行此操作 - 您的.bashrc.profile具有cpto的别名cp -i,大多数现代系统(主要是 RH 衍生物)都会对 root 配置文件执行此操作。

You can check existing aliases by running aliasat the command prompt, or which cpto check aliases only for cp.

您可以通过alias在命令提示符下运行来检查现有别名,或者which cp仅检查cp.

If you do have an alias defined, running unalias cpwill abolish that for the current session, otherwise you can just remove it from your shell profile.

如果您确实定义了别名,则运行unalias cp将取消当前会话的别名,否则您可以将它从您的 shell 配置文件中删除。

You can temporarily bypass an alias and use the non-aliased version of a command by prefixing it with \, e.g. \cp whatever

您可以暂时绕过别名并通过使用前缀来使用命令的非别名版本\,例如\cp whatever

回答by codeling

You probably have an alias somewhere, mapping cpto cp -i; because with the default settings, cpwon't ask to overwrite. Check your .bashrc, your .profileetc.

您可能在某处有一个别名,映射cpcp -i; 因为使用默认设置,cp不会要求覆盖。检查你的.bashrc,你的.profile等等。

See cp manpage: Only when -iparameter is specified will cpactually prompt before overwriting.

参见cp manpage: 只有-i指定了参数才会cp在覆盖前实际提示。

You can check this via the aliascommand:

您可以通过以下alias命令进行检查:

$ alias
alias cp='cp -i'
alias diff='diff -u'
....

To undefine the alias, use:

要取消定义别名,请使用:

$ unalias cp

回答by pgl

This is probably caused by cpbeing already aliased to something like cp -i. Calling cpdirectly should work:

这可能是由于cp已经别名为cp -i. cp直接调用应该有效:

/bin/cp -rf /zzz/zzz/* /xxx/xxx

Another way to get around this is to use the yescommand:

解决此问题的另一种方法是使用以下yes命令:

yes | cp -rf /zzz/zzz/* /xxx/xxx

回答by Chris

As some of the other answers have stated, you probably use an alias somewhere which maps cpto cp -ior something similar. You can run a command without any aliases by preceding it with a backslash. In your case, try

正如其他一些答案所述,您可能在某处使用了映射cpcp -i或类似的别名。您可以通过在命令前面加上反斜杠来运行没有任何别名的命令。在你的情况下,试试

\cp -r /zzz/zzz/* /xxx/xxx

The backslash will temporarily disable any aliases you have called cp.

反斜杠将暂时禁用您调用的任何别名cp

回答by Avseiytsev Dmitriy

By default cphas aliase to cp -i. You can check it, type aliasand you can see some like:

默认情况下cp,别名为cp -i. 你可以检查它,输入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'

To solve this problem just use /bin/cp /from /tocommand instead cp /from /to

要解决此问题,请改用/bin/cp /from /to命令cp /from /to

回答by sigeje

you can use this command as well:

你也可以使用这个命令:

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

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

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

不过,它会用较新的文件更新您现有的文件。

回答by Steve Buzonas

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

另一种在没有别名的情况下调用命令的方法是command在 bash 中使用内置命令。

command cp -rf /zzz/zzz/*

command cp -rf /zzz/zzz/*

回答by user5035029

cpis usually aliased like this

cp通常是这样的别名

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

回答by mihaitzateo

It is not cp -i. If you do not want to be asked for confirmation, it is cp -n; for example:

不是cp -i。如果您不想被要求确认,则是cp -n; 例如:

cp -n src dest

Or in case of directories/folders is:

或者在目录/文件夹的情况下是:

cp -nr src_dir dest_dir

回答by Maat

I simply used unalias to remove the "cp -i" alias, then do the copy, then set back the alias. :

我只是使用 unalias 删除“cp -i”别名,然后进行复制,然后重新设置别名。:

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