[英]rsync - copy files with same name
I have some different files with the same name and I want to copy all of them to the destination which has a flat structure (no directories, just files), is there any way to append some text onto one of the file names so that both can be copied. 我有一些同名的不同文件,我想将所有文件都复制到结构平坦的目的地(没有目录,只有文件),有什么办法可以在其中一个文件名后附加一些文本,以便两者可以复制。
Need to use rsync because there are some files that I need to exclude from the copy. 需要使用rsync,因为有些文件需要从副本中排除。
For example: dir1/file1.txt
dir1/dir2/file1.txt
both get copied, and in the destination there is: file1.txt
file1.txt.txt
例如:
dir1/file1.txt
dir1/dir2/file1.txt
都被复制,并且在目标位置有: file1.txt
file1.txt.txt
typically, when I want to do some complex name-mungling, I just write the list of files (with find dir1 >listfiles
) and fix it with a text editor. 通常,当我想进行一些复杂的名称交换时,我只需编写文件列表(使用
find dir1 >listfiles
)并使用文本编辑器对其进行修复。
for example, s/^.*\\/([^\\/]+)$/cp \\0 destination/\\1/
converts a file like 例如,
s/^.*\\/([^\\/]+)$/cp \\0 destination/\\1/
转换为
dir1/file1.txt
dir1/dir2/file1.txt
to a script like: 像这样的脚本:
cp dir1/file1.txt destination/file1.txt
cp dir1/dir2/file1.txt destination/file1.txt
then you could do something like cut -f 3 <listfiles | sort | uniq -d
那么您可以执行诸如
cut -f 3 <listfiles | sort | uniq -d
cut -f 3 <listfiles | sort | uniq -d
cut -f 3 <listfiles | sort | uniq -d
to find those with the same destination filename. cut -f 3 <listfiles | sort | uniq -d
查找具有相同目标文件名的文件。 then go back to the editor and fix those lines. 然后回到编辑器并修复这些行。
After a few minutes you get a full script for exactly the copy you want, without surprises because you can see each command and apply the best fix for each case. 几分钟后,您将获得所需副本的完整脚本,这毫不奇怪,因为您可以看到每个命令并针对每种情况应用最佳解决方案。
As far as i know there is no default option in rsync to do that. 据我所知,rsync中没有默认选项可以做到这一点。 But i guess that since you are copying files with the same name but from different directories, you are using multiple rsync commands.
但是我猜想,因为您要从不同目录复制相同名称的文件,所以您正在使用多个rsync命令。
So, this gives you two options: 因此,这给您两个选择:
Create folders.. 创建文件夹..
rsync -av /home/user1/file1 /media/foo/user1/file1
rsync -av /home/user2/file1 /media/foo/user2/file1
etc..
or rename the files with an id 或使用ID重命名文件
rsync -av /home/user1/file1 /media/foo/parent_dir-file1
rsync -av /home/user2file1 /media/foo/parent_dir-file1
etc..
If you want to use the second solution you can build a simple script. 如果要使用第二种解决方案,则可以构建一个简单的脚本。 As you are using rsync i suppose that you know the basics on GNU-Linux, so a simple bash script would be enough!
当您使用rsync时,我想您已经了解GNU-Linux的基础知识,所以一个简单的bash脚本就足够了!
A basic ID is to get the parent folder name and add it as variable to the path of the rsync
command. 基本ID是获取父文件夹名称并将其作为变量添加到
rsync
命令的路径。 ( it won't always work ) (它并不总是有效的)
IF you want to be sure of a good id you can for example set a counter and increment like 如果您想确定一个好的ID,可以设置一个计数器,例如
file1-1
file1-2
file1-3
But you will loose the track of its absolute path. 但是,您将松开它的绝对路径。
All the solutions can work, its up to you to choice the one that feed your needs! 所有解决方案都可以使用,由您选择满足您需求的解决方案!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.