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.

Possible Solutions Step-by-Step Troubleshooting |Working Solutions| Sequential Troubleshooting| |---|---| |Copying Directories/Content in Linux|

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.

How to Copy Directories/Contents in Linux (UTF-8)

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.

Copy a Directory in Linux

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.

Copy directories on Linux

2. Copy the contents of a Linux directory

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.

```bash $ cp -R /mkv/* /mkv_backup ``` This command means to recursively copy all files and subdirectories under the `/mkv` directory to the `/mkv_backup` directory. The `-R` flag indicates recursion, which is used for copying directories.

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:

Copy the contents of a directory in Linux

To copy a directory to a remote host, you can use the Secure Copy (SCP) command in Linux or the File Transfer Protocol (FTP) in Windows. Here's a guide using SCP with SSH, which is more secure: 1. **Using SCP with SSH**: - Make sure you have SSH access to the remote server. - Open your terminal on your local machine. - Run the following command, replacing `username` with the username of the remote server, `remote_host` with the IP address or domain of the remote server, `local_directory` with the path of the directory you want to copy from your local machine, and `remote_directory` with the path where you want to copy the directory on the remote server: ```bash scp -r username@remote_host:remote_directory local_directory ``` For example: ```bash scp -r myusername@192.168.1.100:/home/remote/user/documents /home/local/user/documents ``` This will prompt you for your password. Enter it, and the transfer will begin. 2. **Using FTP**: - If you prefer FTP, you'll need an FTP client like FileZilla for Windows. - Install FileZilla, open it, and go to "File" > "Site Manager." - Add a new site, entering the remote server's IP address or domain, username, and password. - Choose "FTP" as the protocol, and "Normal" as the logon type. - Connect to the server. - In the "Local Site" panel, navigate to the directory you want to copy. - In the "Remote Site" panel, navigate to the location where you want to place the copied directory. - Select the directory in the "Local Site" panel and drag it to the "Remote Site" panel. Remember that FTP transfers are not encrypted by default, so use SCP or SFTP for more secure transfers.

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.

Method 1: Copy a Directory Using the SCP Command SCP (Secure Copy) is a command used to securely transfer files and directories between Linux and Unix systems. Here's how to use the SCP command to copy a directory: 1. Open a terminal. 2. Type the SCP command in the following format: ```bash scp -r [source_path] [target_username@target_host:]target_path ``` Where the parameters mean: - `-r`: Specifies that the copy should be recursive, copying the entire directory. - `[source_path]`: The full path of the directory you want to copy. - `[target_username]`: The username on the target server. - `[target_host]`: The IP address or domain name of the target server. - `[target_path]`: The full path of the directory on the target server where you want to copy the files. For example, if you want to copy the local directory `/home/user/source` to the remote server `example.com` under the user `remoteuser`'s `/home/remoteuser/destination` directory, the command would be: ```bash scp -r /home/user/source remoteuser@example.com:/home/remoteuser/destination ``` 3. Enter your password when prompted, and confirm the authenticity of the target host (usually by typing `yes`). 4. Wait for the file transfer to complete. Note that for using SCP, you need SSH access on both ends, and the target server should be configured to allow SSH connections.

SCP Command to Copy Directory

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 user@remote_host: ``` Replace `` with the path to the directory you want to copy from your local system, `user` with the username on the remote server, `remote_host` with the IP address or domain of the remote server, and `` with the path to the directory where you want to copy the files on the remote server. For example, if you have a directory named `my_files` in your home directory and want to copy it to a remote server with the user `myuser` at `192.168.1.100` in their `documents` folder, the command would be: ```bash scp -r ~/my_files myuser@192.168.1.100:/home/myuser/documents ``` Make sure to enter your password when prompted, and the files will be copied securely over SSH.

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:

Use the SCP command to recursively copy the `/mkv` directory to the `/mkv_backup/` directory on the host `192.168.178.35`: ```shell scp -r /mkv devconnected@192.168.178.35:/mkv_backup/ ```

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).

Method 2: Copy a Directory with the rsync Command

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:

Run the following command with administrator privileges to install rsync:

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:

Use the rsync command to copy directories

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.

How to Recover Deleted Directories in Linux Using Software Accidentally deleting directories is a common occurrence in the Linux operating system. If you don't have backups, this can lead to the loss of important data. Fortunately, there are tools available to help you recover deleted directories. Here are some methods using software to restore directories in Linux: 1. **TestDisk & PhotoRec** TestDisk is a powerful data recovery tool that also comes with its companion program, PhotoRec, for file recovery. To use TestDisk: - First, open a terminal. - Install TestDisk and PhotoRec. On Ubuntu or Debian, type: ``` sudo apt-get install testdisk ``` - For Fedora or CentOS, use: ``` sudo dnf install testdisk ``` - After rebooting, run the `testdisk` command. - Follow the on-screen prompts to select your hard drive and partition. - Choose "Advanced," then locate the partition where you deleted the directory. - Select the "undelete" option to restore files and directories. 2. **Ext3/4 undelete** If your file system is ext3 or ext4, you can use the built-in undelete feature. In the terminal, execute: ``` sudo extundelete --directory /path/to/deleted/directory --restore-all ``` Replace `/path/to/deleted/directory` with the path to your deleted directory. 3. **R-Linux** R-Linux is a commercial data recovery tool designed to recover files and directories from ext2, ext3, and ext4 file systems. Download it from the official website and follow the provided instructions for installation and usage. 4. **Data Rescue for Linux** Data Rescue is a cross-platform data recovery tool that also works on Linux. Download the version compatible with your Linux distribution and follow the provided instructions. Keep in mind that the success rate of data recovery depends on various factors, such as the file system type, whether data has been written to the disk after deletion, and how long ago the deletion occurred. Before attempting recovery, it's best to stop using the hard drive to minimize the risk of overwriting lost data.

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.

    • 1. Data deletion recovery
    • 2. Flexible scanning modes
    • 3. Preview before recovery
    • 4. Free remote assistance

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".

Select "Linux Recovery."

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.

Select the type of data to recover Select the type of data to recover

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.

Recover Lost Data in Windows 10

Last Words

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.