Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use rsync to copy only hidden files

Tags:

macos

rsync

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.

like image 321
Sophie Gage Avatar asked Mar 30 '09 19:03

Sophie Gage


People also ask

Will rsync copy hidden files?

Thus, rsync never receives the hidden files as arguments. So the solution is to use entire directory name (instead of asterisk) as argument to rsync command. Note: The trailing slashes at the end of both paths. Any other syntax may lead to unexpected results!

Does rsync include dot files?

If we just add the directory itself as a parameter, no globbing takes place, and rsync even copies dotfiles.

Does rsync ignore existing files?

Rsync with --ignore-existing-files: We can also skip the already existing files on the destination. This can generally be used when we are performing backups using the –link-dest option, while continuing a backup run that got interrupted. So any files that do not exist on the destination will be copied over.

Does rsync copy everything?

If you want to copy a directory with its sub-directory and all contents from one location to another within your system, you can do so as by typing rsync followed by the source and destination directory. Note: Specifying “/” after the source directory only copies the contents of the directory.


2 Answers

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.

like image 53
sth Avatar answered Oct 21 '22 02:10

sth


It's usually ".??*" to make sure you don't copy "." and ".."

(What if you had a file that was just ".a" ?)

like image 31
Martin Beckett Avatar answered Oct 21 '22 03:10

Martin Beckett