Advertisement
XenoTheStrange

Simple Compare Directory Files by Name and Size [Python, Linux]

Aug 23rd, 2022 (edited)
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4. import os
  5. import subprocess as sub
  6.  
  7. """This checks 2 directories to see what files are different between them by name, then compares files present in both directories to see if they are not identical (at least in size). It's not as good as hashing, but I was doing this with 4GB iso files soooo yeah.
  8. If you don't include the second directory it'll use whatever dir you're in."""
  9.  
  10. def main(basedir, dir1, dir2):
  11.     if not dir1[-1]=="/":dir2 +="/"
  12.     if not dir2[-1]=="/":dir2 +="/"
  13.     dat1 = sub.check_output(["ls", dir1]).decode("utf-8").split("\n")
  14.     dat2 = sub.check_output(["ls", dir2]).decode("utf-8").split("\n")
  15.     dat1.pop()
  16.     dat2.pop()
  17.     with open("in.1.but.not.2.txt","w") as file:
  18.         for i in dat1:
  19.             if not i in dat2:
  20.                 file.write(i+"\n")
  21.     with open("in.2.but.not.1.txt","w") as file:
  22.         for i in dat2:
  23.             if not i in dat1:
  24.                 file.write(i+"\n")
  25.     with open("sizediff.txt", "w") as file:
  26.         for i in dat1:
  27.             if i in dat2:
  28.                 size1 = sub.check_output(["du", dir1+i]).decode("utf-8").split("\t")[0]
  29.                 size2 = sub.check_output(["du", dir2+i]).decode("utf-8").split("\t")[0]
  30.                 if not size1 == size2:
  31.                     file.write(i+"\n")
  32.  
  33. if __name__ == "__main__":
  34.     if len(sys.argv) < 3:sys.argv.append(os.getcwd())
  35.     main(sys.argv[0], sys.argv[1], sys.argv[2])
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement