Linux 复制并覆盖 shell 脚本中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13855904/
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
Copy and overwrite a file in shell script
提问by AlwaysALearner
I want to copy a certain file to a location, irrespective of that file already exists in the destination or not. I'm trying to copy through shell script.But the file is not getting copied. I'm using the following command
我想将某个文件复制到某个位置,而不管该文件是否已存在于目标中。我正在尝试通过 shell 脚本进行复制。但是文件没有被复制。我正在使用以下命令
/bin/cp -rf /source/file /destination
/bin/cp -rf /source/file /destination
but that doesn't work.
但这不起作用。
采纳答案by Luca Davanzo
This question has been already discussed, however you can write a little script like this:
这个问题已经讨论过了,但是你可以写一个这样的小脚本:
#!/bin/bash
if [ ! -d "" ]; then
mkdir -p ""
fi
cp -R "" ""
回答by Maulzey
Use
用
cp -fr /source/file /destination
this should probably solve the problem.
这应该可以解决问题。
回答by user309122
Your problem might be caused by an alias for cp command created in your system by default (you can see al your aliases by typing "alias"). For example, my system has the following alis by default: alias cp='cp -i', where -i overrides -f option, i.e. cp will always prompt for overwriting confirmation.
您的问题可能是由系统中默认创建的 cp 命令别名引起的(您可以通过键入“别名”查看所有别名)。比如我的系统默认有如下alis:alias cp='cp -i',这里-i覆盖-f选项,即cp会一直提示覆盖确认。
What you need in such case (that'll actually work even if you don't have an alias) is to feed "yes" to that confirmation. To do that simply modify your cp command to look like this:
在这种情况下,您需要做的是(即使您没有别名,这实际上也可以工作)是为该确认提供“是”。为此,只需将您的 cp 命令修改为如下所示:
yes | cp /source/file /destination
是 | cp /源/文件/目标
回答by denesuk
/bin/cp -rf src dst or /usr/bin/env cp -rf
/bin/cp -rf src dst 或 /usr/bin/env cp -rf