Sometimes, you might need to copy hidden files or folders from one location to another. In Linux, hidden files are characterized by a period (.) as the first character of their name. A common example would be copying a user's home directory or a directory with a website, as these directories often contain many hidden files.
Everything will work smoothly when copying a single hidden file. However, issues arise when trying to copy multiple files using the * wildcard pattern. This pattern doesn't account for the period (.) character, resulting in all hidden files being excluded from the copy process. This design choice was made to avoid unintentional recursion by skipping references to the current folder (.) and the parent folder (..).
Nonetheless, there are ways to circumvent this limitation. It is possible to copy the contents of a folder without using *. For example, let's create an app folder with .htaccess and index.php files and a backup folder where you want to copy the files. You can use the same link to the current folder and then cp will copy all content, including hidden files there to the desired folder:
cp -r app/. backup
Alternatively, you can also explicitly specify that you want to copy the contents of the folder passed in the first parameter by using the -T option:
cp -rT app backup
This command also requires the -r option, for recursive folder processing, as the first parameter specifies a folder, not a list of files.
Lastly, if you want to copy only hidden files from the directory you can use the following wildcard syntax:
cp app/.* backup
However, avoid adding the -r option to this command. If included, it will attempt to copy not only the specified directory but also all directories above it, including the system root, which can lead to unintended consequences.