1

I've been trying to use rsync to simply copy all my TIF image files from the current folder onto a 'HR' folder under 'Documents' with no success so far, by the following command:

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

As I see it the -r option is the one used for recursive copying but said command only copies whatever TIF file there is on the current folder.

Could anyone please tell me where I went wrong with it?

Suffice to say,said current folder has lots of multilevel subfolders containing hundreds of TIF files.

  • The -r option is the one used for recursing into directories. Unless your current working directory contains a subdirectory matching the glob *.tif, you have not specified any directories into which to recurse. You'll want instead to recurse from the current directory, and use the --include and/or --exclude options to only include files matching *.tif as you recurse. – user4556274 Jul 10 '17 at 15:10
  • Similar request: https://unix.stackexchange.com/questions/76237/rsync-certain-files-excluding-the-rest-ignoring-svn-directory-recursively – user4556274 Jul 10 '17 at 15:19
  • @user4556274 Duplicate of https://unix.stackexchange.com/questions/2161/rsync-filter-copying-one-pattern-only . This is a very old problem. My answer there doesn't mention hide, but it doesn't have a significant benefit over the classic include/exclude method, so I'm closing this as a duplicate of the older thread. – Gilles 'SO- stop being evil' Jul 12 '17 at 00:22

2 Answers2

2

You have a wildcard (*) in your command line, which is expanded by the shell, so rsync sees it's been given a list of TIFF files in the current directory. For example your command line might be expanded to this:

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

None of these TIFF files is a directory so rsync has nowhere into which it can recurse.

On the other hand, if you try this, rsync can use the opportunity to find directories and recurse into them. I've used the -a flag to maintain the file metadata (permissions, timestamps). This attempt will copy all files, not just those ending with .tif, but if that's sufficient this is an easy solution to comprehend:

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

Now for the last part of the puzzle. If you want to copy only TIFF files (matching *.tif) then you need to tell rsync that although it can recurse it should copy only these files. This is the most complex part of an rsync command, but fortunately it's quoted almost exactly as an example in the manual page (see man rsync and search for "hide"):

rsync -am --include '*.tif' --filter 'hide,! */' . /home/myusername/Documents/HR/
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • It worked like a charm! Not to mention the -m option makes sure all subfolders with not TIF file are out of the table. I've added the -v for following things up more closely and voilá: just what I wanted!

    Also, thanks for the heads-up regarding the use of wildcards with rsync.

    – Scott Carey Jul 10 '17 at 15:45
  • @ScottCarey it's not just wildcards with rsync; it's how all wildcards are handled - the shell does wildcards so that tools don't need to. – Chris Davies Jul 10 '17 at 15:55
  • Silly newbie me. I already have.

    Probably, an equally silly question, but here it goes:

    none of the above options are supposed to delete anything on the source, right? I mean, the pruning of empty folders is only executed on the destination, correct?

    – Scott Carey Jul 10 '17 at 16:30
  • @ScottCarey see https://unix.stackexchange.com/a/375226/100397 – Chris Davies Jul 10 '17 at 17:06
-1

I usually read the man page before using a command.

So, when I use rsync, I use the -av options which is akin to -rlptgoDv, or -rlptgoD (-a) and -v (verbose).

EDIT (after your comment):

The exact same happened to me, maaaany moons ago, I read the USAGE section and sure enough, it is written there (AFTER THE VERY FIRST EXAMPLE): Note that the expansion of wildcards on the commandline (*.c) into a list of files is handled by the shell.... I also noticed that -r does not preserve dates and -v is also handy, I thought -av would be perfect.

From the man page:

[..]
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::
  [...]
thecarpy
  • 3,935
  • Using the man page - that's exactly what I did. Even getting to the point of copying and pasting the options, but with no avail. – Scott Carey Jul 10 '17 at 15:10