Linux Commands

How to Copy Directory in Linux: (Beginner to Advance)

While working in several operating systems, we use to add files and folders, cut, copy, edit and delete them as per our choice. Just like all these, the Linux system provides us the opportunity to copy a single folder to another folder even though the folder is empty or not. Therefore, we will discuss some easy methods to copy one directory to another in Ubuntu 20.04 system. Let’s have a fresh start by using the different ways to copy a directory from one place to another in Linux.

For this particular purpose, we must open up the terminal shell as we are going to do all things in it. There are two ways to open it. One is from the activity search area and the other is the shortcut key “Ctrl+Alt+T”. Practice one of the mentioned methods to open it and start implementing the commands in it one by one.

Example 01

Firstly, we need to see all the files and directories held in our home folder of Ubuntu 20.04. This process can be done with the help of a list command. The list command contains only a single word “ls”. Execute it and you will be shown with the home folder directories and files as per the image attached.

Now, we are creating a new directory in our home directory to perform copy operations after some time. The Ubuntu 20.04 system provides us with the “mkdir” query to create a new empty directory. So, we have used the “mkdir” command in our command shell and named the directory as “new”. The command will be executed with the help of an Enter key.

Imagine, one directory is not enough for you to perform copy operations for better understandability. So, we have again utilized the “mkdir” command with the new directory name “test”. After the directory creation, we have listed the home directory contents. The list “ls” command is clearly showing the addition of 2 new directories in the home folder i.e. new and test.

Let’s start copying a directory to another folder. For this, the Linux system came up with the shortcut copy command i.e., “cp” keyword along with some of its flag options. The command contains the folder to be copied and the location where it should be pasted. The syntax of this command is shown in the stated query.

$ cp –r directory/ path-to-destination-folder/

Within the below image, you can take a glance that the cp command contains the “-r” flag along with the source folder (to be copied) and the destination folder (path to the folder in which it will be pasted). The “new” folder will be copied to the “Documents” folder. The same “cp” command is used to copy the “test” folder to the “new” folder inside the “Documents” folder. Both the commands perform the same thing but at different levels.

Let’s navigate within the Documents folder of our system first via the “cd” instruction in the shell. After the navigation towards the Documents folder, we have listed its contents via the “ls” command. You can see that the “new” folder is successfully copied.

Let’s perform the navigation again with a 1 level up this time using the “cd” command. We are successfully navigated in the Documents/new folder. After listing the contents of the “new” folder, we have got to know that it has successfully copied the “test” folder within it.

Example 02

Let’s have a look at copying a file into a directory. So, we have created a new empty file “new.txt” and listed the home folder contents with the “ls” instruction. It shows that the file is created, new, and the “test” folder is also there.

The copy command is used the same way without the “-r” flag. A single difference is that the file name doesn’t contain any “/” sign at the end of it. The destination folder is also specified.

After navigating within the “new” folder and listing its contents, we got to know that the copy operation was successful at the simple file as well.

Example 03

Let’s take a look at another illustration of copying multiple contents folder to another location in Linux. So, we have started it with the navigation towards the “Music” folder of the home directory. After navigation, the list command is utilizing the contents of the Music folder i.e. empty.

After that, we have navigated towards the Documents folder and got to know that it contains two text files in it as well via the list command.

Now, we have been using the same way of copying the data but with the “-avr” flag in the cp command this time. So, we have to mention the source location i.e. “/home/saeedraza/Documents”. After this, you need to add the destination folder location i.e. “/home/saeedraza/Music. It will take the contents from the Documents folder and save them to the Music folder one by one.

After running the list command while navigating within the Music folder, we have got to know that the Documents folder is here now.

After navigating within the “Music/Documents” folder, we have got to know that all the contents i.e., “new1.txt”, “new2.txt” of the “Documents” folder are also copied within the “Music” folder along with the “Documents” folder.

Example 04

Let’s say you have created a new file in the home folder, and listed its data in the shell with the cat commands as per the below image output.

The cp command is here with the “-v” flag to copy the contents of a new.txt file to another file “test.txt” file. The test.txt file will be automatically generated with the use of this command. On looking at the test.txt file with the cat command, we have found that the contents are copied.

Conclusion

This article contains a tremendous amount of commands to copy a folder to another location. We have tried to cover the copying of a single folder to another location, copying a single file to another location and other files, and many more. We are hoping that this article will be quite simple to understand and easy to implement for every user of the Ubuntu 20.04 system.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.

Page was generated in 0.059543132781982