Article / 文章中心

Fdupes - 在 Linux 中查找和删除重复文件的命令行工具

发布时间:2022-04-11 点击数:744
Fdupes是由Adrian Lopez用C编程语言编写的 Linux 实用程序,在 MIT 许可下发布。该应用程序能够在给定的目录和子目录集中找到重复文件。


概述

对于大多数计算机用户来说,查找和替换重复文件是一个常见的要求。查找和删除重复文件是一项繁重的工作,需要时间和耐心。如果你使用的系统是windows系统,DuplicateFilesDeleter可以帮你轻松解决问题。如果你使用的是Linux系统,你也不用担心,查找重复文件也会是一件非常容易事情,这就是 fdupes 实用程序。

编辑搜图

什么是 fdupes?

Fdupes是由Adrian Lopez用C编程语言编写的 Linux 实用程序,在 MIT 许可下发布。该应用程序能够在给定的目录和子目录集中找到重复文件。Fdupes 通过比较文件的 MD5 签名,然后进行逐字节比较来识别重复项。Fdupes 可以传递许多选项来列出、删除和替换带有重复文件的硬链接的文件。

比较按顺序开始:

大小比较>部分 MD5 签名比较>完整 MD5 签名比较>逐字节比较。

在 Linux 上安装 fdupes

在基于CentOS / RHEL和Fedora的系统上,您需要打开epel 存储库来安装 fdupes 包。

复制
# yum install fdupes# dnf install fdupes [centos8]1.2.

注意:从centos 8开始,默认的包管理器yum被dnf取代.

如何使用 fdupes 命令?

复制
$ fdupes -h用法: fdupes [options] 目录...
 -r --recurse 为每个给定的目录跟随子目录
                    内遇到
 -R --recurse:对于在此选项之后给出的每个目录,请遵循
                    内遇到的子目录(注意':'在
                    选项的结尾,更多详细信息的手册页)
 -s --symlinks 跟随符号链接
 -H --hardlinks 通常,当两个或多个文件指向同一个文件时
                    磁盘区域它们被视为非重复;这
                    选项将改变这种行为
 -n --noempty 排除零长度文件
 -A --nohidden 排除隐藏文件
 -f --omitfirst 忽略每组匹配中的第一个文件 -1 --sameline 在一行中列出每组匹配项
 -S --size 显示重复文件的大小
 -m --summarize 总结重复信息
 -q --quiet 隐藏进度指示器
 -d --delete 提示用户输入要保留和删除的所有文件
                    其他; 重要:在特定情况下,
                    一起使用此选项时可能会丢失数据
                    使用 -s 或 --symlinks,或者当指定一个特定目录不止一次;参考fdupes 文档以获取更多信息
 -N --noprompt 和 --delete 一起,保留第一个文件
                    每组重复并删除其余的没有提示用户
 -v --version 显示 fdupes 版本
 -h --help 显示此帮助信息1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.

fdupes使用演示

1、出于演示目的,让我们在一个目录下创建几个重复文件,如下所示:

复制
mkdir /home/dba/test && cd /home/dba/test && for i in {1..15}; do echo "hello world" > file${i}.txt ; done1.

运行上述命令后,让我们使用ls 命令验证是否创建了重复文件。

复制
[root@192_168_209_128 test]# ls -ltotal 60-rw-r--r-- 1 root root 12 Apr 10 23:02 file10.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file11.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file12.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file13.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file14.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file15.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file1.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file2.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file3.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file4.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file5.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file6.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file7.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file8.txt-rw-r--r-- 1 root root 12 Apr 10 23:02 file9.txt1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.

上面的脚本创建了15 个文件,即 file1.txt、file2.txt…file15.txt,每个文件都包含相同的数据,即,

复制
"hello world"1.

2、现在在文件夹test中搜索重复文件。

复制
[root@192_168_209_128 test]# fdupes /home/dba/test/home/dba/test/file1.txt                /home/dba/test/file2.txt/home/dba/test/file3.txt/home/dba/test/file4.txt/home/dba/test/file5.txt/home/dba/test/file6.txt/home/dba/test/file7.txt/home/dba/test/file8.txt/home/dba/test/file9.txt/home/dba/test/file10.txt/home/dba/test/file11.txt/home/dba/test/file12.txt/home/dba/test/file13.txt/home/dba/test/file14.txt/home/dba/test/file15.txt1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.

3、使用-r 选项在每个目录(包括其子目录)下递归搜索重复项。

它递归地搜索所有文件和文件夹,具体取决于文件和文件夹的数量,扫描重复项需要一些时间。同时,您将看到终端的总进度,类似这样。

复制
[root@192_168_209_128 test]#  fdupes -r /homeProgress [2544/3628] 70% 1.2.

4、使用-S选项查看在文件夹中找到的重复项的大小。

复制
[root@192_168_209_128 test]# fdupes -S /home/dba/test12 bytes each:                          
/home/dba/test/file1.txt/home/dba/test/file2.txt/home/dba/test/file3.txt/home/dba/test/file4.txt/home/dba/test/file5.txt/home/dba/test/file6.txt/home/dba/test/file7.txt/home/dba/test/file8.txt/home/dba/test/file9.txt/home/dba/test/file10.txt/home/dba/test/file11.txt/home/dba/test/file12.txt/home/dba/test/file13.txt/home/dba/test/file14.txt/home/dba/test/file15.txt1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.

5、您可以同时使用-S和-r选项查看遇到的每个目录和子目录的重复文件大小,如下所示:

复制
[root@192_168_209_128 test]# fdupes -Sr /home/dba/test |more315 bytes each:                         
/home/dba/test/etc/firewalld/zones/public.xml/home/dba/test/etc/firewalld/zones/public.xml.old39 bytes each:
/home/dba/test/etc/subuid-/home/dba/test/etc/subgid-4364 bytes each:
/home/dba/test/etc/vmware-tools/poweroff-vm-default/home/dba/test/etc/vmware-tools/poweron-vm-default/home/dba/test/etc/vmware-tools/resume-vm-default/home/dba/test/etc/vmware-tools/suspend-vm-default984 bytes each:
/home/dba/test/etc/sane.d/dc210.conf/home/dba/test/etc/sane.d/dc240.conf13 bytes each:
/home/dba/test/etc/sane.d/s9036.conf/home/dba/test/etc/sane.d/nec.conf1 byte each:
/home/dba/test/etc/at.deny/home/dba/test/etc/resolv.conf.save104 bytes each:
/home/dba/test/etc/dconf/db/gdm/home/dba/test/etc/dconf/db/site/home/dba/test/etc/dconf/db/local4504 bytes each:--More--1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.

6、除了递归搜索一个文件夹或所有文件夹外,您可以根据需要选择两个文件夹或三个文件夹。更不用说,如果需要,您可以使用选项-S和/或-r。

复制
$ fdupes /home/avi/Desktop/ /home/avi/Templates/1.

7、要在保留副本的同时删除重复文件,您可以使用选项'-d'。使用此选项时应格外小心,否则您可能最终会丢失必要的文件/数据,并注意该过程是不可恢复的。

复制
[root@192_168_209_128 test]# fdupes -d /home/dba/test [1] /home/dba/test/file1.txt            [2] /home/dba/test/file2.txt[3] /home/dba/test/file3.txt[4] /home/dba/test/file4.txt[5] /home/dba/test/file5.txt[6] /home/dba/test/file6.txt[7] /home/dba/test/file7.txt[8] /home/dba/test/file8.txt[9] /home/dba/test/file9.txt[10] /home/dba/test/file10.txt[11] /home/dba/test/file11.txt[12] /home/dba/test/file12.txt[13] /home/dba/test/file13.txt[14] /home/dba/test/file14.txt[15] /home/dba/test/file15.txtSet 1 of 1, preserve files [1 - 15, all]: 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.

您可能会注意到列出了所有重复项,并提示您删除,一个一个或某个范围或一次全部删除。您可以选择类似下面的范围来删除特定范围的文件文件。

复制
Set 1 of 1, preserve files [1 - 15, all]: 2-15
   [-] /home/dba/test/file1.txt
   [+] /home/dba/test/file2.txt
   [-] /home/dba/test/file3.txt
   [-] /home/dba/test/file4.txt
   [-] /home/dba/test/file5.txt
   [-] /home/dba/test/file6.txt
   [-] /home/dba/test/file7.txt
   [-] /home/dba/test/file8.txt
   [-] /home/dba/test/file9.txt
   [-] /home/dba/test/file10.txt
   [-] /home/dba/test/file11.txt
   [-] /home/dba/test/file12.txt
   [-] /home/dba/test/file13.txt
   [-] /home/dba/test/file14.txt
   [-] /home/dba/test/file15.txt1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.

8、从安全的角度来看,您可能希望将“fdupes”的输出打印到文件中,然后检查文本文件以确定要删除的文件。这减少了意外删除文件的机会。你可以这样做:

复制
$ fdupes -Sr /home > /home/fdupes.txt1.

注意:您可以将“/home”替换为您想要的文件夹。如果要分别搜索递归和打印大小,也可以使用选项“-r”和“-S” 。

9、您可以使用选项-f省略每组匹配项中的第一个文件。

首先列出目录的文件。

复制
[root@192_168_209_128 test]# ls -l /home/dba/testtotal 60-rw-r--r-- 1 root root 12 Apr 10 23:22 file10.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file11.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file12.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file13.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file14.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file15.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file1.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file2.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file3.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file4.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file5.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file6.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file7.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file8.txt-rw-r--r-- 1 root root 12 Apr 10 23:22 file9.txt1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.

然后从每组匹配项中省略第一个文件。

复制
[root@192_168_209_128 test]# fdupes -f  /home/dba/test/home/dba/test/file2.txt                /home/dba/test/file3.txt/home/dba/test/file4.txt/home/dba/test/file5.txt/home/dba/test/file6.txt/home/dba/test/file7.txt/home/dba/test/file8.txt/home/dba/test/file9.txt/home/dba/test/file10.txt/home/dba/test/file11.txt/home/dba/test/file12.txt/home/dba/test/file13.txt/home/dba/test/file14.txt/home/dba/test/file15.txt1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.

10、检查已安装的 fdupes 版本。

复制
[root@192_168_209_128 test]#  fdupes -versionfdupes 1.6.1