如何找到系统里的重复文件,快速释放磁盘空间?

☞ 程序员进阶架构师必备资源免费送 ☜

不管是 Windows 电脑还是 Linux 电脑,在使用的过程中,或多或少都会留下很多重复的文件。 这些文件不仅会占用我们的磁盘,还会拖累我们的系统,所以,很有必要干掉这些重复的文件。

本文将介绍 6 种方法找到系统里的重复文件,让你快速释放硬盘空间!

1. 使用 diff 命令比较文件

在我们平常操作当中,比较两个文件的差异最简单的方法可能就是使用 diff 命令。 diff 命令的输出将使用 < > 符号显示两个文件之间的差异,利用这个特性我们可以找到相同的文件。

当两个文件有差异时,diff 命令将输出差异点:

$ diff index.html backup.html
2438a2439,2441
> 
> That's all there is to report.
> 

如果你的 diff 命令没有输出,则表示两个文件相同:

$ diff home.html index.html
$

但是, diff 命令的缺点是它一次只能比较两个文件,如果我们要比较多个文件,这样两个两个比较效率肯定非常低下。

2. 使用校验和

校验和命令 cksum 会根据一定的算法将文件的内容计算成一个很长的数字(如2819078353 228029)。 虽然算出的结果不是绝对唯一,但是内容不相同的文件导致校验和相同的可能性跟中国男足进世界杯差不多。

$ cksum *.html
2819078353 228029 backup.html
4073570409 227985 home.html
4073570409 227985 index.html

在我们上面的操作中,我们可以看到第二个和第三个文件校验和是相同的,所以我们可以认为这两个文件是一样的。

3. 使用 find 命令

虽然 find 命令没有查找重复文件的选项,但是它却可用于按名称或类型搜索文件并运行cksum 命令。 具体操作如下。

$ find . -name "*.html" -exec cksum {} \;
4073570409 227985 ./home.html
2819078353 228029 ./backup.html
4073570409 227985 ./index.html

4. 使用 fslint 命令

fslint 命令可以用来专门查找重复文件。 但是这里有个注意事项,就是我们需要给它一个起始位置。 如果我们需要运行大量文件,该命令可能需要相当长的时间才能完成查找。

$ fslint .
-----------------------------------file name lint
-------------------------------Invalid utf8 names
-----------------------------------file case lint
----------------------------------DUPlicate files    <==
home.html
index.html
-----------------------------------Dangling links
--------------------redundant characters in links
------------------------------------suspect links
--------------------------------Empty Directories
./.gnupg
----------------------------------Temporary Files
----------------------duplicate/conflicting Names
------------------------------------------Bad ids
-------------------------Non Stripped executables

Tips 我们必须在系统上安装 fslint ,还需要将它添加到搜索路径中:

$ export PATH=$PATH:/usr/share/fslint/fslint

5. 使用 rdfind 命令

rdfind 命令还将寻找重复的(相同内容的)文件。 被称为“冗余数据查找”,该命令可以根据文件日期确定哪些文件是原始文件,这对我们选择删除重复项很有帮助,因为它会删除较新的文件。

$ rdfind ~
Now scanning "/home/alvin", found 12 files.
Now have 12 files in total.
Removed 1 files due to nonunique device and inode.
Total size is 699498 bytes or 683 KiB
Removed 9 files due to unique sizes from list.2 files left.
Now eliminating candidates based on first bytes:removed 0 files from list.2 files left.
Now eliminating candidates based on last bytes:removed 0 files from list.2 files left.
Now eliminating candidates based on sha1 checksum:removed 0 files from list.2 files left.
It seems like you have 2 files that are not unique
Totally, 223 KiB can be reduced.
Now making results file results.txt

我们还可以在 dryrun 中运行。

$ rdfind -dryrun true ~
(DRYRUN MODE) Now scanning "/home/alvin", found 12 files.
(DRYRUN MODE) Now have 12 files in total.
(DRYRUN MODE) Removed 1 files due to nonunique device and inode.
(DRYRUN MODE) Total size is 699352 bytes or 683 KiB
Removed 9 files due to unique sizes from list.2 files left.
(DRYRUN MODE) Now eliminating candidates based on first bytes:removed 0 files from list.2 files left.
(DRYRUN MODE) Now eliminating candidates based on last bytes:removed 0 files from list.2 files left.
(DRYRUN MODE) Now eliminating candidates based on sha1 checksum:removed 0 files from list.2 files left.
(DRYRUN MODE) It seems like you have 2 files that are not unique
(DRYRUN MODE) Totally, 223 KiB can be reduced.
(DRYRUN MODE) Now making results file results.txt

rdfind 命令还提供一些忽略空文件(-ignoreempty)和跟随符号链接(-followsymlinks)之类的选项。 下面详细解释它的常用选项。

这里需要我们注意一下,rdfind命令提供了使用 -deleteduplicates true 设置删除重复文件的选项。 顾名思义,使用这个选项它将自动删重复的文件。

$ rdfind -deleteduplicates true .

. . .

Deleted 1 files.    <==

当然,前提是我们也必须在系统上安装 rdfind 命令。

6. 使用 fdupes 命令

fdupes 命令也可以很容易地识别重复文件,并提供了大量有用的选项。 在最简单的操作中,它会把重复文件放在一起,如下所示:

$ fdupes ~
/home/alvin/UPGRADE
/home/alvin/mytwin

/home/alvin/lp.txt
/home/alvin/lp.man

/home/alvin/penguin.png
/home/alvin/penguin0.png
/home/alvin/hideme.png

-r 选项代表递归,表示它将在各个目录下面使用递归的方式来查找重复文件。 但是,Linux 下有许多重复文件是很重要的(比如用户的 .bashrc 和 .profile 文件),如果被删除将导致系统异常。

# fdupes -r /home
/home/shark/home.html
/home/shark/index.html

/home/dory/.bashrc
/home/eel/.bashrc

/home/nemo/.profile
/home/dory/.profile
/home/shark/.profile

/home/nemo/tryme
/home/shs/tryme

/home/shs/arrow.png
/home/shs/PNGs/arrow.png

/home/shs/11/files_11.zip
/home/shs/ERIC/file_11.zip

/home/shs/penguin0.jpg
/home/shs/PNGs/penguin.jpg
/home/shs/PNGs/penguin0.jpg

/home/shs/Sandra_rotated.png
/home/shs/PNGs/Sandra_rotated.png

fdupes 命令的常用选项如下表所示:

小结

Linux 系统为我们提供了很多用于定位和删除重复文件的工具,使用这些工具将快速找到磁盘里的重复文件并删除它们。 希望本次分享能给大家带来帮助~