How to Copy Folder/Directory in Linux

How to Copy Folder or Directory in Linux
How to Copy Folder or Directory in Linux

Copy Folder/Directory in Linux

Folder or Directory contains a set of files. A directory can contain multiple files. The top-level main directory known as the root directory. Directories inside the root directory known as subdirectories. In this tutorial, you are going to learn how to Copy the Folder/Directory in Linux.

Copy Directory Using cp command

The basic syntax to copy the directory by using cp command:

cp -arv SOURCE_DIRECTORY DEST_DIRECTORY

Where,
SOURCE_DIRECTORY: The source directory which you want to copy.
DEST_DIRECTORY: The destination directory where the SOURCE_DIRECTORY directory will be copied.
OPTIONS: Following are the options:

  1. -a: This option preserves the attributes like modification date,-time, ownership, file mode, etc.
  2. -r: This option will copy directory recursively.
  3. -v: It will verbose the output.

In the below example, it will copy the directory demo and all the files inside the directory to the demo2 directory. This process is done by preserving attributes with verbose output.

cp -arv demo demo2

The output should be:

How to Copy Folder or Directory in Linux - cp command example
How to Copy Folder or Directory in Linux – cp command example

Copy Directory using rsync command

You can copy files using rsync command across the network at a faster speed than cp command. rsync Command also archives the files and then copies it. So it reduces the size and time. This command is useful in transferring large files.

Following is the basic syntax to copy files using rsync command:

rsync -azv SOURCE_DIRECTORY DEST_DIRECTORY

In the above command -a (Archive Mode) option copies files recursively with all its permission information. -z option compresses the data so the file sizes are reduced and will take less time. Option -v will just verbose the output.

In the following example, all the files inside the directory demo1 will be copied to the demo3 directory. This process completes by preserving attributes with verbose output.

rsync -azv demo1 demo3

The output should be:

How to Copy Folder or Directory in Linux - rsync command
How to Copy Folder or Directory in Linux – rsync command

Conclusion

You have successfully learned How to Copy Folder/Directory in Linux. If you have any queries regarding this then please don’t forget to comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here