I have several directories with files which mostly have the same name but might be different and I need to have all these directories merged into a new one. I need to be able to compare the same named files and if the same ignore/overwrite or if different move/rename the older one by appending the modification date/time to the filename.
To add more details and to address @Serhat Cevikel and @Wildcard: Data is stored on two drives with similar structure and it is somehow complicated since there are sub-folders which need to be taken into consideration. Here is a tree of the testing environment I have created and some comments:
/bmrlbackup/drive1/
`-- user001
`-- directory1
`-- project001
|-- file000 #identical
|-- file001 #older same name
|-- file0011 #unique
|-- phase1
| |-- file000 #identical
| |-- file110 #unique
| |-- file999 #newer same name
| `-- phase11
| `-- file111 #unique
`-- phase2
`-- file120 #unique
/bmrlbackup/drive2/
`-- user002
`-- directory2
`-- project001
|-- file000 #identical
|-- file001 #newer same name
|-- file0012 #unique
|-- phase1
| |-- file000 #identical
| |-- file210 #unique
| `-- file999 #older same name
`-- phase2
|-- file220 #unique
`-- phase21
`-- file221 #unique
The output for the first rsync:
#rsync -a --ignore-existing --remove-source-files $sd1/ $dd1/
project001/
project001/file0011
project001/phase1/
project001/phase1/file110
project001/phase1/phase11/
project001/phase1/phase11/file111
project001/phase2/
project001/phase2/file120
Changed the remm (remaining "same" files) to list the sub-directories as well:
#remm=`ls -1 $(find $sd1/ -type f)`
/bmrlbackup/drive1/user001/directory1/project001/file000
/bmrlbackup/drive1/user001/directory1/project001/file001
/bmrlbackup/drive1/user001/directory1/project001/phase1/file000
/bmrlbackup/drive1/user001/directory1/project001/phase1/file999
Here, the two files:
/bmrlbackup/drive1/user001/directory1/project001/file000
/bmrlbackup/drive1/user001/directory1/project001/phase1/file000
are the same in both locations and need not be copied or can be moved and overwrite the destination.
The same name different content files:
/bmrlbackup/drive1/user001/directory1/project001/file001
/bmrlbackup/drive1/user001/directory1/project001/phase1/file999
The "same name different content" files need to be compared and the older one needs to be renamed: appended with the modification date&time, so if source is newer then append the name of destination file and move the source, and if the source is older, then append the name of the source and move the name appended source.
The resultant of this process will eventually move all the files from drive1 to drive2.
Then everything errors for oldest=`find {$sd1,$dd1}....
Advice?
There are no more than 10000 files on each drive with sizes from 4k to 800M.