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

系统 Linux
Linux系统为我们提供了很多用于定位和删除重复文件的工具,使用这些工具将快速找到磁盘里的重复文件并删除它们。

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

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

[[282960]]

1. 使用 diff 命令比较文件

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

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

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

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

  1. $ diff home.html index.html 

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

2. 使用校验和

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

  1. $ cksum *.html 
  2. 2819078353 228029 backup.html 
  3. 4073570409 227985 home.html 
  4. 4073570409 227985 index.html 

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

3. 使用 find 命令

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

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

4. 使用 fslint 命令

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

  1. $ fslint . 
  2. -----------------------------------file name lint 
  3. -------------------------------Invalid utf8 names 
  4. -----------------------------------file case lint 
  5. ----------------------------------DUPlicate files    <== 
  6. home.html 
  7. index.html 
  8. -----------------------------------Dangling links 
  9. --------------------redundant characters in links 
  10. ------------------------------------suspect links 
  11. --------------------------------Empty Directories 
  12. ./.gnupg 
  13. ----------------------------------Temporary Files 
  14. ----------------------duplicate/conflicting Names 
  15. ------------------------------------------Bad ids 
  16. -------------------------Non Stripped executables 

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

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

5. 使用 rdfind 命令

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

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

我们还可以在 dryrun 中运行。

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

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

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

  1. $ rdfind -deleteduplicates true . 
  2. ... 
  3. Deleted 1 files.    <== 

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

6. 使用 fdupes 命令

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

  1. $ fdupes ~ 
  2. /home/alvin/UPGRADE 
  3. /home/alvin/mytwin 
  4.  
  5. /home/alvin/lp.txt 
  6. /home/alvin/lp.man 
  7.  
  8. /home/alvin/penguin.png 
  9. /home/alvin/penguin0.png 
  10. /home/alvin/hideme.png 

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

  1. # fdupes -r /home 
  2. /home/shark/home.html 
  3. /home/shark/index.html 
  4.  
  5. /home/dory/.bashrc 
  6. /home/eel/.bashrc 
  7.  
  8. /home/nemo/.profile 
  9. /home/dory/.profile 
  10. /home/shark/.profile 
  11.  
  12. /home/nemo/tryme 
  13. /home/shs/tryme 
  14.  
  15. /home/shs/arrow.png 
  16. /home/shs/PNGs/arrow.png 
  17.  
  18. /home/shs/11/files_11.zip 
  19. /home/shs/ERIC/file_11.zip 
  20.  
  21. /home/shs/penguin0.jpg 
  22. /home/shs/PNGs/penguin.jpg 
  23. /home/shs/PNGs/penguin0.jpg 
  24.  
  25. /home/shs/Sandra_rotated.png 
  26. /home/shs/PNGs/Sandra_rotated.png 

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

小结

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

 

责任编辑:赵宁宁 来源: 良许Linux
相关推荐

2019-11-20 10:23:51

磁盘WindowsLinux

2023-03-03 00:07:24

2022-11-06 19:34:53

UbuntuLinux

2023-03-05 22:11:20

删除文件磁盘

2020-01-10 16:00:16

Windows 10更新磁盘空间

2022-08-07 12:17:21

Snap磁盘

2023-04-18 23:31:59

Linux磁盘系统

2018-07-24 08:50:40

Linux磁盘空间磁盘利用率

2020-11-17 11:19:48

Linux磁盘空间

2010-04-08 15:24:36

Windows磁盘空间

2020-04-09 16:29:40

Windows 10系统更新微软

2015-11-25 13:37:52

磁盘空间LinuxUbuntu

2020-11-04 18:32:30

APTapt命令Linux

2020-11-25 08:41:56

Windows

2018-01-03 08:42:40

Linux命令磁盘空间

2011-01-18 10:25:19

Linux磁盘分区

2021-02-11 08:11:50

Window10Docker容器

2010-05-27 17:51:55

Linux查看磁盘空间

2009-08-21 10:22:37

Linux系统磁盘空间管理工具

2011-08-19 14:34:03

iPhone开发
点赞
收藏

51CTO技术栈公众号