How To Copy Entire Directory In Linux? – POFTUT

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.

Copy Only Directory

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.

Copy Entire Directory Recursively

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.

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.

Copy While Preserving Source Attributes

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.

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.

Copy Verbosely

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.

Copy Entire Directory with rsync Command

Copy Entire Directory with rsync Command

Leave a Reply

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

Enjoy this blog? Please spread the word :)