0

I have a two different .zip files in /tmp/1/ and /tmp/2

I want to compare these two different location files and move the matched string files into /tmp/3

/tmp/1

ArchiveFile_aaa.zip
ArchiveFile_bbb.zip
ArchiveFile_mmm.zip
ArchiveFile_ccc.zip
ArchiveFile_zzz.zip

/tmp/2

ArchiveFile_aaa.zip
ArchiveFile_bbb.zip
ArchiveFile_ccc.zip
ArchiveFile_ddd.zip
ArchiveFile_eee.zip
ArchiveFile_zzz.zip
ArchiveFile_ttt.zip
ArchiveFile_mmm.zip
ArchiveFile_fff.zip

I can get the same string of these files using

grep -f /tmp/1 /tmp/2

. But how to move those matched string files into /tmp/3

Booth
  • 488
  • 2
  • 4
  • 11

1 Answers1

0

I resorted to python, It should work with any version of Python, I have only tested in on Linux however. For Windows you may have to reverse the "/".

I have not tried this with evil file names - but with simple names.

To use

python dir_merge.py DIR1 DIR2 DIR3

Where DIR1 is a directory with files in, DIR2 also a directory with files in. DIR3 can already exists, or it will be created - only the common files go into this directory. The files are renamed into DIR3

import sys
import os
import pprint

''' 
usage: Python dir_merge.py Directory1 Directory2 NEW_DIRECTORY

i.e.
       python dir_merge.py tmp1 tmp2 tmp3
'''

d1=sys.argv[1]
d2=sys.argv[2]
d3=sys.argv[3]

files_in_d1 = os.listdir(d1)
files_in_d2 = os.listdir(d2)

print("Contents of d1 are...")
pprint.pprint(files_in_d1)
print("Contents of d2 are...")
pprint.pprint(files_in_d2)

common_files = list(set(files_in_d1).intersection(files_in_d2)) 
print("Common Files are ")
pprint.pprint(common_files)
if len(common_files) > 0:
  try:
    os.mkdir(d3)
  except:
    print("Can not create a directory")

  for f in common_files:
     os.rename(d1+'/'+f,d3+'/'+f)
     print("Moved file "+f)
else:
  print("Nothing Matches in the 2 directories")

Let me know how you get on.... I should check that you have entered 3 values at the start - but that can be up to you.

Regards

Tim Seed
  • 209
  • 1
  • 3