Operating a Linux screen requires the mastery of varying technical terms and skills.
Copying directories on Linux is no different. It is the most frequent task you will perform on the command line.
So how exactly do you copy a directory in Linux?
This tutorial will show you how to copy files and directories in Linux using the cp command, rsync, and scp utilities.
Copying Files and Directory – cp command
How to Copy Files?
The most basic use case is copying files in the recurring directory.
If you want to copy a file named main.txt to the main_secondary.txt in the running directory, run this command:
cp main.txt main_secondary.txt
The -i option gives you a confirmation prompt before overwriting your files.
cp -i main.txt main_secondary.txt
Whenever you use the cp command for copying files to a directory, the new files belong to the owner executing this command. The -p option preserves ownership of such files.
cp -p main.txt main_secondary.txt
The -v option is another useful command. It can copy what is being done on each file.
cp -v file.txt file_backup.txt
This is the final output:
Output 'main.txt -> 'main_secondary'
Copying Directory in Linux
In the previous section, we discussed the steps in copying files.
Now, let’s answer the question, “How to copy directory in Linux?”
Here is what you must do when copying the directory in Linux.
You need to run the cp command as well as the -R option for recursive. By doing this, you’ll be able to copy a directory.
You also have to mention the destination and source directory that will be copied.
$ cp -R <source_folder> <destination_folder>
Let’s use “/rdj” as an example directory name. If you want to copy the “/rdj” directory to the backup folder “/rdj_backup”, run this command:
$ cp -R /rdj /rdj_backup
Once you have executed this command, the “/rdj” folder will be duplicated in the “/rdj_backup” folder. Your output should be similar to this:
root@devconnected:-# cp -R /rdj /rdj_backup/ root@devconnected:-# ls -l /rdj_backup/ total 10 drwxr-xr-x 130 root root 11188 Sept 2 16:24 rdj root@devconnected:-#
How to Copy Files and Directories Recursively
To recursively copy the content of a directory, use the cp command with the -R option. It is also important to identify the source-destination along with the wildcard character.
$ cp -R <source_folder>/* <destination_folder>
Let’s apply this with our “/rdj” example. To copy the contents of the “/rdj” directory on the “/rdj_backup” destination, write down this -R option command:
$ cp -R /rdj/* /rdj_backup
You must realize that when listing down the content of the folder, the content is copied and not the folder itself.
How to Copy Multiple Directories in Linux using cp command
To copy multiple directories, use the cp command. Also, write down the different directories you want to copy as well as the destination folder.
$ cp -R <source_folder_1> <source_folder_2> ... <source_folder_n> <destination_folder>
Going back to our “/rdj” example, if you want to copy multiple directories found in the home destination, run the following command:
$ cp -R /rdj/* /home/* /rdj_backup
This command will set up the destination directory.
It will also duplicate all files and sub-directories recursively from the main source to the destination directory.
If, for example, you only want to copy the files and its respective sub-directories and not the target directories use the -T command:
cp -RT rdj rdj_backup
Copying Files and Directories in Linux- rsync command
Besides the cp command, the rsync command is another efficient tool for the command line.
Its function is to integrate directories and source files between 2 positions.
Also, it copies files to both a local and remote destination.
The rsync command features numerous options that control each facet of their behavior. The -a option performs the most tasks. It does the following:
- Copy a directory recursively
- Transfer and denies devices
- Protects symbolic links and moderation times
- Preserves ownership, group, source and permissions
To copy a file from one destination to another, execute the following command:
$ rsync -a file.txt file_backup.txt
If a source-destination file is present, the rsync command will overwrite it.
If rsync is not yet downloaded on your program, install it the following sudo commands.
$ sudo apt-get install rsync $ sudo yum install rsync
Going back to our “/rdj” example, you want to duplicate the “/rdj” folder to a secondary server found at 192.168.1.23.
Besides this, you may also want to duplicate the source directory to the “/rdj_backup” of the isolated server, with the “d username “linuxisthebest”.
To achieve that, execute this command:
$ rsync -ar /rdj email@example.com:/rdj_backup
Furthermore, you can also copy everything located in the “/rdj/ directory instead of the directory itself. You can do this by attaching a wildcard character to the directory. By doing this, all your content will be copied.
$ rsync -ar /rdj/* firstname.lastname@example.org:/rdj_backup/
Lastly, if you want to include the present date while running a directory backup, execute the Bast parameter solution. Use this command:
$ rsync -ar /rdj/* email@example.com:/rdj_backup/rdj_$(date "+%F")
Linux Copy Directory using scp command
To copy a directory on Linux on a distant location, run the scp command along with the r-option for recursive. Follow these up with the directory you want to copy and the destination folder.
$ scp -r <source_folder> <destination_user>@<destination_host>:<path>
Let us go back to our “/rdj” example. You want to copy the “/rdj” directory to the backup server 192.168.1.23 and place it in the “/rdj_backup” destination folder.
To do these, execute the following command:
$ scp -r /rdj firstname.lastname@example.org:/rdj_backup/
Just like the rsync command, you can run Bash parameter substitution to duplicate your directory to a different directory on your server.
$ scp -r /rdj email@example.com:/rdj_backup/rdj_$(date "+%F")
This tutorial shows you how the basic commands and options are needed if you want to start copying files and directories.
It extensively discusses both the rsync and cp command, the two most reliable tools for you to copy directories and files.