Search code examples
macosrsync

Use rsync to copy only hidden files


I want to back up all the hidden files and directories in my homedir using rsync, but not the non-hidden files and directories.

For example, given this directory listing:

drwxr-xr-x   7 sophie  sophie  238 31 Mar 08:45 .
drwxr-xr-x  15 sophie  sophie  510 31 Mar 08:14 ..
-rw-r--r--   1 sophie  sophie    4 31 Mar 08:12 .foo
drwxr-xr-x   3 sophie  sophie  102 31 Mar 08:45 .hiddendir
drwxr-xr-x   4 sophie  sophie  136 31 Mar 08:13 VisibleDirectory
-rw-r--r--   1 sophie  sophie    9 31 Mar 08:13 VisibleFile

I want to back up .foo, .hiddendir, and all the contents of .hiddendir whether they are hidden or not. I don't want to back up VisibleDirectory or VisibleFile.

All the incantations I have come up with back up ".", and therefore all its contents including VisibleFile and VisibleDirectory, and I can't figure out how to exclude it. Please help!

I'm using Mac OS X 10.5.6 (Leopard) and rsync version 2.6.9 protocol version 29.


Solution

  • A common pattern to match the hidden items is .[^.]*

    rsync -a ~/.[^.]* /path/to/backup
    

    This copies all files starting with a single dot. Note that it doesn't include files starting with more than one dot.