Copy hidden files and folders in Ubuntu

We can copy hidden files and folders in Ubuntu distribution through cp command-line utility. In addition to, other cp command-line utility options would also be discussed. It is worth mentioning here that, cp command only copies files (not folders) by default.

Furthermore, we would explain different combinations with the help of examples. Lets say we have source directory Source_DIR and Destination directory as Dest_DIR. Destination directory contains a random directory and file. Whereas, source directory contains a hidden file and directory. It also contains a normal file and directory. Observe, hidden file or directory names begin with . (a dot) sign.

.
├── Dest_DIR
-     ├── Random_DIR
-     └── Random_File
└── Source_DIR
-     ├── Directory_1
-     ├── File_1
-     ├── .Hidden_DIR
-     └── .Hidden_file
I. To copy all the contents (hidden or otherwise) of a directory to other directory
cp -r /path/to/Source_DIR/. /path/to/Dest_DIR/

This would copy all the contents inside Source_DIR to Dest_DIR and get us the outcome –

.
├── Dest_DIR
-     ├── Directory_1
-     ├── File_1
-     ├── .Hidden_DIR
-     ├── .Hidden_file
-     ├── Random_DIR
-     └── Random_File
└── Source_DIR
-     ├── Directory_1
-     ├── File_1
-     ├── .Hidden_DIR
-     └── .Hidden_file

where,

-r is used to copy directories recursively.

II. To copy Source directory inside Destination directory
cp -r /path/to/Source_DIR/ /path/to/Dest_DIR/

This would copy Source_DIR and its contents inside Dest_DIR.

.
├── Dest_DIR
-     ├── Random_DIR
-     ├── Random_File
-     └── Source_DIR
-          ├── Directory_1
-          ├── File_1
-          ├── .Hidden_DIR
-          └── .Hidden_file
└── Source_DIR
-     ├── Directory_1
-     ├── File_1
-     ├── .Hidden_DIR
-     └── .Hidden_file
III. To copy without creating Source directory inside Destination directory

In II, we copied entire Source directory to Destination directory. But, in case we don’t want Source_DIR to be created inside Dest_DIR. Then, issue the following in terminal –

cp -rT /path/to/Source_DIR/ /path/to/Dest_DIR/

where,

-T is for no target directory.

.
├── Dest_DIR
-     ├── Directory_1
-     ├── File_1
-     ├── .Hidden_DIR
-     ├── .Hidden_file
-     ├── Random_DIR
-     └── Random_File
└── Source_DIR
-     ├── Directory_1
-     ├── File_1
-     ├── .Hidden_DIR
-     └── .Hidden_file

In conclusion, we have discussed how to copy hidden files and folders in Ubuntu distribution with the help of an example.

Additional Info

We have used the command-line utility tree to get the contents of directories. With tree, we don’t have to get into each of directories to check for the relevant changes made. Since, the package isn’t installed by default for Ubuntu distribution. Therefore –

Note: Following operations would require you to have superuser privileges. In case you don’t have one, then contact your System Administrator for assistance.

To install tree command-line utility, issue the following in terminal –

sudo apt update
sudo apt install tree

Now, to check for a specific directory –

tree /path/to/list/

For instance,

tree /home/

By default, tree command-line utility doesn’t list hidden files or directories. Therefore, to list even those hidden files/directories –

tree -a /path/to/list

where,

-a to print all files/directories.

For instance,

tree -a /home/

We would discuss more about tree in subsequent articles.