如何强制'cp'覆盖目录,而不是在内部创建另一个目录? [英] How to force 'cp' to overwrite directory instead of creating another one inside?
问题描述
我正在尝试编写一个Bash脚本,该脚本将覆盖现有目录.我有一个目录foo/
,并且正在尝试用它覆盖bar/
.但是当我这样做时:
I'm trying to write a Bash script that will overwrite an existing directory. I have a directory foo/
and I am trying to overwrite bar/
with it. But when I do this:
cp -Rf foo/ bar/
创建一个新的bar/foo/
目录.我不要foo/
中有两个文件. a
和b
.在bar/
中也有相同名称的文件.我希望foo/a
和foo/b
替换bar/a
和bar/b
.
a new bar/foo/
directory is created. I don't want that. There are two files in foo/
; a
and b
. There are files with same names in bar/
as well. I want the foo/a
and foo/b
to replace bar/a
and bar/b
.
推荐答案
您可以使用cp
中的-T
选项执行此操作.
有关cp
,请参见手册页.
You can do this using -T
option in cp
.
See Man page for cp
.
-T, --no-target-directory
treat DEST as a normal file
因此,根据您的示例,以下是文件结构.
So as per your example, following is the file structure.
$ tree test
test
|-- bar
| |-- a
| `-- b
`-- foo
|-- a
`-- b
2 directories, 4 files
将-v
用于Verbose时,您可以看到明显的区别.
当您仅使用-R
选项时.
You can see the clear difference when you use -v
for Verbose.
When you use just -R
option.
$ cp -Rv foo/ bar/
`foo/' -> `bar/foo'
`foo/b' -> `bar/foo/b'
`foo/a' -> `bar/foo/a'
$ tree
|-- bar
| |-- a
| |-- b
| `-- foo
| |-- a
| `-- b
`-- foo
|-- a
`-- b
3 directories, 6 files
使用选项-T
时,它将覆盖内容,将目标像普通文件而不是目录一样处理.
When you use the option -T
it overwrites the contents, treating the destination like a normal file and not directory.
$ cp -TRv foo/ bar/
`foo/b' -> `bar/b'
`foo/a' -> `bar/a'
$ tree
|-- bar
| |-- a
| `-- b
`-- foo
|-- a
`-- b
2 directories, 4 files
这应该可以解决您的问题.
This should solve your problem.
这篇关于如何强制'cp'覆盖目录,而不是在内部创建另一个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!