. Advertisement .
..3..
. Advertisement .
..4..
Copying Linux directories is undoubtedly a big part of all systems and administrator routines. And, using the Linux terminal is one of the easiest and quickest methods to copy files and directories.
This tutorial will introduce some useful methods to make a Linux recursive copy.
How To Make A Linux Recursive Copy
Method 1: Use SCP
If you are a frequent coder, you must have once used the scp command. This tool is often employed to copy files between different systems on the same network.
It not only works on Linux and other network systems, but also supports Windows systems. You will surely appreciate its impressive performance in copy directories and files recursively.
With SCP, users can easily copy local files recursively to a remote system. That’s the -r option that enables this function. Here is the syntax of the SCP command:
scp -r LOCAL_PATH REMOTE_PATH
The following example will explain how the command copies the files or directories to a remote system:
scp -r /home/ismail/Downloads 192.168.1.20:/mnt/backup
In this case, an absolute path will also be used for the local directory. Let’s copy the Downloads current directory folder recursively to the remote.
scp -r Downloads 192.168.1.20:/mnt/backup
A double dot copies all the parent directory’s contents with the scp -r .. 192.168.1.20:/mnt/backup command.
On the reverse trend, if you want to make a recursive copy from the remote to the local, it would be best to employ the same -r option.
scp -r REMOTE_PATH LOCAL_PATH
Method 2: Use * Wildcard
An asterisk * is considered a wildcard. This function gives all files starting with a specified name as a parameter to CP. In case of a different extension, there is still a way to copy files with the same name file1.
Here, don’t try to specify the extension at the filename’s end. Instead, let’s use a wildcard with the cp Folder/file1.* Folder3/ command. If you want to copy files from the same extension, do the reverse with the previous method.
Method 3: Use The -r Flag
The -R and -r flag offers an easy way to copy directories and contents recursively. All you have to do is to type the desired directory name after the destination directory and the command.
The -a flag will also work to solve the problem. The two mentioned commands have the same functionality. Yet, the -a flag will not change the metadata, lick creation date when copying the file.
Code:
cp -r Folder/ Folder3/
Method 4: Use The find Command
The find command allows users to find and copy files recursively with the same filenames and extensions from a directory and sub-directories. Use the find and the exec commands:
find Folder/ -name '*.txt' -exec cp -r {} Folder3 \;
Conclusion
If you are working with Linux, you will surely appreciate The above tutorial has offered four easy-to-follow and useful methods to make a Linux recursive copy.
Leave a comment