Skip to content
Home » Unix » How to Copy Hidden File and Directory

How to Copy Hidden File and Directory

Hidden File and Directory

First of all, let's see what we have in the directory.

[erp@test ~]$ ls -al /backup/reports
total 136
drwxrwxr-x 3 erp erp    91 Jan 13 00:16 .
drwxr-xr-x 4 erp erp    43 Jan 13 00:16 ..
drwxrwxr-x 2 erp erp    32 Jan 13 00:09 .batch_env
-rw-r--r-- 1 erp erp   141 Nov 10  2019 .batch_program

-rw-rw-r-- 1 erp erp  9790 Jan 13 00:11 report1
-rw-rw-r-- 1 erp erp 22277 Jan 13 00:11 report2
-rw-rw-r-- 1 erp erp 98094 Jan 13 00:11 report3

As we can see, there're a hidden directory and a hidden file with a leading period in their file name.

Then we try to copy them to another place by using a wildcard character (* asterisk) to represent everything.

[erp@test ~]$ cp -rp /backup/reports/* /backup/reports_backup/

Then we check the copied content.

[erp@test ~]$ ls -al /backup/reports_backup/
total 132
drwxrwxr-x 2 erp erp    51 Jan 13 00:13 .
drwxr-xr-x 4 erp erp    43 Jan 13 00:09 ..
-rw-rw-r-- 1 erp erp  9790 Jan 13 00:11 report1
-rw-rw-r-- 1 erp erp 22277 Jan 13 00:11 report2
-rw-rw-r-- 1 erp erp 98094 Jan 13 00:11 report3

Apparently, not everything is copied to the backup location. The hidden directory and file are not copied. Did we miss something when using a wild card?

Solution

To represent real everything in the directory, we should use a period (.) instead of a wild card (*).

[erp@test ~]$ cp -rp /backup/reports/. /backup/reports_backup/
[erp@test ~]$ ls -al /backup/reports_backup/
total 136
drwxrwxr-x 3 erp erp    91 Jan 13 00:16 .
drwxr-xr-x 4 erp erp    43 Jan 13 00:16 ..
drwxrwxr-x 2 erp erp    32 Jan 13 00:09 .batch_env
-rw-r--r-- 1 erp erp   141 Nov 10  2019 .batch_program

-rw-rw-r-- 1 erp erp  9790 Jan 13 00:11 report1
-rw-rw-r-- 1 erp erp 22277 Jan 13 00:11 report2
-rw-rw-r-- 1 erp erp 98094 Jan 13 00:11 report3

Hidden file and directory are copied.

Leave a Reply

Your email address will not be published. Required fields are marked *