在使用 --files-from 复制期间使用 rsync 重命名文件?

发布于 2024-08-02 11:07:49 字数 117 浏览 5 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仄言 2024-08-09 11:07:49

不完全正确...您可以使用 rsync 重命名途中的文件,但前提是一次 rsync 一个文件,并设置 --no-R --no-implied-dirs 选项,然后在目的地路径。

但那时,您可能只想使用其他工具。

例如,这将起作用:

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

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.

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
作死小能手 2024-08-09 11:07:49

无法使用 rsync 任意重命名文件。 rsync 所能做的就是将文件移动到不同的目录。

您必须在发送方或接收方使用第二个工具来重命名文件。

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

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

云雾 2024-08-09 11:07:49

编辑:

刚刚意识到问题要求实际重命名要传输的文件,并且没有尝试解决问题,即使重命名的文件没有更改,也会复制它们(这是我关心的问题)答案如下)。

对于您的问题,您实际上可以使用带有硬链接的方法。不要复制源目录,而是使用 cp --recursive --link

创建一个新目录作为指向相同数据的硬链接。 <重命名>(硬链接不会占用磁盘上的额外空间)也许还可以使用 --no-dereference 选项来避免遵循符号链接。

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

原始答案:

A认为使用--hard-links的技巧应该可以做到这一点。

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

为了用代码来说明它,请看以下输出:

############## 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

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.

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文