Do you want to copy a directory or its contents to another location on your Linux system? Are you unsure how to do that? Wondering how many ways you can achieve that? If so, we've got four different solutions for you.
Please provide the English content you want to translate, and I'll promptly translate it into Chinese for you.
Solution 1. Copying a Directory in Linux...Full Steps
Solution 2. Copying Directory Contents in Linux...Full Steps
| |Copying Directories to a Remote Host|Solution 1. Using the SCP Command...Full Steps
Solution 2. Using the rsync Command...Full Steps
| |Recovering a Deleted Directory in Linux|Launch Data Recovery Wizard...Full Steps|Copying directories or contents is part of any system administrator's daily routine on Linux. If you have been using Linux for some time, you would know how important it is to keep your folder structure in good shape in Linux. Sometimes, you may need to copy some directories or contents within your system.
The best way to copy files is by using the command line. If we're talking about Linux, you can use different commands to copy files, such as cp and rsync.
The cp command is one of the most common ways to copy a file, while rsync is used to copy directories.
This tutorial will help you get a complete insight into how to easily copy directories and folders on a Linux system.
Copying directories and their contents is a regular task that anyone can easily accomplish on Linux with the right commands at their fingertips. If you don't know what they are and how to use them to perfection, stick with us until the end for full assistance.
Copying directories in Linux isn't as straightforward as it is in Windows. Linux is a complex platform, and you need to execute a few commands to get the job done conveniently. We've laid out a detailed step-by-step process to help you easily copy directories in Linux.
Step 1. To copy a directory in Linux, you must use the cp command with the -R option. The command also requires specifying the source directory and the target directory where you want to copy it to.
Copy (recursively)
This command is commonly used in Linux or Unix systems to recursively copy a directory and all its contents to another location. "cp" stands for "copy," and the "-R" flag indicates a recursive copy, meaning it will replicate all subdirectories and files within the directory.Step 2. Let's use an example. Suppose you want to back up the /Mkv directory to a backup folder called /mkv_backup. Remember that the /mkv_backup folder is also located in the root of your file system.
So the command to copy the /mkv directory to this backup folder would be:
Recursively copy the /mkv directory to /mkv_backup:
Step 3: Once that's done, the /mkv folder will be successfully copied to /mkv_backup. You've now successfully copied a folder to another folder in Linux!
To copy multiple directories at once, use the following format:
Copy (recursive)...
All the source folders will be copied to the same destination folder and will be available from there.
In Linux, you can also copy the contents of a directory like you would copy a directory. The only difference is that you need to use a wildcard so that all the files get copied.
Step 1. To copy the contents of a directory in Linux, you can use the `cp` command with the `-R` option. After that, you need to specify the source directory followed by a wildcard character.
Recursively copy all files and directories under the root directory to the current directory
Step 2. If you want to copy the contents of the /mkv directory from the /mkv_backup folder, use the given command carefully.
Step 3: Use the ls command to list the contents of the backup folder, and you'll see the files that have been copied into your backup directory.
List the files and folder information in the /mkv_backup directory:
When copying a directory to a remote host, we cannot use the regular CP command, as we need to communicate with two different machines. Instead, we need to use the SCP command or the RSYNC command. Unlike the CP command, these commands are built to work over a network and can only operate between systems.
You can use the SCP command to copy a directory to a remote location on Linux. You need to use the `-r` option to copy recursively, followed by the directory you want to copy and the destination location. The command you would use is:
```bash
scp -r
Copy a file from your local machine to a remote server using the SCP command:
```shell $ scp -r local_path username@remote_host:/remote_path ``` Replace `local_path` with the path to the local file or directory you want to copy, `username` with the username on the remote server, `remote_host` with the address of the remote server, and `/remote_path` with the destination location on the remote server.For example, to copy the contents of the /mkv directory to the /mkv_backup folder on a backup server at 192.168.178.35 with this feature enabled, you would enter:
You would need to use Bash parameter substitution to copy the directory into a custom directory on the server.
Use the scp command to recursively copy the /etc directory to the user 'devconnected' on the IP address 192.168.178.35, with the destination being the /mkv_backup/ directory, and name the folder with the current date in the format of %Y-%m-%d.
This operation takes place in a Shell command line, and it would be translated as:In the Shell command line, execute the scp command to recursively copy the /etc directory to the host at IP 192.168.178.35. The target user is 'devconnected' and the path is /mkv_backup/, where the folder will be named using the current date in the format of year-month-day (for example, 2022-01-01).
If you want to use the rsync command to copy a directory to a remote location, you also need to specify the source folder and the remote destination.
If the server does not have rsync available, you can install it with sudo privileges. You can install rsync with the following command:
You will also need to use the RSYNC command with the -r option (for recursion) and the -a option (for all files). The final command would look like this:
Use $ rsync -ar @ for synchronization.
Let's illustrate this with a real-world example:
Source: Copy the /mkv folder to the backup server at IP address 192.168.178.28/24.
Goal: Copy the above directory to the backup folder /mkv_backup on the remote server.
Assuming the username is `devuser`, run the following command:
Use the rsync command for synchronization: -a indicates archive mode, preserving all file attributes; -r means to handle recursively, processing all files and subdirectories within the directory; /mkv is the directory to be backed up; devuser is the username of the target server; 192.168.178.35 is the IP address of the target server; and /mkv_backup is the target backup directory.
You could choose to copy the contents of the /mkv/ directory. After copying the folder, you would append a wildcard to it.
Use the rsync command to recursively (-a) and preserve permissions (-p) synchronize all files in the /mkv/ directory to the device with the IP address 192.168.178.35, targeting the path /devconnected/mkv_backup/.
To do this, you must enter the current date and perform a directory backup here. The command you need to enter is:
Use the `rsync` command with archive (-a) and recursive (-r) modes to synchronize all files in the `/mkv` directory to the `/mkv_backup` directory on the user `devconnected`'s machine at IP address `192.168.178.35`, including the current date (in the format `%Y-%m-%d`) in the filename:
```bash $ rsync -ar /mkv/* devconnected@192.168.178.35:/mkv_backup/etc_$(date +%F) ``` This command will create a backup directory named `etc_YYYY-MM-DD` (where `YYYY-MM-DD` is the current date) and copy the contents of the `/mkv` directory into it.Copying directories or anything else in Linux isn't a big issue, but what if your important files are unavailable or permanently deleted? Doesn't the situation look annoying?
If you, too, are going through such a bothersome situation, then restoring directories, files, and folders in Linux is the best option for you. You can do that perfectly. With the increased demand for data recovery tools over the past few years, the number of service providers has also increased.
Data Recovery Wizard Pro is one of the most popular data recovery tools out there that grants users the freedom to recover lost files on Linux. Be it MX Linux or Monaro, Linux Mint, Ubuntu, or Debian, it's a tool that works successfully on all platforms.
Users can even use this tool to easily recover deleted files from a Windows PC. It's a simple, easy-to-use, and reasonably priced data recovery wizard that completes the recovery process in just a few seconds.
Though officially supported only on Mac and Windows, it can read the directory structure of Linux. So if you've lost files on Linux, connect the disk to a Windows or Mac machine and use the tool to recover deleted files from Linux.
If you're eager to learn more about this tool, let's delve into its main features and highlights, which will make things clearer.
Here are the steps to recover data using Data Recovery Wizard:
Step 1. Install and launch the Data Recovery Wizard on your Windows computer. Select “Linux Recovery” on the left.
Step 2: The software will automatically detect your IP and port. You need to enter your username and password. If the IP address or port is incorrect, you can correct it manually. Then click "Connect Now".
Step 3. Once connected successfully, the Linux data recovery software will start scanning the disk to look for all lost and deleted files. After the scan is completed, click on “Filter” > “Type” to specify the type of lost file.
Step 4. First, preview the scanned files. Then, select the target data and hit the "Recover" button to restore lost files in your Linux system.
And that's it, folks! That's all there is to know about copying directories and their contents to another location in Linux. Working with Linux is different from Windows and macOS, where you need to input different commands to perform tasks conveniently. This guide introduced you to four different methods for copying directories and their contents from one location to another.