Linux / Unix Rsync Copy Hidden Dot Files and Directories Only

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
Difficulty level Easy
Root privileges No
Requirements rsync / bash or ksh
Est. reading time 2m
ls -ld ~/.??*
ls -ld ~/.[^.]*
Sample outputs:

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/.??*
rsync -avzP ~/.[^.]*

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

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

Sample outputs:

Fig.02 Rsync example to copy only hidden files

Rsync Copy Hidden Dot File Command Options

The options are as follows:

  1. -a – Archive mode copy. In this mode all given files are copied in recursive mode. Copy symlinks as symlinks, file permissions, date and time stapms, and much more.
  2. -v – Verbose copy. In other words, show what the rsync is doing.
  3. -z – Compress file data during the transfer.
  4. -P – Display progress bar


You learned how to to use the rsync command to copy hidden dot files including directories. See rsync man page here for more info or type the following man command:
man rsync

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 6 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • Roc White Nov 11, 2012 @ 3:23

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

    • ITN John Nov 11, 2012 @ 6:28

      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
      total 0
      • TW Nov 12, 2012 @ 4:09

        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.

  • Abdi Mar 1, 2013 @ 5:14

    I love your website – Keep up the good work!

  • qjwkqjwk Aug 20, 2013 @ 20:55

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

  • Jordan Erickson Sep 11, 2013 @ 3:37

    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!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum