How to Copy Directory in Linux

Copy Directory in Linux is the process that every Linux user must know. However, It is not a big deal to copy and paste in any operating system either it is Linux, Windows, or Mac. But for the users who don’t know the Linux command to copy files or directory could be problematic work to do.

So, here in this post you will not just learn how to copy files or directory from one location to another location in Linux but also your are going to learn many other related terms.

Copy Directory in Linux (Copy and Paste in Linux)

In order to copy Directory in Linux operating system, there is a command-line utility called “cp” command. For copying Directory in Linux, you will have to execute the “cp” command.

Using the cp command you can copy the file on the same location with a different name or you can copy the file on another location with the same name or a different name.

Copy Directory from One Location to Another Location In Linux

As I told you you can copy Linux directory from one location to another location or on the same location also.

Here is how to copy Directory in Linux-

  • Check your current directory using pwd command.
  • Now get to the location where the directory exists which you want to copy.
  • now run cp -r command followed by the directory name you want to copy along with the location where you want to place the copied directory.

Syntax-

$ cp -r <source_dir> <destination_dir>

Example-

$ cp -r dir1 /home/user/Public

In this example, I am copying the dir1 which is on my Desktop. dir1 is being copied from Desktop to Public Directory.

Copy Directory in Linux

Explanation about the Image –

In this image, you can see the proper use of the “cp -r” command. In the image, I have copied my Project Directory from Desktop to Public Directory.

Note: When you use the “cp” Linux command to copy directory then It is important to use “-r” option for recursive. Otherwise, you will get the following error message-

$ cp: -r not specified; omitting directory 'DirectoryName'

Copy Directory in Linux with Different Name

Sometimes you might need to copy a directory in Linux with different name.

Till now, you have learned how to copy but can you guess how could you copy along with changing the name?

Guessing??

Well, It is quite easy..

Run cp -r command followed by the directory name (that you want to copy), then place the path followed by a New Directory Name.

$ cp -r dir /home/user/Public/dir2

When the above command will be run, directory dir1 will be copied into the Public directory with dir2 name.

Is not it an easy process?

Copy Linux Directory on the Same Location

Sometimes you may need to create a duplicate directory of any other directory. Doing it very easy in Gnome’s GUI but when it comes to doing it with a command-line interface then it could be difficult for many people.

However, Coping directory on the same location in Linux is the easiest process.

Here is the code-

$ cp -r dir1 dir2 

Here dir1 is being copied at the same location but a different name that is dir2.

It was how to copy directory recursively in Linux operating system.

What’s Next?

Lets learn how to copy multiple folders or directories in in Linux.

Learn how to remove or delete directory in Linux

Copy Multiple Directories in Linux using cp command

It is as simple to copy multiple directories in Linux as coping single directory but yes you will have to use a little bit different command.

To copy multiple directories use “cp” command followed by the list of the directory names you want to copy separated by a blank space and then place the destination.

Could not understand?

Have a look on the syntax-

$ cp -r <source_dir1> <source_dir2> <source_dir3>...<source_dirN> <destination_dir>

Let’s take an example, suppose there are four directories on my computer that are Project, Project2, Project3, and Project4. These all directories are on the Desktop of my computer. I want to copy it to the Public folder(directory) of my PC.

Here is the command that I will have to run in order to copy all four directories-

$ cp -r Project Project2 Project3 Project4 /home/factforgeeks/Public

Here, factforgeeks is the username of my computer. In your case, you will have to place the username of your computer.

copy directory in Linux
Copy Multiple Directories in Linux

What if you want to copy all the files of a Directory into Other Directory In Linux?

It mean you want copy entire files of a directory but not the directory itself.

Doing it is also so simple. Use the following syntax-

$ cp -r Dir1 Dir2

There two different of use this command-

You can use this same command to copy Dir1 into Dir2. I have already explained in the section of “Copy Linux Directory on the Same Location” but there is a condition and that is if you want to use cp -r Dir1 Dir2 for copying Dir1 into Dir2 then in such a case you already need to have Dir2 directory.

But if you already have not created Dir2 and you run cp -r Dir1 Dir2 Command then the control will create a Dir2 directory and will copy all the files that exist in Dir1.

I hope you are not confused but let me explain some more in short-

Two different uses of cp -r <Source_Dir> <Destination_Dir>

  • Copying Directory into another Directory.
  • Copying Files of the Directory into another Directory.

Conditions-

  1. If you use cp -r <Source_Dir> <Destination_Dir> where Destination Directory already exists then control will Copy Source Directory and paste it into Destination Directory.
  2. But if you use cp -r <Source_Dir> <Destination_Dir> where Destination Directory don’t exist then the control will create a destination directory and will copy all the files inside the source directory and will paste them into the newly-created directory.

How to Copy Directories and Files to Remote Host in Linux?

Sometimes you have to store some really important data on your computer and you don’t want to lose it at any cost. But a computer is just a machine, and machine failure is very very common it can occur anytime.

In such a case, you can store your Important directories and files on a backup server or a remote server.

How to do that?

Well here is all the details-

You have two ways to copy your to data on remote server or backup server.

  • rsync
  • spc

These both command can be use for that purpose, use any of these according to your convenient.

Copying using rsync command

If you want to copy files and directories to a remote location then rsync could help you in this job.

First of all Install rsync utility in order to use it. You can install rsync using apt-get or yum command. Use the following command to install the utility.

$ sudo apt-get install rsync
$ sudo yum install rsync

Once rsync is installed on your Linux machine. You can use it after going through our article.

You will have to use rsync command followed by the source folder of your computer and remote destination.

Here is the syntax-

$ rsync -ar <source_dir> <destination_usename>@<destination_IP>:<path>

It is again a simple process but some new users get confused and don’t even try to do it. But trust me it is as simple as copying files into our machine itself.

Assume you have dir1 Directory on the Desktop of your computer and you want to copy on the backup server or remote server whose IP address is 192.168.0.1 and user name of the remote server is root.

Here is what we will do-

$ rsync -ar dir1 [email protected]:/root/Desktop

When you execute this command the directory dir1 will be copied to the 192.168.0.1 remote server.

How to use ssh (Secure Shell) for copying files securely?

You can also use rsync utility with ssh for sending files encrypted. It means when you send data using ssh then the data will travel in encrypted format. A human can not read encrypted data and it is very safe for data transmission.

Add the following options in command to use ssh-

$ rsync -avzhe <source_dir> <destination_usename>@<destination_IP>:<path>

Copying using scp command

The scp command an alternative to rsync, it is also used to copy files or directories on a remote or host server.

In order to use spc command you will have to use -r option for recursive.

Syntax-

$ spc -r <source_dir> <destination_usename>@<destination_IP>:<path>

Conclusion

In this tutorial blog post, you learned how to copy a directory in Linux Operating System. I tried to cover all the methods in Linux to copy directories within the system or to the remote or backup server. If you have any confusion or any syntax is not working for you then please let me know in the comment section.

Leave a Comment