How to Copy a Directory Linux
You can use the command with the option to copy a directory in Linux. The -r option stands for “recursive,” meaning it will copy the directory and all its contents, including subdirectories.
Here are the steps to copy a directory in Linux:
Use the cp -r Command
Step 1 – Open your terminal.
Open your terminal, either connect to the server via SSH, via the console, or bash shell
Step 2 – Use the cp -r command to copy a directory
Type the following command and press Enter:bash
cp -r /path/to/source/directory /path/to/destination/directory
Replace /path/to/source/directory
with the path to the directory you want to copy and /path/to/destination/directory
with the path to the location where you want to copy the directory.
Press Enter.
The cp
command will copy the directory and all of its contents to the destination directory. If the destination directory does not exist, it will be created.
Use Rsync
One alternative method is to use the rsync
command, which is a powerful tool for syncing and transferring files between directories or systems.
Here are the steps to copy a directory using rsync
:
Step 1 – Open your terminal.
Open your terminal, either connect to the server via SSH, via the console, or bash shell
Step 2 – Use the rsync command to copy a directory
Type the following command and press Enter:bash
rsync -av /path/to/source/directory/ /path/to/destination/directory/
Replace /path/to/source/directory/
with the path to the directory you want to copy and /path/to/destination/directory/
with the path to the location where you want to copy the directory.Note the use of the trailing slashes after the directory paths. This tells rsync
Copy the source directory’s contents to the destination directory, rather than creating a new directory within the destination directory.
Press Enter.
The rsync
command will copy the directory and all of its contents to the destination directory. Unlike the cp
command, rsync
also provides options for incremental copying, partial transfers, and resuming interrupted transfers, which can be useful for large or complex directory structures.
Want to learn more Linux facts? Check out the rest of our Tech Quicky content!!
Recent Comments