TECHNOLOGY

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

While working in a few working frameworks, we use to add records and organizers, cut, duplicate, alter and erase them according to our decision. Very much like every one of these, the Linux framework gives us the chance to duplicate a solitary organizer to another envelope despite the fact that the organizer is vacant or not. Subsequently, we will talk about a few simple strategies to duplicate one catalog to one more in Ubuntu 20.04 framework. We should have a new beginning by utilizing the various ways of replicating an index starting with one spot then onto the next in Linux.
For this specific reason, we should open up the terminal shell as we will do everything in it. There are two methods for opening it. One is from the action search region and the other is the alternate way key “Ctrl+Alt+T”. Practice one of the referenced strategies to open it and begin carrying out the orders in it individually.

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.

Related Articles

Leave a Reply

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

Back to top button