Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Im looking for a way to merge 2 directories together.
Either the second into the first, or both into a new tree, either way works for my needs.
What I want to happen is to have ALL files moved from the source to the target, but if the name conflicts, I want to keep both files, probably by adding a suffix or something similar.
I need to make sure that I don't loose any files from either tree, even if the names conflict. At this time, I don't care if the files are identical, I can sweep for identical files later using a md5/file-compare method.
My first thought was to use rsync, but I don't think I can configure it to rename the target file if it already exists.
Second thought was to create a directory list from both trees and create a new directory tree using 'mkdir -p' which has both source trees. Then I could use 'mv -i', but that makes it interactive rather than automated. For this method to work, I would need a 'mv' command which would 'fail' if the target exists, then I could trap the failure in the script and force a new target name...
cd dir1
for i in *; do
dest="../dir2/$i"
if [[ -e "$dest" ]]; then
dest=${dest}.dir1_backup
fi
echo mv "$i" "$dest"
done
Change dir1 and dir2 to your source and destination directories respectively. Run it once as-is, make sure it's doing what you expect, and if you're happy with the result remove the "echo" and run it again to actually move the files.
I would first use the mv '-n' no clobber option to first merge the directories without overwriting any files. Then for each file (for loop) I would then use mv '--backup=numbered'.
cd dir1
for i in *; do
dest="../dir2/$i"
if [[ -e "$dest" ]]; then
dest=${dest}.dir1_backup
fi
echo mv "$i" "$dest"
done
Change dir1 and dir2 to your source and destination directories respectively. Run it once as-is, make sure it's doing what you expect, and if you're happy with the result remove the "echo" and run it again to actually move the files.
I tried your suggestion, however it doesn't treat special characters correctly. I'm having issues with directory and file names which include dollar signs, spaces, back-slashes, ampersands and the like...
I would first use the mv '-n' no clobber option to first merge the directories without overwriting any files. Then for each file (for loop) I would then use mv '--backup=numbered'.
My version of 'mv' doesn't seem to have the "-n" option...
Ew, why do you have directory/file names with those kinds of characters? Spaces are one thing, but backslashes, dollar signs, ampersands? Scripting anything with those files is going to be a nightmare.
That script should work fine for files with spaces, but I wouldn't even know where to begin on the others.
You could try putting quotes around the "${dest}.dir1_backup" and see where that gets you. I generally leave off quotes when a space won't hurt things (such as assigning the string to a variable), but I suppose other characters could cause problems there.
Last edited by suicidaleggroll; 02-05-2015 at 11:48 AM.
Using the --backup option existing destination files can be renamed with a suffix.
rsync -bavr /source /destination
However, the files only get renamed once. Running the same command twice will overwrite the existing files. I would make backups of both directories if possible and test using the dry run option. I have not tried using the backup option
Ew, why do you have directory/file names with those kinds of characters? Spaces are one thing, but backslashes, dollar signs, ampersands? Scripting anything with those files is going to be a nightmare.
That script should work fine for files with spaces, but I wouldn't even know where to begin on the others.
You could try putting quotes around the "${dest}.dir1_backup" and see where that gets you. I generally leave off quotes when a space won't hurt things (such as assigning the string to a variable), but I suppose other characters could cause problems there.
I don't have much of a choice. The directories are backups/archives of user files, and users can do pretty much anything which is "legal". Some example directories in the list:
.../AT&T Diagram and Firewall Policies 10-08-2009/
.../Fitness & Newspaper $15/
.../CustExt - PH/
.../Add'l Info/
Well, I suppose I could create a more recent CentOS 6.6 VM and move the directories there to get this working.
The need for the merge happened because we had 2 different backup systems, and I need to merge the old stuff with the new stuff.
I want to get the old stuff moved into the new backup server tree, but without loosing anything or overwriting any of the new backups.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.