Open In App

How to Copy a Directory in Linux

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

There is this famous quote for Linux by Jamie Zawinski that goes like this “Linux is only free if your time has no value.” Certainly, that’s true as people start using Linux they have to learn more about it, and when encountering it we have to solve it by ourselves, and that way we learn more about Linux.

In this article, we are going to learn how we can copy the directory in Linux. There are two ways to do this operation:

Let’s look into both of these operations to copy the directory in the Linux.

Method 1: How to Copy the directory using the GUI interface

The simplest way to copy the directory in Linux is by using the simple copy-and-paste options. Let’s understand this with an example, in this example, we are going to copy a directory into another directory.

Step 1. Go to the path in which you want to copy that specific directory. Here, we want to copy the ‘dir_1’ directory and paste it into the ‘dir_2’ directory.

Screenshot-from-2023-12-26-18-03-49

dir_1 that is in the test directory

Step 2. Now select the directory you want to copy. In the example, we are going to select the ‘dir_1’ directory so just right-click, and then click on copy option.

Screenshot-from-2023-12-26-18-08-16

The following will copy the whole directory and all the contents in it.

Step 3. Now, as we have already copied the directory it’s time to paste the directory somewhere that is into another directory of course. Just go to another directory where you want to copy the copied directory and its content.

After doing the above step, right-click in the directory and then click on the paste option. We are going to copy ‘dir_1’ and then paste it into the ‘dir_2’ directory.

Screenshot-from-2023-12-26-18-21-14

Step 4. The directory will be successfully copied and pasted.

Screenshot-from-2023-12-26-18-23-21

Method 2: How to Copy the directory using the Terminal

If you are using and working on Linux, you would already know how powerful and useful the terminal is. And, all the users of Linux must require to learn the art of using the terminal.

Now, let’s look into how we can copy the directory using the terminal.

Step 1. Open the terminal where your directory is located. Go to the path, right-click then choose the option ‘open in Terminal’.

$ ls

ls” command will list all the contents in the current directory.

Screenshot-from-2023-12-26-18-30-07

Terminal in the present working directory

Step 2. To copy the directory we have to use the specific command for that operation. The ‘cp’ command we are going to use to copy the directory into another directory.

cp command works as the following:

$ cp <dir_name_to_copy> <path_to_the_directory_where_to_copy>

Note: You need to be in the working directory, where the directory you want to copy exists.

If you want to know more about the usage of cp command, just type the following in the terminal:

$ man cp

The above command will open the manual for how to use cp.
Step 3. It’s time to copy the directory ‘dir_1’ into the directory ‘dir_2’. As we want to copy the ‘dir_1’ into ‘dir_2’. We have to know the path of ‘dir_2’.

Screenshot-from-2023-12-26-18-46-37

‘-r’ stands for recursive, it will copy the contents of the copied directory recursively. It is used when copying the folder.

Congratulations!!! we have copied a directory successfully using the terminal. Now, you are good to go to become a pro-Linux user.

How to Copy Multiple Directories in Linux

Till now we have learn how to copy a directory using terminal but the situation arises when you want to copy multiple directories and you want to use terminal for that operation. Let us learn how we can do this operation.

Step 1. Open the terminal where your directories are present. As in the image below i want to copy directories “test1, test2, and test3” and then paste them in the folder “gfg”.

$ ls

use “ls” command to list all the content.

Screenshot-from-2023-12-31-22-38-28

the folder that i want to copy

Step 2. To copy multiple directories we will use the same “cp” command but here comes the twist, as we want to select the multiple directories we have to pass those directories as an arguments in the command. Like this:

$ cp -r <dir1> <dir2> <dir3> <target location>


The above command we will use to copy the directories in the folder name “gfg

$ cp -r test1 test2 test3 /home/cosmos/Workspace/gfg


Then press enter, it will copy all the selected files in the target location.

Screenshot-from-2023-12-31-22-46-27

No output is the best output in bash

Step 3. let’s check it out if the directories are copied or not in the target directory that is “gfg“.

Screenshot-from-2023-12-31-22-49-36

Present working directory

Hurray!!! we have learned how to copy multiple directories using terminal. Now, you are good to go to become a Linux expert.

Conclusion

Using the terminal to copy files and folders makes the operations a lot more faster and efficient and for the Linux user, it is important that they know how to use the terminal efficiently and effectively.

Linux terminal is the powerful tool and hence it is important to know it’s working and the commands to get the task done more efficiently.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads