Just like files in Linux, the directories can be moved from one location using different methods. This guide will enlighten you on the method of copying a directory in Linux.
How can you Copy a Directory in Linux?
You will have to follow the method described below to copy a directory along with its contents to another directory in Linux:
Step # 1: Learn the Usage of the “cp” Command in Linux
The “cp” command in Linux is used for copying a directory from one location to another. However, before copying our desired directory to another, we will first learn the usage of the “cp” command by running the following command:
$ cp --help
This command will display the help manual of the “cp” command on your terminal from where you will be able to learn all the options that can be used with the “cp” command as shown in the image below:
Step # 2: List the Contents of the Directory that you Need to Copy to Another Directory
Now, we would like to show you the contents of the directory which we wish to copy to another. For that, we have executed the following command:
$ ls /home/system/Directory1
The contents of our directory “Directory1” are shown in the image below. This output will help us in verifying the correctness of the copying process.
Step # 3: Copy the Target Directory to the Directory of your Choice
We will use the following command for copying the directory “Directory1” to another directory “Directory2”:
$ cp –r /home/system/Directory1 /home/system/Directory2
This command will recursively copy all the contents of the target directory along with the directory itself to the other directory. However, no output will appear on the terminal as shown in the image below:
Step # 4: List the Contents of the Latter Directory
Now, to verify if “Directory1” has been successfully copied to “Directory2”, we will list the contents of “Directory2” with the help of the following command:
$ ls /home/system/Directory2
The output shown in the image below implies that “Directory1” now resides inside “Directory2”. However, we are still unsure about whether the contents of “Directory1” have successfully been copied to “Directory2” or not. Therefore, we will also perform the next step.
Step # 5: Expand the Contents Listing of the Latter Directory to Ensure the Correctness of the Copying Process
In this step, we will recursively list the contents of the directory residing inside “Directory2” i.e., “Directory1” to know whether the contents of the latter directory have successfully been copied or not. For that, we will run the following command:
$ ls –R /home/system/Directory2
The output shown in the image below verifies that the copying process has taken place successfully i.e., “Directory1” has been copied to “Directory2” along with all of its contents.
This article was meant to guide you about the method of copying a directory in Linux. After learning this method, you will conveniently be able to copy directories from one location to another.