Last active
August 16, 2023 16:43
-
-
Save DavideWiest/a1bd6d79a2acd46f2d91014eb238a301 to your computer and use it in GitHub Desktop.
Delete Git Duplicates (useful for the Obsdian Git Plugin)
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
"terminal version" | |
import os | |
import platform | |
try: | |
from tqdm import tqdm | |
USETQDM = True | |
except ImportError: | |
USETQDM = False | |
HOSTNAME = platform.node() | |
def deleteDuplicateMdFiles(dir): | |
duplicates = [] | |
for root, dirs, files in os.walk(dir): | |
duplicates += [os.path.join(root, f) for f in files if f.endswith(HOSTNAME + ".md") and f.replace(f"-{HOSTNAME}", "") in files] | |
for d in duplicates: | |
print("- " + d) | |
print(f"Found {len(duplicates)} duplicate files.") | |
if USETQDM: | |
for file in tqdm(duplicates)): | |
deleteDuplicate(dir, file, False) | |
else: | |
for file in duplicates: | |
deleteDuplicate(dir, file, True) | |
def deleteDuplicate(dir, file, printInfo): | |
filePath = os.path.join(dir, file) | |
os.remove(filePath) | |
if printInfo: print(f"Deleted: {filePath}") | |
if __name__ == "__main__": | |
target_directory = input("Enter the path of the directory (. for the current working directory): ") | |
if target_directory == ".": target_directory = os.getcwd() | |
if os.path.isdir(target_directory): | |
deleteDuplicateMdFiles(target_directory) | |
print("Finished process.") | |
else: | |
print("Invalid directory path.") |
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
"automatic version using the current working directory" | |
import os | |
import platform | |
HOSTNAME = platform.node() | |
def deleteDuplicateMdFiles(dir): | |
duplicates = [] | |
for root, dirs, files in os.walk(dir): | |
duplicates += [os.path.join(root, f) for f in files if f.endswith(HOSTNAME + ".md") and f.replace(f"-{HOSTNAME}", "") in files] | |
for file in duplicates: | |
deleteDuplicate(dir, file, False) | |
def deleteDuplicate(dir, file, printInfo): | |
filePath = os.path.join(dir, file) | |
os.remove(filePath) | |
if printInfo: print(f"Deleted: {filePath}") | |
if __name__ == "__main__": | |
target_directory = os.getcwd() | |
deleteDuplicateMdFiles(target_directory) |
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
// typescript-equivalent, not tested yet | |
import * as fs from 'fs'; | |
import * as path from 'path'; | |
import os from 'os'; | |
const HOSTNAME = os.hostname(); | |
function deleteDuplicate(dir: string, file: string): void { | |
const filePath = path.join(dir, file); | |
fs.unlinkSync(filePath); | |
console.log(`Deleted: ${filePath}`); | |
} | |
if (require.main === module) { | |
const targetDirectory = process.cwd(); | |
const files = fs.readdirSync(targetDirectory); | |
files.forEach(file => { | |
if (file.endsWith(`${HOSTNAME}.md`) && files.includes(file.replace(`-${HOSTNAME}`, ''))) { | |
deleteDuplicate(targetDirectory, file); | |
} | |
}); | |
} |
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
"smaller version without tqdm" | |
import os | |
import sys | |
import platform | |
HOSTNAME = platform.node() | |
def deleteDuplicateMdFiles(dir): | |
duplicates = [] | |
for root, dirs, files in os.walk(dir): | |
duplicates += [os.path.join(root, f) for f in files if f.endswith(HOSTNAME + ".md") and f.replace(f"-{HOSTNAME}", "") in files] | |
print(f"Found {len(duplicates)} duplicate files.") | |
for file in duplicates: | |
deleteDuplicate(dir, file, True) | |
def deleteDuplicate(dir, file, printInfo): | |
filePath = os.path.join(dir, file) | |
os.remove(filePath) | |
if printInfo: print(f"Deleted: {filePath}") | |
if __name__ == "__main__": | |
target_directory = input("Enter the path of the directory (. for the current working directory): ") | |
if target_directory == ".": target_directory = os.getcwd() | |
if os.path.isdir(target_directory): | |
deleteDuplicateMdFiles(target_directory) | |
print("Finished process.") | |
else: | |
print("Invalid directory path.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment