The directory and its content are copied to create the backups for particular data present in the directory. In Linux, built-in utilities such as cp are considered for the copy of the directory to the new location. However, to copy the directory along with its content (recursively), there are several ways to perform this task.
This post will specify the various ways to copy the directory and its content to a new location in Linux.
- Method 1: Using cp Command
- Method 2: Using rsync Command
- Method 3: Using tar Command
- Method 4: Using GUI
Method 1: Using cp Command
As mentioned earlier, the cp command is utilized for copying the files/directories. The syntax for copying the directory recursively is shown below:
Syntax
$ cp -Rvp [source path] [destination path]
The description of the above syntax is mentioned below:
- “cp”: “cp” command to copy the directory.
- “Rvp”: “R” option for recursively, “v” option for verbose mode, and “p” option to preserve the permissions.
- “source path”: Define the source path of the directory.
- “destination path”: Refers to the destination path of the directory (new location).
Example
In the following example, the “Henry” directory will be copied to the new location named the “example” directory. The “R” flag will copy the recursively (all content of the directory), the “p” flag preserves the permissions of the files, and the “v” flag for verbose mode:
$ cp -Rvp /home/itslinuxfoss/Henry /home/itslinuxfoss/example
The directory is copied along with its content.
To verify that the directory is copied along with its content, let’s jump into the copied directory and display its content through the “ls” command:
$ cd example/Henry
$ ls
The “Henry” directory has been copied with content “color-file.txt”, “file1.txt”, “file3.txt”, etc.
Method 2: Using rsync Command
The “rsync” (Remote Synchronization) is the built-in utility Linux that can be utilized for syncing the directory with its content. The syntax for using the “rsync” utility is given below:
Syntax
rsync -av [Source Path] [Destination Path]
The syntax is defined as
- “rsync”: to sync the directory.
- “a”: Option for archive mode.
- “v”: Option for verbose mode
- “source path”: define the source path of the directory.
- “destination path”: refers to the destination path of the directory (new location).
Example
The following command sync copies the “Henry” directory to the “Desktop” directory. Here “a” flag is used for preserving the file permissions, symbolic links, and modification time. While the “v” flag for verbose mode:
$ rsync -av /home/itslinuxfoss/Henry /home/itslinuxfoss/Desktop
The given directory is synced (copied) along with its content.
Verify Copied Directory and its Content
To verify that directory is copied/synced, jump into the desktop directory and locate the copied directory.
$ cd Desktop/Henry
$ ls
The “Henry” directory is synced (copied) along with its content “color-file.txt”, “file1.txt”, “file3.txt”, etc.
Method 3: Using tar Command
Another way to copy the directory is to copy the directory along with its content to create the tar file of the directory. Then copy it to the new location using the cp utility as described in method 1. The syntax to tar a directory is given below:
Syntax
$ tar -cvf [Creaete_Directory_Name].tar [Target Directory]
The syntax is defined as
- “tar”: for creating the tar file keyword.
- “c”: option for creating the archive
- “v”: option for verbose mode,
- “f”: option for using the archive device.
- “Create Directory Name”: Define the name of the directory
- “Target Directory”: Define the target directory path to convert it into a tar file.
Example
An example is considered to create and copy the directory along with its content. For this, follow the below-mentioned steps:
Step 1: Create tar File
To create the tar file of the “Henry” directory, execute the tar command with “c”, “v”, and “f” options as shown:
$ tar -cvf Copy_dir.tar Henry
The directory is converted into a tar file with its content.
Step 2: Copy the tar File
Use the cp utility and copy the tar file (Copy_dir.tar) to the new location (New_Dir) as shown:
$ cp Copy_dir.tar New_Dir
The “Copy_dir.tar” file has been copied to the “New_Dir” directory.
Step 3: Extract the Copied tar File
Final step is to jump into the “New_Dir” directory where the file is copied. To extract the tar file (Copy_dir), use the “tar” command with the “xf” (x for extract and f refers file) option:
$ cd New_Dir$ tar -xf Copy_dir.tar
The “Copy_dir.tar” file is extracted.
Verify the Change
Once the file is extracted, the copied directory (Henry) will be available and can be verified by displaying the content of the directory:
$ ls Henry
The “Henry” directory is available with its content.
Method 4: Using GUI
Lastly, the user can copy the directory by right-clicking or press the “Ctrl+C” from the keyboard:
Once the file is copied, go into the new location where the user wants to copy and paste it by pressing right-click:
The directory is copied as shown below:
Conclusion
To copy the directory and its content to the new location, use the “cp” with the “R” flag, “rsync”, or ”tar” commands. Additionally, users can follow the GUI method to copy the directory along with its content.
For using the “tar” command, first, create the tar file of the directory, paste it into the destination, and then extract it. This write-up has illustrated all the possible methods to copy the directory along with its content to the new location.