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




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

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

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

1. 使用 diff 命令比较文件

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

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

<code>$ diff index.html backup.html
2438a2439,2441
>

> That's all there is to report.
>
/<code>

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

<code>$ diff home.html index.html
$/<code>

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

2. 使用校验和

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

<code>$ cksum *.html
2819078353 228029 backup.html
4073570409 227985 home.html
4073570409 227985 index.html/<code>

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

3. 使用 find 命令

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

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

4. 使用 fslint 命令

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

<code>$ fslint .
-----------------------------------file name lint
-------------------------------Invalid utf8 names
-----------------------------------file case lint
----------------------------------DUPlicate files\t<==
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/<code>

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

<code>$ export PATH=$PATH:/usr/share/fslint/fslint/<code>

5. 使用 rdfind 命令

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

<code>$ 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/<code>

我们还可以在 dryrun 中运行。

<code>$ 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/<code>

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

选项 意义 -ignoreempty 忽略空文件 -minsize 忽略小于特定大小的文件 -followsymlink 遵循符号链接 -removeidentinode 删除引用相同inode的文件 -checksum 标识要使用的校验和类型 -deterministic 决定如何排序文件 -makesymlinks 将重复文件转换为符号链接 -makehardlinks 用硬链接替换重复文件 -makeresultsfile 在当前目录中创建结果文件 -outputname 提供结果文件的名称 -deleteduplicates 删除/取消链接重复文件 -sleep 设置读取文件之间的休眠时间(毫秒) -n,-dryrun 显示本应执行的操作,但不要执行

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

<code>$ rdfind -deleteduplicates true .
...
Deleted 1 files.\t<==/<code>

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

6. 使用 fdupes 命令

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

<code>$ 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/<code>

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

<code># 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/<code>

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

选项 意义 -r --recurse 递归 -R --recurse 递归指定的目录 -s --symlinks-H --hardlinks 遵循符号链接目录 -H --hardlinks 将硬链接视为重复链接 -n --noempty 忽略空文件 -f --omitfirst 省略每组匹配中的第一个文件 -A --nohidden 忽略隐藏文件 -1 --sameline 相同列表匹配单行 -S --size 显示重复文件的大小 -m --summarize 汇总重复文件信息 -q --quiet 进度指示器 -d --delete 提示用户保存文件 -N --noprompt 与--delete一起使用时无效,保留集合中的第一个文件 -I --immediate 在遇到它们时删除重复项 -p --permissions 权限不会将具有不同所有者/组或权限位的SONCIDER文件作为重复项 -o --order=WORD 根据规范的WORD订单文件 -i --reverse 排序时反向逆序 -v --version 显示fdupes版本 -h --help 显示帮助

小结

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

作者:良许Linux
原文链接:https://juejin.im/post/5e671a53518825494b3ce22c


分享到:


相關文章: