gpt4 book ai didi

Using rsync to rename files during copying with --files-from?(在复制过程中使用rsync重命名文件--files-from?)

转载 作者:bug小助手 更新时间:2023-10-26 21:26:07 29 4
gpt4 key购买 nike



Using rsync, how can I rename files when copying with the --files-from argument? I have about 190,000 files, each of which need to be renamed when copying from source to destination. I plan to have the list of files in a text file to pass to the --files-from argument.

使用rsync时,如何在使用--files-from参数复制时重命名文件?我有大约190,000个文件,在从源复制到目标时,每个文件都需要重命名。我计划将文件列表放在一个文本文件中传递给--files-from参数。


更多回答
优秀答案推荐

Not entirely true... you CAN rename files enroute with rsync, but only if you rsync one file at a time, and set the --no-R --no-implied-dirs options, then explicitly set the destination name in the destination path.

并不完全正确..。您可以使用rsync重命名路径中的文件,但前提是您必须一次对一个文件进行rsync,并设置--no-R--no-impline-dirs选项,然后在目标路径中显式设置目标名称。



But at that point, you may just want to use some other tool.

但在这一点上,你可能只想使用其他工具。



This, for example, would work:

例如,这将是可行的:



rsync --no-R --no-implied-dirs
1.2.3.4::module/$FILENAME
/$PATH/$TOFILE/$NEWFILENAME


There is no way to arbitrarily rename files with rsync. All rsync can do is move files to a different directory.

没有办法使用rsync任意重命名文件。Rsync所能做的就是将文件移动到另一个目录。



You must use a second tool either on the sending or receiving side to rename the files.

您必须在发送端或接收端使用另一个工具来重命名文件。



EDIT:

编辑:


Just realized that the question asks to actually rename the files for the transfer and does not try to solve the issue, that renamed files are copied even when they didn't changed (which was my concern in answer below).

我刚刚意识到,问题要求实际重命名要传输的文件,而不是试图解决问题,重命名的文件即使在没有更改的情况下也会被复制(这是我在下面的回答中关注的问题)。


For your question you can actually use an approach with the hard links. Instead of copying your source directory, create a new one as hard links to the same data using cp --recursive --link <source> <renamed> (the hard links do not take extra space on your disk) perhaps also with --no-dereference option to avoid following symbolic links.

对于您的问题,您实际上可以使用带有硬链接的方法。不是复制您的源目录,而是使用cp--递归--link<源><重命名>创建一个新的硬链接作为指向相同数据的硬链接(硬链接不会占用磁盘上的额外空间),或许还可以使用--no-dereference选项来避免遵循符号链接。


Then rename the files in the <renamed>/ directory as you wish, and upload them to the destination.

然后根据需要重命名 /目录中的文件,并将它们上传到目的地。


Original answer:

最初的答案是:


A think this trick using --hard-links should do exactly that.

A我认为这个技巧使用--硬链接应该能做到这一点。


Create a hardlink copy of your filenames within the source directory and upload it to the destination. Then rename the files as you please and upload the changes -- since the hardlinks are preserved, no data are actually transffered. Finally you can delete the directory with the hardlinks in your source, which will also vanish from the destination if you use --delete option in the rsync.

在源目录中创建文件名的硬链接副本,并将其上载到目标目录。然后随意重命名文件并上载更改--由于保留了硬链接,因此实际上不会传输任何数据。最后,您可以删除源文件中带有硬链接的目录,如果在rsync中使用--ete选项,它也会从目标文件中消失。


To illustrate it with a code look at the following output:

要用代码来说明这一点,请看以下输出:


############## the initial backup
$ rsync -av --hard-links --delete source backup/destination/
sending incremental file list
created directory backup/destination
source/
source/00_run
source/01_share
source/02_study
source/03_improve

sent 495 bytes received 137 bytes 1,264.00 bytes/sec
total size is 160 speedup is 0.25

############## make hardlinks to the old names at the source
$ cp --recursive --link {source,.oldnames} && mv .oldnames source

############## upload the hard links to the destination
$ rsync -av --hard-links --delete source backup/destination/
sending incremental file list
source/
source/.oldnames/
source/.oldnames/00_run => source/00_run
source/.oldnames/01_share => source/01_share
source/.oldnames/02_study => source/02_study
source/.oldnames/03_improve => source/03_improve

sent 373 bytes received 96 bytes 938.00 bytes/sec
total size is 320 speedup is 0.68

############## rename the files
$ cd source/
$ for f in *; do mv $f `echo $f | tr '[:lower:]' '[:upper:]'`; done
$ cd ..

############## upload the renamed files (the new names are linked to the old names)
$ rsync -av --hard-links --delete source backup/destination/
sending incremental file list
deleting source/03_improve
deleting source/02_study
deleting source/01_share
deleting source/00_run
source/
source/00_RUN => source/.oldnames/00_run
source/01_SHARE => source/.oldnames/01_share
source/02_STUDY => source/.oldnames/02_study
source/03_IMPROVE => source/.oldnames/03_improve

sent 409 bytes received 209 bytes 1,236.00 bytes/sec
total size is 320 speedup is 0.52

############## remove the links to the old names at the source and destination
$ rm -r source/.oldnames/
$ rsync -av --hard-links --delete source backup/destination/
sending incremental file list
deleting source/.oldnames/03_improve
deleting source/.oldnames/02_study
deleting source/.oldnames/01_share
deleting source/.oldnames/00_run
deleting source/.oldnames/
source/

sent 160 bytes received 153 bytes 626.00 bytes/sec
total size is 160 speedup is 0.51


更多回答

Actually, rsync is useful for a single file in some circumstances. If the file is large, and has either only had minor changes, or has only been appended to, it's a good fit.

实际上,rsync在某些情况下对单个文件很有用。如果文件很大,并且只做了很小的更改,或者只追加了一些内容,那么它非常适合。

According to man page, --no-implied-dirs only affects how --relative works. And --relative is the same as -R. But you use --no-R, which makes me wonders what effect --no-implied-dirs is supposed to have in that case?

根据手册页,--no-暗示-dis只影响--Relative的工作方式。和--相对等同于-R。但是您使用了--no-R,这让我想知道在这种情况下,no-insual-dirs应该会有什么效果?

What if we are moving the files into the same directory?

如果我们要将这些文件移到同一目录中,该怎么办?

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com