How to Control fdupes Deletion Order: Keep Duplicate Files by Directory Priority

How to use fdupes directory argument order to control duplicate-file retention priority, including a, b, c directory scenarios and subdirectory ordering.

When using fdupes to delete duplicate files across three directories, such as a, b, and c, and you want to keep a first, then b, and delete duplicates from c first, the key is not a complex rule. It is the order of directory arguments.

In non-interactive delete mode, fdupes keeps the first file it sees in each duplicate group and deletes later duplicates. Therefore, directory arguments should be arranged from highest retention priority to lowest.

In other words, to achieve “delete from c first, then b, and keep a as much as possible”, write the command like this:

1
fdupes -rdN a b c

The scan order is a -> b -> c. When the same file exists in all three directories, the file in a is found first and kept, while duplicates in b and c are deleted. If only b and c contain duplicates, b is kept and c is deleted.

Parameter Meaning

Common parameters are:

  • -r: recursively scan subdirectories.
  • -d: delete duplicate files.
  • -N: when used with -d, skip interactive confirmation, keep the first file in each duplicate group, and delete the rest.

Therefore, the basic format for automatic duplicate deletion is:

1
fdupes -rdN 目录A 目录B 目录C

The earlier a directory appears, the higher its retention priority. The later it appears, the more likely its duplicate files are to be deleted.

Preview Before Deleting

Using -dN deletes files directly, so it is better to preview duplicate groups first:

1
fdupes -r a b c

The output is grouped by duplicate files. In each group, the file shown earlier is the one more likely to be kept in non-interactive deletion mode.

You can also view summary information:

1
fdupes -rm a b c

If the data is important, save the result and inspect it manually:

1
fdupes -r a b c > duplicates.txt

After confirming that the order within each duplicate group matches your expectations, run:

1
fdupes -rdN a b c

How Subdirectories Are Handled

As long as -r is enabled, fdupes recursively scans all files under the directories you pass in. Retention priority is still determined by the order in which paths appear in the command.

For example:

1
fdupes -rdN dir_a dir_b dir_c

This means:

  • dir_a has the highest priority.
  • dir_b comes next.
  • dir_c has the lowest priority.

If dir_a/sub1/file.txt and dir_c/sub1/file.txt have identical content, the file under dir_a is kept. If dir_a/x/y/file.txt and dir_c/file.txt have identical content, the file under dir_a is still kept first. fdupes compares file content; filenames and directory depth do not need to match.

Precisely Controlling Subdirectory Priority

If you only pass parent directories, the scan order inside subdirectories is determined by fdupes traversal behavior. This is enough in most cases. But if you want a specific subdirectory to have higher priority, write it explicitly before its parent directory.

For example, suppose you want to keep dir_a first, then keep dir_b/special, then process the rest of dir_b, and finally process dir_c:

1
fdupes -rdN dir_a dir_b/special dir_b dir_c

This makes dir_b/special scan before dir_b. When dir_b is scanned later, files under special have already been recorded, so that subdirectory effectively has higher priority than the rest of dir_b.

This pattern is useful when:

  • a is the most important baseline directory.
  • A subdirectory inside b is more important than the rest of b.
  • c is mainly a low-priority backup directory.

The path order can be extended further:

1
fdupes -rdN a b/important b c/keep-first c

The rule is still the same: the earlier it appears, the more likely it is to be kept.

Use a List for Many Directories

If there are many directories and subdirectories, manually writing a long command is error-prone. You can write paths into a text file such as folders.txt, ordered by priority:

1
2
3
4
5
/path/to/dir_a
/path/to/dir_b/sub_important
/path/to/dir_b
/path/to/dir_c/sub_1
/path/to/dir_c

Then pass them to fdupes with xargs:

1
cat folders.txt | xargs fdupes -rdN

If paths may contain spaces, use null-separated input for better safety:

1
tr '\n' '\0' < folders.txt | xargs -0 fdupes -rdN

Important Boundaries

First, fdupes compares file content, not filenames. Two files with completely different names can still be treated as duplicates if their content is identical.

Second, if directory a contains duplicates internally, fdupes -rdN a b c may also delete later duplicates inside a. This command means “keep the first file according to the overall scan order”, not “never delete anything under a”.

Third, by default, fdupes does not follow symbolic links. If you need to handle files behind symlinks, confirm whether -s is needed and whether that matches your data-safety expectations.

Fourth, fdupes only deletes duplicate files. It does not clean up empty directories. After deletion, if b and c contain empty folders, you can run:

1
find b c -type d -empty -delete

Safer Operating Habit

If the directories contain important data, do not start with -rdN. A safer workflow is:

  1. Run fdupes -r a b c first to view duplicate groups.
  2. Confirm that the first file in each group is the one you want to keep.
  3. Then run fdupes -rdN a b c for automatic deletion.
  4. After deletion, check whether empty directories need cleanup.

If you are very worried about accidentally deleting files under a, first clean a smaller range of low-priority directories, or export the results and filter them manually. The directory order in fdupes is useful, but it is not an access-control rule. Once a path is included in the scan, duplicate files inside it may participate in deletion decisions.

Summary

To delete duplicate files with fdupes by priority, put the directories you want to keep earlier and the directories you want to delete from later.

To keep a, then b, and delete from c first:

1
fdupes -rdN a b c

To give a subdirectory higher priority, write it before its parent directory:

1
fdupes -rdN a b/important b c

The key sentence is simple: fdupes -dN keeps duplicate files that appear first and deletes duplicates that appear later. Directory order is your retention priority.

记录并分享
Built with Hugo
Theme Stack designed by Jimmy