How Can I copy-merge two Directories Using Linux Command?
January 11 2023
Assume that if you have number of files in two different directories and you want to merge or sync up the those in a single directory, you can use the options below,
There are a few different ways to copy-merge the contents of two directories in Linux,
Copy (cp) command is used to copy files and directories and --recursive (-r) option copy the contents of directories recursively
To copy-merge the contents of two directories logs-directory_1 and logs-directory_2 into a new directory merged_dir, you can use the following command:
cp -r logs-directory_1/* merged_destination/
cp -r logs-directory_2/* merged_destination/
rsync : this command is another tool that can be used to copy and synchronize files and directories.
rsync -a /path/to/source/ /path/to/destination
And use the command below for logs-directory_1 and logs-directory_2.
rsync -a logs-directory_1/ logs-directory_2/
or
rsync -avu logs-directory_1/* merged_destination/
rsync -avu logs-directory_2/* merged_destination/
Comments (0)