In Linux, copying files and directories is the regular-based job of all the users. It is generally beneficial for creating a backup of important files and directories. Linux provides supportive command line utilities to copy a folder/file, directories, or subdirectories from the present working directory to the target directory. In addition, it can also be done using the “GUI” method
This guide enlists all the possible command line and graphical interface ways to copy a folder in Linux. The outcomes of the guide are listed below:
- Method 1: How to Copy a Folder in Linux Via Command Line?
- Method 1.1: Copy a Folder Using the “cp” Command
- Method 1.2: Copy a Folder Using the “rsync” Command
- Method 2: How to Copy a Folder in Linux Via GUI?
Method 1: How to Copy a Folder in Linux Via Command Line?
Linux offers two well-known command line tools to copy a folder. The first command line utility is the “cp” command, and the second is the “rsync” command. First, move on to the working of the “cp” command to copy a folder in Linux.
Method 1.1: Copy a Folder Using the “cp” Command
The “cp” command line tool facilitates the Linux user to copy single/multiple directories at the destination file or directory. The basic syntax of the “cp” command to copy a folder is typed below:
Syntax:
$ cp -r [source-directory-path] [destination-directory-path]
The above syntax contains the following components:
- cp: Main keyword that represents the “cp” command.
- -r: Denotes the “recursive” flag of the “cp” command that copies all source directory files, directories, and subdirectories at the destination place.
- source-directory-path: Represents the absolute path of the source directory.
- Destination-directory-path: Shows the absolute path of the destination directory.
Let’s move on to the various practical examples of copying single and multiple directories by using the above syntax
Example 1: Copy a Single Folder
There is a “New” folder in the “home” directory containing the following data:
$ ls -l new
To move the “new” folder along with its content from the “home” to the “test” directory use the “cp” command in this way:
$ cp -r /home/itslinuxfoss/new /home/itslinuxfoss/test
The output of the “ls -l” command verifies that the folder/directory “new” has copied from “home” to the “test” directory with all its content.
Example 1: Copy Multiple Folders
The user can copy multiple folders simultaneously with the help of the “cp” command. Suppose there are “Dir1”, “Dir2”, and “Dir3” folders in the “Sample” directory:
$ cp -r /home/itslinuxfoss/Sample/Dir1 /home/itslinuxfoss/Sample/Dir2 /home/itslinuxfoss/Sample/Dir3 /home/itslinuxfoss/Extra
The “ls -l” command confirms that the “Dir1”, “Dir2”, and “Dir3” has been copied from “Sample” to “Extra” directory.
Method 1.2: Copy a Folder Using the “rsync” Command
The “rsync” command is another command line utility that copies folders from the source to the desired target place within the local system. Moreover, the user can also keep syncing the files between different directories. The syntax of the “sync” command is written below:
Syntax:
$ rsync -a [source-directory-path] [destination-directory-path ]
The “rsync” syntax displays the following parameters:
- rysync: Denotes the “rysync” command.
- -a: Transfers/syncs all the source directory data from the local system’s target directory.
- [source-directory-path]: Represents the source directory with the absolute path, i.e., /home/itslinuxfoss/directory1.
- [Destination Path]: Target directory where copied directory/folder needs to be placed.
Example 1: Copy an Entire Folder in Local System
As an example, the directory “documents” contains two subdirectories and three files that are listed by the “ls -l” command:
Copy the “documents” directory entirely (with its content) from “home” to the “Downloads” directory use the “rsync” command in this way:
$ rsync -a /home/itslinuxfoss/documents /home/itslinuxfoss/Downloads
Hence the “documents” directory has been copied from “home” to the “Downloads” directory.
Example 2: Copy a Particular Folder of a Directory
Instead of copying a folder completely, the user can also copy a particular folder available inside the specified directory.
Suppose to copy the “Directory1” folder from the “documents” directory to the “Sample” directory, use the “rsync” command:
$ rsync -av /home/itslinuxfoss/documents/Directory1 /home/itslinuxfoss/Sample
The output ensures that “Directory1” has been copied from “documents” to “Sample” directory.
Method 2: How to Copy a Folder in Linux Via GUI?
The most common and convenient way to copy a folder in Linux is the “GUI” Graphical User Interface. Some essential steps are outlined below to perform this task:
Step 1: Select a Folder
First, “select” the desired folder by utilizing the mouse or keyboard. Once it is selected, press the “right-click” on the mouse as it will generate a drop-down menu list. Click on the “copy” option from the list as shown below:
In the above image, “Folder” is selected, which is located in the “Templates” directory.
Step 2: Copy the Folder at the Target Place
When the selected “Folder” is copied, open the destination/target directory. Right-click inside it and hit the “Paste” option from the pop-up menu list:
As a result of this action, the “Folder” is copied from “Templates” to the “Videos” directory displayed in the below image:
Note: The user can also copy multiple folders by following these stated steps.
Conclusion
Linux offers the “cp(copy)” and the “rsync(remote sync)” command line tools to copy a folder from the source to the destination place. These commands are also beneficial for copying multiple folders at the same time. The user can also perform this task by using the “copy” and “paste” options via “GUI” in the Graphical User Interface. This post has listed all possible aspects of copying a folder in Linux.