Copying directories on Linux is something that comes up quite often while administrating a Linux system.
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 same 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, requesting approval, before overwriting any 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 also quite useful, it stands for verbose, and explains what is being done.
cp -v main.txt main_secondary.txt
This is the final output:
Output 'main.txt -> 'main_secondary.txt'
Copying Directory in Linux
In the previous section, we discussed the steps in copying files.
Now, let’s answer the question, “How to copy a 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, including all of its contents.
You also have to mention the destination and source directory that will be copied.
cp -R <source_folder> <destination_folder>
Let’s use “/directory” as an example directory name. If you want to copy the “/directory” directory to the backup folder “/directory_backup”, run this command:
cp -R /directory /directory_backup
Once you have executed this command, the “/directory” folder will be duplicated in the “/directory_backup” folder. Your output should be similar to this:
cp -R /directory /directory_backup/ ls -l /directory_backup/ total 10 drwxr-xr-x 130 root root 11188 Sept 2 16:24 directory
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 “/directory” example. To copy the contents of the “/directory” directory on the “/directory_backup” destination, write down this -R option command:
cp -R /directory/* /directory_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> <destination_folder>
Going back to our “/directory” example, if you want to copy multiple directories found in the home destination, run the following command:
cp -R /directory/* /home/* /directory_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 into a file, instead of a directory then use the -T option.
cp -RT directory directory_backup
Copying Files and Directories in Linux- rsync command
Besides the cp command, the rsync command is another efficient tool for syncing files from the command line.
Its function is to sync up directories and files between 2 positions.
Also, it copies files from 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, as it includes -rlptgoD. It does the following:
- Allows copying files recursively
- Preserves symbolic links, and file permissions
- Preserves user & group ownerships
- Preserves timestamps on files
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 commands.
sudo apt-get install rsync (or) sudo yum install rsync
Going back to our “/etbox” example, you want to duplicate the “/etbox” folder to a secondary server found at 192.168.1.23.
Besides this, you may also want to duplicate the source directory to the “/etbox_backup” of the isolated server, with the “d username “linuxisthebest”.
To achieve that, execute this command:
rsync -ar /etbox linuxisthebest@192.168.1.23:/etbox_backup
Furthermore, you can also copy everything located in the “/etbox/ 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 /etbox/* linuxisthebest@192.168.1.23:/etbox_backup/
Lastly, if you want to include the present date while running a directory backup, execute the Bash parameter solution. Use this command:
rsync -ar /etbox/* linuxisthebest@192.168.1.23:/etbox_backup/etbox_$(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 “/etbox” example. You want to copy the “/etbox” directory to the backup server 192.168.1.23 and place it in the “/etbox_backup” destination folder.
To do these, execute the following command:
scp -r /etbox linuxisthebest@192.168.1.23:/etbox_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 /etbox linuxisthebest@192.168.1.23:/etbox_backup/etbox_$(date "+%F")
Conclusion
This tutorial shows you what the different commands and options are needed if you want to start copying directories in Linux.