Skip Hidden Files and Directories During Recursive Copy


On the Linux command line, cp -r can be the first command that appears when we want to copy directories recursively. Dotfiles are understood to function under Linux as hidden files. We occasionally exclude hidden files and directories from directories while copying them recursively.

We'll learn how to do it in this tutorial.

Note − Linux commands are case-sensitive.

SCP command

Using the command-line tool SCP (secure copy), you can safely move files and directories between two places. We can copy a file or directory with scp from a local system to a remote system, from a remote system to a local one, and between two remote systems and your local system.

Run the following command to copy a file from a local system to a remote one −

$ scp  script.txt  remote_username@


tutorials@'s password:
script.txt                    100%          0             0.0KB/s          00:00

Here the name of the file we want to copy is “script.txt”, the user on the remote server is tutorials, and the server's IP address is The path to the directory you wish to copy the file to is in the /remote/directory field. The file will be copied to the remote user's home directory if a remote directory is not specified.

rsync command

A helpful Linux command-line utility that copies and syncs files and directories is called rsync. The utility can be used to synchronize data between two remote machines or locally between directories and discs.

In Linux, the fundamental rsync commands sync everything from the location you specify. You might want to exclude particular files, folders, or file types in numerous backup scenarios.

We can instruct the -exclude option to ignore hidden files and directories by passing it the ".*" pattern. We should point out that the ".*" pattern in this case is not a regex. Instead, it denotes any directory or filename that starts with a dot.

In the below example, we will copy all file recursively from ~/sample/ directory but exclude all *.backup files −

$ rsync -av -e ssh --exclude='*.backup' ~/sample/ root@fedora:/tmp


sending incremental file list

sent 1,145,974,357 bytes received 137 bytes  45,904,896.00 bytes/sec
total size is 1,145,674,357  speedup is 1.00 

As you can see the output above, all the files have been recursively copied and doesn’t contains “.backup” files.


In this tutorial, we learned some examples of how to skip hidden files and directories during recursive copy in Linux. We discussed how to replicate directories recursively in this article while omitting hidden files and directories.

The method of copying everything and then removing hidden items is simple. It might, however, result in security holes and performance issues. Any Linux user can quickly repair this problem. Feel free to attempt any method offered by Linux to solve this issue in order to eliminate this error. The procedures outlined above work with various Linux distributions. All operating systems must have file descriptors as a fundamental element.

I hope you find these examples of the commands useful.