如何强制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. So, I have a directory foo/ and I am trying to overwrite bar/ with it. But when I do,
cp -Rf foo/ bar/
会发生什么是创建一个新的bar / foo /目录。我不想要。 foo / a和b中有两个文件。在bar /中也有相同名称的文件。我想要foo / a和foo / b替换bar / a和bar / b。
what happens is that 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.
推荐答案
您可以使用 -T
$ c> cp 。
请参阅 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
作为详细信息时,您可以看到明显的区别。
当您使用 -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屋!