-1

Suppose Directory1 has

1.File1 
2.File2
3.Subdirectory1
    Subdirectory1 has :
    3.1. File 3
    3.2. File 4

and

Directory2 has

1.File1
2.File3
3.Subdirectory1
    Subdirectory1 has :
    3.1. File 3
    3.2. File 6

If any file is uniquely present in Directory2 it has to be deleted. If the file is present in both Directory1 and Directory2 , files in Directory1 has to be copied to Directory2 with the same folder structure [Updates].

1

2 Answers 2

0

Simply use diff, eg :

diff -r dir1 dir2 | grep dir1
Only in dir1: file2
Only in dir1/subdir1: file4
Only in dir2/subdir1: file6

You can then awk, or store the result in a temporary file and use it in your script.

4
  • Thanks . I initially started with this approach . Faced problems when the files had same name in sub-directories . Commented Jun 12, 2019 at 14:12
  • In this case you can use "find /dir1/ -type f -exec md5sum {} \; > dir1.txt" then same on dir2 than diff. Another solution is to use git !
    – lowmath
    Commented Jun 12, 2019 at 14:49
  • So it is impossible to create same hashmap for two non identical files , But the files in Directory1 will always be an update over files in directory2 [GIVEN] . In these conditions files in directory2 has to be replaced by files from directory1 . Commented Jun 12, 2019 at 15:29
  • That's why i think you should combine the different approaches in a script. md5sum will just tell you that files are really different even if same name. If the important thing for you is not the name but the content, you script should always copy / move the file from dir1 to dir2 in any case...
    – lowmath
    Commented Jun 13, 2019 at 7:22
0

You seem to talk about a mirroring function, see the nice Open Source tool rsync. https://rsync.samba.org/

It can do all of that and even more (also remote synchronization via LAN or via SSH if needed).

rsync -options --otherOptions sourceDir targetDir

Usually you would use these commandline options:

rsync -av /src/foo /dest
or
rsync -av /src/foo/ /dest/foo

Note: if you omit the trailing "/" of /src/foo, then rsync will mirror to /dest and create a foo subdir. You have either the one or the other choice how to use this command.

Not the answer you're looking for? Browse other questions tagged or ask your own question.