How to merge two directory trees with similar layout into a third directory? Let us consider the following example to understand the problem.
Suppose two directories dir1 and dir2 have 3 sub-directories a, b and c in each of them. The directory layout is like below:
These directories a, b and c have some files in them. The output of tree command will illustrate better:
1. Using cp to create merge:
Now we want to merge these two directories into a third directory, say "merged".
The simplest thing that you can do to achieve this is to copy recursively the directories like below:
1.1 Problem with cp command and alternative:
The problem with this approach is that the files created inside merged directory are copy of original files, and not the original files themselves. But wait, (you might be asking yourself) what is the problem if the files are not original? So to answer your question, consider the situation where you have large number of bulky files. In that case, copying all the files might take hours.
Now let's get back and try the same with mv command instead of cp.
The directories are not merged. So we cannot use mv command to merge directories like this.
Now how can you keep the original files inside "merged" directory?
2. The solution:
The cp command has a very useful option to draw us out of this situation.
The -l or --link option to cp aommand creates the hard links instead of copying the files themselves. Let us try with that.
Before trying out the hard link option to cp command, let us print the inode number of the original files.
The tree command has option to print the inodes with --inodes option:
Now we have the inodes listed here, we can proceed to creating the hard links with --link option to cp command:
2.1 Verify the files:
Now the files are copied, let us verify if the inodes match with original files:
As you can see that the files have same inodes as original files. Now the problem is solved and we have the original files inside merged directory. We can now cleanup by removing the directories dir1 and dir2.