linux中怎么找到系统里的重复文件并快速释放磁盘空间

发布时间:2021-10-25 14:57:35 作者:小新
来源:亿速云 阅读:206

这篇文章主要介绍linux中怎么找到系统里的重复文件并快速释放磁盘空间,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

1. 使用 diff 命令比较文件

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

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

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

如果你的 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)之类的选项。下面详细解释它的常用选项。

linux中怎么找到系统里的重复文件并快速释放磁盘空间

这里需要我们注意一下,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中怎么找到系统里的重复文件并快速释放磁盘空间

以上是“linux中怎么找到系统里的重复文件并快速释放磁盘空间”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 怎么在Linux系统中释放磁盘空间
  2. 怎么在Linux下快速找到被删除的文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux

上一篇:切片传递的隐藏危机有哪些

下一篇:Python爬虫经常会被封的原因是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》