How To Copy Entire Directory In Linux?

Linux provides different tools to copy directories entirely. The most popular and well-known tool is cp. In this tutorial, we will look at how to copy Linux directories entirely or specific contents.

Copy Only Directory

In this first example, we will copy the only directory. We can just provide the source directory and destination directory names to the cp command. In this example, we will try to copy a single directory. As we can see we get an error like below.

$ cp backup newbackup

Copy Only Directory

Copy Entire Directory Recursively

Now here is the real scenario. We will copy our directory named backup with a new named newbackup. We will use the -r option which makes copy operation recursively. Recursive means subdirectories and files will be copied too.

$ cp -r backup newbackup

Copy Entire Directory Recursively

Copy Special Files

During recursive copy operation, special files content will not be copied by default. We can copy special file contents with the -b option like below.

$ cp -r -b backup newbackup

Copy While Preserving Source Attributes

Files and directories provide some attributes in Linux operating systems. These attributes are file mode, ownership, timestamp, context, links, xattr, etc. During directory copy, we can copy these attributes too with the -a option like below.

$ cp -r -a -v backup new_backup

Copy While Preserving Source Attributes

Copy Entire Directory To The Current User Home Directory

Linux bash shell provides some shortcuts about the paths. If we want to copy a directory completely into the current user home directory we can ~ tilde to specify the user home directory path. In this example, we will copy the directory named backup to the current user home directory.

$ cp -r backup ~/

Copy Verbosely

During copy operation, there will be no sign about the current operation. We can make this copy operation verbose where copied files will be printed to the shell. We will use -v command like below.

$ cp -r -v newbackup oldbackup

Copy Verbosely

Copy Entire Directory with rsync Command

rsync is very popular and useful tools used to copy, sync and backup files and directories. We can use rsync in order to copy the complete directory with its subdirectories locally or to the remote systems. We will use -r options to copy all directories and files recursively and also use -v option for verbose output.