LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 02-05-2015, 10:39 AM   #1
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 286

Rep: Reputation: Disabled
Merge directories


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...

Looking for suggestions and/or scripts. Thanks
 
Old 02-05-2015, 10:51 AM   #2
maples
Member
 
Registered: Oct 2013
Location: IN, USA
Distribution: Arch, Debian Jessie
Posts: 814

Rep: Reputation: 265Reputation: 265Reputation: 265
for the mv command, could you use the yes command to pipe "n" to mv?
Code:
yes n | mv (...)
EDIT: This isn't that good of a suggestion, look at some of the other options suggested.

Last edited by maples; 02-05-2015 at 11:15 AM.
 
Old 02-05-2015, 11:05 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Code:
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.
 
Old 02-05-2015, 11:10 AM   #4
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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'.
 
Old 02-05-2015, 11:37 AM   #5
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 286

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Code:
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...
 
Old 02-05-2015, 11:38 AM   #6
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 286

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by metaschima View Post
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...

> touch a b
> mv -n a b
mv: invalid option -- n

mv (GNU coreutils) 5.97
 
Old 02-05-2015, 11:40 AM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
Old 02-05-2015, 11:40 AM   #8
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 286

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by maples View Post
for the mv command, could you use the yes command to pipe "n" to mv?
Code:
yes n | mv (...)
EDIT: This isn't that good of a suggestion, look at some of the other options suggested.
Just testing to be complete...

> yes n | mv -i a b
mv: overwrite `b'? yes: standard output: Connection reset by peer
yes: write error
 
Old 02-05-2015, 11:48 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,745

Rep: Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924
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
 
Old 02-05-2015, 12:00 PM   #10
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 286

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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/
 
Old 02-05-2015, 12:14 PM   #11
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
What about 'cp -n' ?
 
Old 02-05-2015, 12:32 PM   #12
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 286

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by metaschima View Post
What about 'cp -n' ?
Not sure which versions of cp/mv people are referencing. They don't appear to be valid options for my versions.

> cp -n a b
cp: invalid option -- n
Try `cp --help' for more information.

> cp --version
cp (GNU coreutils) 5.97

Red Hat Enterprise Linux Server release 5.10 (Tikanga)

Last edited by usao; 02-05-2015 at 12:35 PM.
 
Old 02-05-2015, 12:54 PM   #13
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try rsync:
https://taoofmac.com/space/HOWTO/Merge%20Folders
plus the '-b' option.

Yeah RHEL is ancient.

Code:
bash-4.2$ cp --version
cp (GNU coreutils) 8.21
 
Old 02-05-2015, 12:58 PM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by metaschima View Post
Yeah RHEL is ancient.

Code:
bash-4.2$ cp --version
cp (GNU coreutils) 8.21
Only ancient versions of RHEL.

CentOS 6.6:
Code:
$ cp --version
cp (GNU coreutils) 8.4
CentOS 7:
Code:
$ cp --version
cp (GNU coreutils) 8.22
 
Old 02-05-2015, 03:06 PM   #15
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 286

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
merge directories d-fens Linux - General 9 02-04-2010 05:48 PM
Merge 2 same name directories in linux. nishith Linux - Server 12 09-25-2009 12:15 AM
How to merge two directories without losing data riteshanand40 Linux - Newbie 1 05-18-2009 06:17 AM
merge directories ryedunn Linux - Newbie 4 11-15-2006 09:02 AM
how do I Merge 2 directories syssyphus Linux - General 4 01-21-2006 02:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration