Skip to content

Instantly share code, notes, and snippets.

@salamanders
Last active July 20, 2021 19:43
Show Gist options
  • Save salamanders/d16b07672aab6dfaac45403e1b072a04 to your computer and use it in GitHub Desktop.
Save salamanders/d16b07672aab6dfaac45403e1b072a04 to your computer and use it in GitHub Desktop.
// ~/Documents/kotlin/kotlinc/bin/kotlinc -script dedupe.kts -- -d /media/pi
import java.io.File
require(args.isNotEmpty()) { "kotlinc dedupe.kts [-d] [ROOT FILE PATH]" }
val delete: Boolean = args[0] == "-d"
val root: File = File(args.last()).canonicalFile
require(root.exists() && root.isDirectory) { "Root must exist and be a directory: '$root'" }
println("Starting in '$root'")
val files = root.walkTopDown()
.filter { it.isFile && it.canRead() && it.length() > 0 }
.map { it.canonicalFile }
.sortedBy { it.canonicalPath }
.toList()
println("Found ${files.size} files.")
files.forEachIndexed { index, file ->
files.filterIndexed { otherIndex, otherFile ->
otherIndex > index &&
otherFile.name == file.name &&
otherFile.length() == file.length() &&
otherFile.canonicalPath != file.canonicalPath
}.forEach { otherFile ->
println("'$file' matches '$otherFile', removing sibling...")
if(delete) {
otherFile.delete()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment