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

文章目录

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

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

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

比较按顺序开始:

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

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

# yum install fdupes
# dnf install fdupes [centos8]

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

$ 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 显示此帮助信息

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

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

[root@192_168_209_128 test]# ls -l
total 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.txt

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

"hello world"

[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.txt

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

[root@192_168_209_128 test]# fdupes -r /home
Progress [2544/3628] 70%

[root@192_168_209_128 test]# fdupes -S /home/dba/test
12 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.txt

[root@192_168_209_128 test]# fdupes -Sr /home/dba/test |more
315 bytes each:
/home/dba/test/etc/firewalld/zones/public.xml
/home/dba/test/etc/firewalld/zones/public.xml.old
39 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-default
984 bytes each:
/home/dba/test/etc/sane.d/dc210.conf
/home/dba/test/etc/sane.d/dc240.conf
13 bytes each:
/home/dba/test/etc/sane.d/s9036.conf
/home/dba/test/etc/sane.d/nec.conf
1 byte each:
/home/dba/test/etc/at.deny
/home/dba/test/etc/resolv.conf.save
104 bytes each:
/home/dba/test/etc/dconf/db/gdm
/home/dba/test/etc/dconf/db/site
/home/dba/test/etc/dconf/db/local
4504 bytes each:
--More--

$ fdupes /home/avi/Desktop/ /home/avi/Templates/

[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.txt
Set 1 of 1, preserve files [1 - 15, all]:

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

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.txt

$ fdupes -Sr /home > /home/fdupes.txt

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

首先列出目录的文件。

[root@192_168_209_128 test]# ls -l /home/dba/test
total 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.txt

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

[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.txt

[root@192_168_209_128 test]# fdupes -version
fdupes 1.6.1

© 版权声明

相关文章