Skip to content

Instantly share code, notes, and snippets.

@keithah
Created March 6, 2023 00:23
Show Gist options
  • Save keithah/5a70b788d0e71f107bef4e560795f2ee to your computer and use it in GitHub Desktop.
Save keithah/5a70b788d0e71f107bef4e560795f2ee to your computer and use it in GitHub Desktop.
import os
import argparse
import subprocess
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Recursively delete duplicate directories in one directory using rclone')
parser.add_argument('dir1', help='The first directory to compare')
parser.add_argument('dir2', help='The second directory to compare')
parser.add_argument('--dry-run', action='store_true', help='Do a dry run (do not actually delete any directories)')
parser.add_argument('--max-depth', type=int, default=1, help='The maximum depth to search for duplicate directories (default: 1)')
args = parser.parse_args()
# Get a list of directories in dir1 and dir2
cmd = f'rclone lsf --dirs-only {args.dir1}'
proc = subprocess.run(cmd, shell=True, capture_output=True, text=True)
dir1_dirs = proc.stdout.split('\n')
dir1_dirs.remove('') # Remove any empty strings from the list
cmd = f'rclone lsf --dirs-only {args.dir2}'
proc = subprocess.run(cmd, shell=True, capture_output=True, text=True)
dir2_dirs = proc.stdout.split('\n')
dir2_dirs.remove('') # Remove any empty strings from the list
# Recursively search for duplicate directories
dirs_to_delete = []
for root, dirs, _ in os.walk(args.dir2):
# Check if we've exceeded the maximum depth
depth = root[len(args.dir2) + len(os.path.sep):].count(os.path.sep)
if depth > args.max_depth:
continue
# Check each directory in dir2 against those in dir1
for dir2_dir in dirs:
if os.path.join(root, dir2_dir)[len(args.dir2) + len(os.path.sep):] in dir1_dirs:
# If the directories have the same name, add the one in dir2 to the list to be deleted
dir2_path = os.path.join(root, dir2_dir)
dirs_to_delete.append(dir2_path)
# Remove the directory from the list so we don't check it again
dirs.remove(dir2_dir)
# Print a list of the directories that would be deleted if running in dry run mode
if args.dry_run:
if len(dirs_to_delete) > 0:
print("The following directories would be deleted:")
for dir_to_delete in dirs_to_delete:
print(dir_to_delete)
else:
print("No duplicate directories found.")
# Actually delete the directories
if not args.dry_run:
for dir_to_delete in dirs_to_delete:
cmd = f'rclone purge "{dir_to_delete}"'
proc = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if proc.returncode == 0:
print(f"Deleted duplicate directory: {dir_to_delete}")
else:
print(f"Error deleting duplicate directory {dir_to_delete}:")
print(proc.stderr.strip())
# Print a message indicating that we're done
if args.dry_run:
print("Finished dry run.")
else:
print("Finished deleting duplicate directories.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment