[英]merge two folders and keeping the files have same name
I have multiple sources folder(these folders have a lot of files named such as ip address Ex: 192.168.2.1
), I want to merge them in a target folder.我有多个源文件夹(这些文件夹中有很多名为 ip 地址的文件,例如
Ex: 192.168.2.1
),我想将它们合并到一个目标文件夹中。
What is the ways of doing this operation on a Linux using terminal.使用终端在Linux上执行此操作的方法是什么。
Source 1来源 1
/Desktop/source1/192.168.2.1
/Desktop/source1/192.168.2.2
/Desktop/source1/192.168.2.3
Source 2来源 2
/Desktop/source2/192.168.2.1
/Desktop/source2/192.168.2.2
/Desktop/source2/192.168.2.3
Source 3来源 3
/Desktop/source2/192.168.2.1
Source 4来源 4
Source 5来源 5
Source 6来源 6
. .
. .
. .
Target目标
/Desktop/target/192.168.2.1
/Desktop/target/192.168.2.2
/Desktop/target/192.168.2.3
/Desktop/target/192.168.2.1.copy
/Desktop/target/192.168.2.2.copy
/Desktop/target/192.168.2.3.copy
/Desktop/target/192.168.2.1.copy.copy
original files have no file extension I just named them as what they are but I am opening them in gedit or any text editor.原始文件没有文件扩展名,我只是将它们命名为它们的名称,但我在 gedit 或任何文本编辑器中打开它们。 The duplicated file suffix might be ('192.168.2.3.copy or 192.168.2.3_2 or anything just needs to be different)
重复的文件后缀可能是('192.168.2.3.copy 或 192.168.2.3_2 或任何需要不同的东西)
What is the way of doing this operation with cp command, shell script or any other command in Linux?在 Linux 中使用 cp 命令、shell 脚本或任何其他命令执行此操作的方法是什么?
cp source1/* target/
cp -f --backup --suffix='.copy' source2/* target/
Just note that this will not add .copy
suffix to any files that are in source2
but not in source1
.只要注意这不会增加
.copy
后缀到在任何文件source2
而不是在source1
。 That is, .copy
will only be added for duplicate file names.也就是说,只会为重复的文件名添加
.copy
。
For multiple source folders, you can do something like:对于多个源文件夹,您可以执行以下操作:
cp source1/* target/
for i in {2..n} ; do
cp -f --backup=numbered source${i}/* target/
done
Replace n
with the your folder number.将
n
替换为您的文件夹编号。 This will put a .~1~
for the first copy, .~2~
for the second copy and so on.这将为第一个副本放置
.~1~
,为第二个副本放置.~2~
,依此类推。
The answer by Munir is perfect.穆尼尔的回答是完美的。 My reputation is too low to comment on his solution, but I would like to mention on it why this works:
我的声誉太低,无法评论他的解决方案,但我想提一下为什么这样做:
cp -f --backup --suffix='.copy' source2/* target/
Normally --backup will backup the file in the target folder, by appending a "~" at the end.通常 --backup 将备份目标文件夹中的文件,在末尾附加一个“~”。 The suffix option changes the "~" with copy.
后缀选项用复制改变“~”。 So why does this solution change the name of the source file and not the target?
那么为什么这个解决方案会更改源文件而不是目标文件的名称呢? It's the addition of the -f option in the line, which is a radically different behavior than what -f normally does.
这是在行中添加了 -f 选项,这与 -f 通常所做的行为完全不同。 This is documented in the last paragraph of the man page:
手册页的最后一段记录了这一点:
As a special case, cp makes a backup of SOURCE when the force and backup options are given and SOURCE and DEST are the same name for an existing, regular file.
作为一种特殊情况,当给定 force 和 backup 选项并且 SOURCE 和 DEST 与现有常规文件的名称相同时,cp 会备份 SOURCE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.