谁能告诉我为什么 rsync 不从源的子文件夹中复制文件?

谁能告诉我为什么 rsync 不从源的子文件夹中复制文件?

我一直试图rsync通过以下命令将所有 TIF 图像文件从当前文件夹复制到“文档”下的“HR”文件夹中,但到目前为止没有成功:

rsync -r *.tif /home/myusername/Documents/HR

正如我所见,该-r选项是用于递归复制的选项,但该命令仅复制当前文件夹中的任何 TIF 文件。

谁能告诉我我哪里出了问题吗?

可以说,当前文件夹有很多多层子文件夹,其中包含数百个 TIF 文件。

答案1

您的命令行中有一个通配符 ( *),该通配符由 shell 扩展,因此rsync可以看到它已获得当前目录中的 TIFF 文件列表。例如,您的命令行可能会扩展为:

rsync -r cry.tif frown.tif smile.tif /home/myusername/Documents/HR

这些 TIFF 文件都不是目录,因此rsync无法递归到其中。

另一方面,如果您尝试这样做,rsync可以利用这个机会查找目录并递归到它们中。我使用该-a标志来维护文件元数据(权限、时间戳)。此尝试将复制所有文件,而不仅仅是那些以 结尾的文件.tif,但如果足够的话,这是一个易于理解的解决方案:

rsync -a . /home/myusername/Documents/HR/

现在是谜题的最后一部分。如果您只想复制 TIFF 文件(匹配*.tif),那么您需要告诉rsync它,虽然它可以递归,但它应该只复制这些文件。这是命令中最复杂的部分rsync,但幸运的是,它在手册页中几乎完全作为示例引用(请参阅man rsync并搜索“隐藏”):

rsync -am --include '*.tif' --filter 'hide,! */' . /home/myusername/Documents/HR/

答案2

我通常阅读男人使用命令之前的页面。

因此,当我使用 时rsync,我使用-av类似于-rlptgoDv、 或-rlptgoD( -a) 和-v(verbose) 的选项。

编辑(在您发表评论后):

完全一样的事情发生在我身上,maaaany 几个月前,我读了用法部分,果然,它写在那里(在第一个例子之后):Note that the expansion of wildcards on the commandline (*.c) into a list of files is handled by the shell...。我还注意到它-r不保留日期而且-v也很方便,我认为-av这是完美的。

从手册页:

[..]
USAGE
   You use rsync in the same way you use rcp. You must specify a source and a destination, one of which may be remote.

   Perhaps the best way to explain the syntax is with some examples:

          rsync -t *.c foo:src/

   This  would  transfer  all  files matching the pattern *.c from the current directory to the directory src on the machine foo. If any of the files already exist on the remote system then the rsync remote-update protocol is used to update the file by sending
   only the differences in the data.  Note that the expansion of wildcards on the commandline (*.c) into a list of files is handled by the shell before it runs rsync and not by rsync itself (exactly the same as all other posix-style programs). 

          rsync -avz foo:src/bar /data/tmp

   This would recursively transfer all files from the directory src/bar on the machine foo into the /data/tmp/bar directory on the local machine. The files are transferred in "archive" mode, which ensures that symbolic links, devices, attributes,  permissions,
   ownerships, etc. are preserved in the transfer.  Additionally, compression will be used to reduce the size of data portions of the transfer.

          rsync -avz foo:src/bar/ /data/tmp

   A  trailing slash on the source changes this behavior to avoid creating an additional directory level at the destination.  You can think of a trailing / on a source as meaning "copy the contents of this directory" as opposed to "copy the directory by name",
   but in both cases the attributes of the containing directory are transferred to the containing directory on the destination.  In other words, each of the following commands copies the files in the same way, including  their  setting  of  the  attributes  of
   /dest/foo:

          rsync -av /src/foo /dest
          rsync -av /src/foo/ /dest/foo
   Note also that host and module references don’t require a trailing slash to copy the contents of the default directory.  For example, both of these copy the remote directory’s contents into "/dest":

          rsync -av host: /dest
          rsync -av host::module /dest

   You can also use rsync in local-only mode, where both the source and destination don’t have a ’:’ in the name. In this case it behaves like an improved copy command.

   Finally, you can list all the (listable) modules available from a particular rsync daemon by leaving off the module name:

          rsync somehost.mydomain.com::
  [...]

相关内容