Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Find and Delete Duplicate Files in Linux

· 5 min read · Home Tech

In this article
  1. Why Duplicate Files Accumulate
  2. How Duplicate Detection Works
  3. Installing the Tools
  4. Using fdupes
  5. Using rdfind
  6. Using rmlint
  7. What to Do Before Deleting
  8. Frequently Asked Questions

To find and delete duplicate files in Linux, you use command-line tools like fdupes, rdfind, or rmlint, which compare file contents and list or remove exact copies. The safest approach is to first generate a list of duplicates, review it, and then delete files manually or with a script, rather than relying on automatic deletion.

Why Duplicate Files Accumulate

Duplicates are common on any system. They come from repeated downloads, unzipping archives into the same folder, backup tools that copy instead of move, photo libraries syncing from multiple devices, and applications that save config copies. Over time, they waste disk space and make backups larger and slower. On a busy server or a laptop with a small SSD, cleaning them can free gigabytes.

How Duplicate Detection Works

Simple tools compare file names and sizes first. Since files with different sizes cannot be identical, this quickly filters out most candidates. Then they compute a checksum (like MD5, SHA-1, or SHA-256) for files that have the same size. The checksum is a short digital fingerprint; if two files have the same checksum, they are almost certainly identical. Some tools do a byte-by-byte comparison as a final check to avoid the tiny chance of a checksum collision. Tools like fdupes and rmlint also let you compare only files of a certain type or ignore empty files.

Installing the Tools

Most distributions have these tools in their official repositories. On Debian or Ubuntu, use apt:

sudo apt install fdupes rdfind rmlint

On Fedora or RHEL, use dnf:

sudo dnf install fdupes rdfind rmlint

On Arch, use pacman:

sudo pacman -S fdupes rdfind rmlint

Using fdupes

fdupes is the classic tool. Run it on a directory to see a list of duplicate groups:

fdupes /home/user/Documents

It prints groups of files, each group separated by a blank line. To delete duplicates automatically, keeping the first file in each group, use the -d flag. This prompts you to choose which files to keep:

fdupes -d /home/user/Documents

It asks for each group: “Set 1 of 2, preserve files [1 - 2, all]? ”. Type the numbers of the files you want to keep (separated by spaces) or press Enter to keep all. To delete without prompting, use -d -N (keep the first file in each group) or -d -N -f (keep the first file, but list the others). Be careful: -N assumes you want the first file, which may not be the one you want.

Using rdfind

rdfind is another popular tool. It is non-interactive and can automatically delete duplicates, but it also has a dry-run mode. First, run it to see what it would do:

rdfind -dryrun true /home/user/Documents

It prints a list of actions, like “REMOVE” or “KEEP”. To actually delete, run:

rdfind -deleteduplicates true /home/user/Documents

By default, rdfind keeps the first file it finds (based on directory order). You can change the ranking with the -makeresultsfile option or by sorting by time. For example, to keep the oldest file, use -minsize and -maxsize to filter, and -deterministic to make the order predictable. A safer habit is to always run with -dryrun true first and review the output.

Using rmlint

rmlint is more modern and faster, especially for large directory trees. It can also find other lint like empty directories and broken symlinks. To find duplicates:

rmlint /home/user/Documents

It creates a shell script named rmlint.sh in the current directory. You can inspect this script and then run it to delete duplicates:

sh rmlint.sh

You can also use the -d option to delete directly, but the script approach is safer because you review the list first. rmlint supports many options, like -t to set the number of threads and -o to control output format. For example, to ignore files smaller than 1 KB:

rmlint -s 1k /home/user/Documents

What to Do Before Deleting

Always back up important data before running any deletion tool. Even a simple typo in a path can remove the wrong files. Also, consider the following:

  • Check the output carefully. Some tools list files that are not true duplicates if you use only name or size comparison.
  • Use a dry run or generate a list first. For fdupes, use -d without -N to see prompts; for rdfind, use -dryrun; for rmlint, inspect the generated script.
  • If you have hard links, deleting one link does not free space until all links are removed. Tools like rdfind can replace duplicates with hard links, which saves space without deleting data.
  • Be careful with system directories. Running these tools on /usr or /etc can break your system if it removes files that are not truly duplicates.

Frequently Asked Questions

What is the difference between fdupes, rdfind, and rmlint?

All three find duplicate files by content, but they differ in speed, features, and interface. fdupes is simple and interactive, good for small directories. rdfind is non-interactive and can replace duplicates with hard links, which is useful for saving space without deleting. rmlint is the fastest on large trees and also finds other lint like empty directories. For most users, fdupes is enough; for large backups, rmlint is better.

How do I delete duplicates without being prompted?

For fdupes, use fdupes -d -N /path to keep the first file in each group and delete the rest without asking. For rdfind, use rdfind -deleteduplicates true /path. For rmlint, run rmlint /path and then execute the generated rmlint.sh script. Always test with a dry run first.

Can I find duplicates across multiple directories?

Yes. All three tools accept multiple paths. For example, fdupes /home/user/Documents /home/user/Downloads will find duplicates across both directories. Be aware that if a duplicate exists in both, the tool may keep the one from the first directory listed.

What does “hard link” mean in duplicate detection?

A hard link is another name for the same file on disk. If two files are hard links, they share the same inode and data. Deleting one link does not delete the data until all links are removed. rdfind can replace duplicate files with hard links, which saves space without losing any file name. This is a safe way to reduce disk usage, but it means changes to one file affect the other.

Is it safe to delete duplicates automatically?

It can be safe if you are certain the files are true duplicates and you do not need both copies. However, automatic deletion can remove files you might want to keep, such as config files that are identical but used by different programs. Always review the list manually before deleting, and keep a backup of anything you are unsure about.