sodcalculator

08 Everyday Math

Duplicate File Linux: Find and Remove Duplicates

Guide 08-64577 · Updated · Step by step

To find and remove duplicate files on Linux, use command-line tools like fdupes or rdfind, or GUI tools like dupeGuru or fslint. These tools compare file contents (not just names) and let you delete or link duplicates safely.

Why Duplicate Files Accumulate

Duplicate files appear for many reasons: repeated downloads, backup restores, syncing mishaps, or simply copying folders multiple times. On a Linux system, they waste disk space, slow down backups, and make it harder to find the file you actually need. For example, a user might have three copies of a 2 GB video file scattered across directories, costing 4 GB of unnecessary storage. Identifying and removing them is a common housekeeping task for both desktop users and server administrators.

How Duplicate Detection Works

Duplicate detection tools generally work in two stages:

  1. Size comparison: Files with different sizes are immediately excluded—they cannot be identical.
  2. Hash comparison: For files of the same size, the tool computes a checksum (like MD5, SHA-1, or SHA-256) of the content. If the hashes match, the files are considered duplicates.

Some tools also compare file names or metadata first to speed up the process, but content hashing is the reliable method. For large files, some tools use a partial hash (first few kilobytes) before doing a full hash to avoid reading entire files unnecessarily.

Command-Line Tools: fdupes and rdfind

The most popular command-line tools are fdupes and rdfind. Both are available in the default repositories of most distributions.

fdupes is simple and interactive. To scan a directory and list duplicates:

fdupes -r /path/to/folder

The -r flag makes it recursive. It prints groups of duplicate files. To delete duplicates interactively, use -d; it will ask which ones to keep. To keep the first file in each group and delete the rest without asking, use -dN (but be careful—it deletes without prompting).

rdfind is more automated and can also create hard links or symlinks instead of deleting. A typical command:

rdfind -makehardlinks true /path/to/folder

This replaces duplicate files with hard links to the first occurrence, saving space without losing the file in multiple locations. Use -deleteduplicates true to delete duplicates instead. rdfind outputs a summary of what it did.

Graphical Tools for Desktop Users

If you prefer a GUI, several options exist. dupeGuru is a cross-platform tool with a clean interface. It lets you scan folders, shows duplicate groups, and allows you to mark files for deletion or moving. fslint is an older but still functional tool that integrates with file managers. czkawka is a modern, fast GUI and CLI tool written in Rust; it offers duplicate finder, empty folder finder, and similar image finder. These tools are especially useful if you want to preview images or videos before deciding what to delete.

How to Choose the Right Tool

Consider your needs:

  • Quick one-off cleanup: Use fdupes in the terminal.
  • Automated or scripted cleanup: Use rdfind or fdupes in a cron job.
  • Large media libraries: Use dupeGuru or czkawka to preview files before deleting.
  • Server or headless system: Stick with command-line tools.

Also consider whether you want to delete duplicates, move them to a trash folder, or replace them with hard links. rdfind handles all three; fdupes only deletes or preserves.

Common Pitfalls and Safety Tips

Deleting duplicates can be risky if you aren’t careful. Here are common mistakes:

  • Deleting without checking: Always review the list before deleting. A file may have the same content but be in a different location for a reason (e.g., a config file that needs to be present in multiple directories).
  • Ignoring hard links: If you use rdfind -makehardlinks, understand that hard links share the same inode. Deleting one link does not free space until all links are removed. This is usually fine, but be aware.
  • Scanning system directories: Avoid scanning /usr, /bin, or /etc unless you know what you’re doing. Many system files are intentionally duplicated or hard-linked.
  • Not checking symlinks: Tools may follow symlinks and cause unexpected results. Use flags like -n in fdupes to exclude symlinks.

Always back up important data before mass deletion. A simple tar command can save you from a costly mistake.

Alternatives: Using find and md5sum

If you don’t want to install extra tools, you can use built-in commands. A common approach is to find files by size, then compute MD5 hashes:

find /path -type f -size +1M -exec md5sum {} + | sort | awk '{print $1}' | uniq -d

This lists duplicate hashes, but you’d need to map them back to filenames. A more complete script would store hashes and filenames in a temporary file and compare. This method is slower and less user-friendly but works on any Linux system without installing anything.

Frequently Asked Questions

Will fdupes delete files automatically?
No, by default fdupes only lists duplicates. You must use the -d flag to delete, and it will prompt you for each group unless you use -dN to delete all but the first automatically.

Can I find duplicate files by name only?
Yes, you can use find with -exec basename and sort, but that only finds files with the same name, not necessarily the same content. For true duplicates, content comparison is necessary.

Is it safe to delete duplicates in my home directory?
Generally yes, but review the list first. Some applications store configuration files that may be identical but needed in different locations. When in doubt, move them to a trash folder instead of permanent deletion.

What is the fastest way to find duplicates on a large drive?
Use a tool that first filters by size, then hashes only same-size files. rdfind is efficient because it sorts by size and uses a quick hash. For very large drives, consider using czkawka which is optimized for speed.

Does fdupes work on NTFS or other filesystems?
Yes, fdupes works on any mounted filesystem, including NTFS, exFAT, and network shares, as long as you have read permission. However, hard-link creation may not work on filesystems that don’t support hard links (like FAT32).

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Everyday Math bed →