Linux / Unix: Rsync Copy Hidden Dot Files and Directories Only

by on November 9, 2012 · 6 comments· LAST UPDATED February 6, 2014

in , , ,

How do I use the rsync tool to copy only the hidden files and directory (such as ~/.ssh/, ~/.foo, and so on) from /home/jobs directory to the /mnt/usb directory under Unix like operating system?

The rsync program is used for synchronizing files over a network or local disks. To view or display only hidden files with ls command:
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsrsync / bash or ksh
Estimated completion time2m


ls -ld ~/.??*

OR


ls -ld ~/.[^.]*

Sample outputs:

ls command: List only hidden files in Unix / Linux terminal

Fig:01 ls command to view only hidden files

rsync not synchronizing all hidden .dot files?

In this example, you used the pattern .[^.]* or .??* to select and display only hidden files using ls command. You can use the same pattern with any Unix command including rsync command. The syntax is as follows to copy hidden files with rsync:

 
rsync -av /path/to/dir/.??* /path/to/dest
rsync -avzP /path/to/dir/.??* /mnt/usb
rsync -avzP $HOME/.??* user1@server1.cyberciti.biz:/path/to/backup/users/u/user1
rsync -avzP ~/.[^.]* user1@server1.cyberciti.biz:/path/to/backup/users/u/user1
 

In this example, copy all hidden files from my home directory to /mnt/test:

 
rsync -avzP ~/.[^.]* /mnt/test
 

Sample outputs:

Rsync example to copy only hidden files

Fig.02 Rsync example to copy only hidden files

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 6 comments… read them below or add one }

1 Roc White November 11, 2012 at 3:23 am

Wouldn’t “.??*” leave out files like .a, .b, etc.?

Reply

2 ITN John November 11, 2012 at 6:28 am

Noop. Any details on ls version and Unix may help if you are not getting the same result.

ls -l ./.??*
-rw-r--r-- 1 John John    0 Nov 11 11:54 ./.a,
-rw-r--r-- 1 John John    0 Nov 11 11:54 ./.b,
-rw-r--r-- 1 John John    0 Nov 11 11:54 ./.ssh
./.rss:
total 0

Reply

3 TW November 12, 2012 at 4:09 am

root@node0 / # cd /tmp
root@node0 /tmp # ls -ld .??*
ls: cannot access .??*: No such file or directory
2 root@node0 /tmp # touch .a .b
root@node0 /tmp # ls -ld .??*
ls: cannot access .??*: No such file or directory
2 root@node0 /tmp # ls -ld .[^.]*
-rw-r–r–. 1 root root 0 Nov 11 23:07 .a
-rw-r–r–. 1 root root 0 Nov 11 23:07 .b
root@node0 /tmp # ls –version
ls (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.

Reply

4 Abdi March 1, 2013 at 5:14 am

I love your website – Keep up the good work!

Reply

5 qjwkqjwk August 20, 2013 at 8:55 pm

Maybe you are not using bash or a bash compatible shell?

Reply

6 Jordan Erickson September 11, 2013 at 3:37 am

Thanks!! Makes sense when you figure ~/.??* means anything that starts with a dot, has 2 more random chars, then everything after it (as to ignore . and .. ). Easy peasy, and will work for a million years! Thanks again!

Reply

Leave a Comment

Tagged as: , , , , , , , , , , ,

Previous Faq:

Next Faq: