Compare duplicate filenames across directories.

Score: 96

You can do this safely from Bash without overwriting anything. Here are the cleanest and most reliable ways on Arch Linux.


1️⃣ Just check if filenames collide (no paths)

This tells you which filenames exist in both directories, regardless of subdirectories.

comm -12 \
  <(find dir1 -type f -printf '%f\n' | sort) \
  <(find dir2 -type f -printf '%f\n' | sort)

🔹 Output = filenames that appear in both directories 🔹 Safe, read-only 🔹 Ignores file content (only names)


2️⃣ Show full paths of duplicates (more useful)

If you want to see where the duplicates are:

for f in $(comm -12 \
  <(find dir1 -type f -printf '%f\n' | sort) \
  <(find dir2 -type f -printf '%f\n' | sort)); do
    echo "---- $f ----"
    find dir1 dir2 -type f -name "$f"
done

This helps you decide what to rename or move.


3️⃣ Fast method (top-level only, no recursion)

If both directories don’t have subdirectories:

ls dir1 | sort > /tmp/d1
ls dir2 | sort > /tmp/d2
comm -12 /tmp/d1 /tmp/d2

If your goal is copying files without overwriting, use:

cp -vn dir1/* dir2/

Flags:

  • -v → verbose
  • -nno clobber (won’t overwrite existing files)

5️⃣ (Optional) Compare by filename + size (extra safety)

Helps detect same-name but different files:

find dir1 -type f -printf '%f %s\n' | sort > /tmp/a
find dir2 -type f -printf '%f %s\n' | sort > /tmp/b
comm -12 /tmp/a /tmp/b

🔥 Recommendation

If you just want to avoid overwriting, use method 4. If you want to audit before doing anything, use method 2.

If you want:

  • same filename and same content (hash)
  • interactive renaming
  • automatic conflict resolution

tell me 👍