How to copy directory in Linux tutorial with examples

Tutorial to copy directory in Linux with examples : Linux is one of the most widely used open-source operating systems out there.

If you are coming from the programming background then you should be familiar with the Linux OS.

A disk can store various types of files such as sound, video, book, document, and pictures. We can call a directory is a collection of files.

Copying a directory or file in any operating system is one of the easiest tasks. If you are windows or a mac user then maybe you are familiar with the process of copying a file or a directory.

In Linux there are so many commands are available by which you can easily perform the copy operation.

Now let us talk about a couple of commands that are commonly used for copying a file or directory in Linux.

cp and rsync command is mainly used to copy directory in Linux.

And now let’s talk about it.

Use cp command to copy directory in Linux

Syntax of cp command:

~$ cp source destination

~$ cp dir_1 dir_2

~$ cp -option1 source destination

~$ cp -option1 option2 source destination

Example 1:

Suppose you want to copy a directory from /home/Desktop/Videos_1 to /home /Desktop/Videos_2 then the command will go like this.

~$ cp -avr /home/Desktop/Videos_1  /home/Desktop/Videos_2

Explanation :

Here the /home/Desktop/Videos_1 is the source path and /home/Desktop/Videos_2 is the destination path.

  • a – responsible for preserving a specified attribute.
  • v – responsible for verbose output
  • r – responsible for copying a directory recursively.

Example 2:

Suppose you want to copy a complete folder /home/Desktop/movies into another folder /home/Desktop/all_videos. Then your Linux command will look like this.

~$ cp -avr /home/Desktop/movies  /home/Desktop/all_videos

Explanation:

Here we have a folder in the desktop named movies which is containing all the movies released recently and you want to copy this whole folder into another folder named all_videos which is also situated in the desktop.

  • a – responsible for preserving a specified attribute.
  • v – responsible for verbose output
  • r – responsible for copying a directory recursively.

Using rsync command

The rsync command is so versatile, very fast, and powerful. It has the capability to make a copy of a file or a directory at the specified server location.

Syntax of rsync command:

~$ rsync -av /path/to/source  /path/to/destination

~$ rsync -av /path/to/source  /path/to/destination/source

Example 1:

Suppose, you want to backup your entire home directory, which contains a lot of large files and folders. So you just need to copy your entire home directory into the other directory named backups.

~$ rsync -avz /home/john/  /media/backups

Explanation:

Here we are going to copy the entire home directory into the backups.

  • a – responsible for copying a folder, including all its permission.
  • v – responsible for verbose output
  • z – this option along with the rsync command is responsible for compressing the data. After compressing the data the transferring part will become easy and smooth.

Example 2:

This time suppose you want to copy a directory or file into a specified server location then your command will look like this.

~$ rsync -avz  /home/john   server_2.xyz.com:/home/john/backups

Explanation :

This time our source directory in the local disk and the destination directory is the server.

  • a – responsible for copying a folder, including all its permission.
  • v – responsible for verbose output
  • z – This option along with the rsync command is responsible for compressing the data. After compressing the data the transferring part will become easy and smooth. You can also transfer the data to the server very easily even in the presence of a slow connection.

Things to consider while copying a directory in Linux

It is very simple to copy a directory in other operating systems like windows and mac OS. But if you are using a Linux based operating system then it’s a tricky task because it requires a little bit of knowledge of using a command-line tool.

One extra thing that you have to keep in mind that make sure you write the director path or the file path correctly and the name of the directory as well.

Because if you don’t write it correctly along with your specific command then things are not going to work properly and you will be facing a lot of trouble in just copying a file from one directory to another directory.

Conclusion :

In this article, we learned how to copy a folder from one directory to another directory and to the specified server location as well using the cp command and rsync command.

Basically both the commands do almost the same operations but the rsync command only has the capability to copy directory in Linux to the server.

Leave a Comment