Created
January 13, 2026 10:07
-
-
Save PaperNick/15f24b4474ccc4a805fe3fd26f87abac to your computer and use it in GitHub Desktop.
Remove all duplicated files in the CWD by checking each file hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import subprocess | |
| from pathlib import Path | |
| from typing import List | |
| def get_file_hashes() -> List[str]: | |
| try: | |
| file_hashes: str = subprocess.check_output('find . -type f -exec sha256sum {} \\;', text=True, shell=True) | |
| except subprocess.CalledProcessError: | |
| return [] | |
| return file_hashes.strip().split('\n') | |
| duplicate_hashes = get_file_hashes() | |
| groups = {} | |
| for dup in duplicate_hashes: | |
| sha256hash, file_path = dup.split(' ', 1) | |
| if sha256hash not in groups: | |
| groups[sha256hash] = [] | |
| groups[sha256hash].append(file_path) | |
| duplicated_files_count = 0 | |
| for files in groups.values(): | |
| if len(files) > 1: | |
| duplicated_files_count += len(files) | |
| print(f'{duplicated_files_count} duplicated items found.') | |
| if duplicated_files_count == 0: | |
| exit(0) | |
| action = input('Press "c" to continue and remove duplicated files: ').strip() | |
| if action != 'c': | |
| print('Aborting duplicate files removal.') | |
| exit(0) | |
| for files in groups.values(): | |
| if len(files) > 1: | |
| for duplicated_file in files[1:]: | |
| print(f'Removing {duplicated_file}') | |
| Path(duplicated_file).unlink() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment