如何在Linux上识别同样内容的文件

系统 Linux
有时文件副本相当于对硬盘空间的巨大浪费,并会在你想要更新文件时造成困扰。以下是用来识别这些文件的六个命令。

[[267593]]

有时文件副本相当于对硬盘空间的巨大浪费,并会在你想要更新文件时造成困扰。以下是用来识别这些文件的六个命令。

在最近的帖子中,我们看了如何识别并定位硬链接的文件(即,指向同一硬盘内容并共享 inode)。在本文中,我们将查看能找到具有相同内容,却不相链接的文件的命令。

硬链接很有用是因为它们能够使文件存放在文件系统内的多个地方却不会占用额外的硬盘空间。另一方面,有时文件副本相当于对硬盘空间的巨大浪费,在你想要更新文件时也会有造成困扰之虞。在本文中,我们将看一下多种识别这些文件的方式。

用 diff 命令比较文件

可能比较两个文件最简单的方法是使用 diff 命令。输出会显示你文件的不同之处。<> 符号代表在当参数传过来的***个(<)或第二个(>)文件中是否有额外的文字行。在这个例子中,在 backup.html 中有额外的文字行。

  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
  2. $

diff 的***缺点是它一次只能比较两个文件并且你必须指定用来比较的文件,这篇帖子中的一些命令可以为你找到多个重复文件。

使用校验和

cksum(checksum) 命令计算文件的校验和。校验和是一种将文字内容转化成一个长数字(例如2819078353 228029)的数学简化。虽然校验和并不是完全独有的,但是文件内容不同校验和却相同的概率微乎其微。

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

在上述示例中,你可以看到产生同样校验和的第二个和第三个文件是如何可以被默认为相同的。

使用 find 命令

虽然 find 命令并没有寻找重复文件的选项,它依然可以被用来通过名字或类型寻找文件并运行 cksum 命令。例如:

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

使用 fslint 命令

fslint 命令可以被特地用来寻找重复文件。注意我们给了它一个起始位置。如果它需要遍历相当多的文件,这就需要花点时间来完成。注意它是如何列出重复文件并寻找其它问题的,比如空目录和坏 ID。

  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

你可能需要在你的系统上安装 fslint。你可能也需要将它加入你的命令搜索路径:

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

使用 rdfind 命令

rdfind 命令也会寻找重复(相同内容的)文件。它的名字意即“重复数据搜寻”,并且它能够基于文件日期判断哪个文件是原件——这在你选择删除副本时很有用因为它会移除较新的文件。

  1. $ rdfind ~
  2. Now scanning "/home/shark", 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/shark", 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)的功能。查看 man 页面获取解释。

  1. -ignoreempty ignore empty files
  2. -minsize ignore files smaller than speficied size
  3. -followsymlinks follow symbolic links
  4. -removeidentinode remove files referring to identical inode
  5. -checksum identify checksum type to be used
  6. -deterministic determiness how to sort files
  7. -makesymlinks turn duplicate files into symbolic links
  8. -makehardlinks replace duplicate files with hard links
  9. -makeresultsfile create a results file in the current directory
  10. -outputname provide name for results file
  11. -deleteduplicates delete/unlink duplicate files
  12. -sleep set sleep time between reading files (milliseconds)
  13. -n, -dryrun display what would have been done, but don't do it

注意 rdfind 命令提供了 -deleteduplicates true 的设置选项以删除副本。希望这个命令语法上的小问题不会惹恼你。;-)

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

你将可能需要在你的系统上安装 rdfind 命令。试验它以熟悉如何使用它可能是一个好主意。

使用 fdupes 命令

fdupes 命令同样使得识别重复文件变得简单。它同时提供了大量有用的选项——例如用来迭代的 -r。在这个例子中,它像这样将重复文件分组到一起:

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

这是使用迭代的一个例子,注意许多重复文件是重要的(用户的 .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

fdupe 命令的许多选项列如下。使用 fdupes -h 命令或者阅读 man 页面获取详情。

  1. -r --recurse recurse
  2. -R --recurse: recurse through specified directories
  3. -s --symlinks follow symlinked directories
  4. -H --hardlinks treat hard links as duplicates
  5. -n --noempty ignore empty files
  6. -f --omitfirst omit the first file in each set of matches
  7. -A --nohidden ignore hidden files
  8. -1 --sameline list matches on a single line
  9. -S --size show size of duplicate files
  10. -m --summarize summarize duplicate files information
  11. -q --quiet hide progress indicator
  12. -d --delete prompt user for files to preserve
  13. -N --noprompt when used with --delete, preserve the first file in set
  14. -I --immediate delete duplicates as they are encountered
  15. -p --permissions don't soncider files with different owner/group or
  16. permission bits as duplicates
  17. -o --order=WORD order files according to specification
  18. -i --reverse reverse order while sorting
  19. -v --version display fdupes version
  20. -h --help displays help

fdupes 命令是另一个你可能需要安装并使用一段时间才能熟悉其众多选项的命令。

总结

Linux 系统提供能够定位并(潜在地)能移除重复文件的一系列的好工具,以及能让你指定搜索区域及当对你所发现的重复文件时的处理方式的选项。

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2019-04-25 13:30:14

Linux文件分身

2020-12-28 06:44:45

FedoraLinux RPM文件

2023-12-20 22:04:17

Linux二进制文件

2020-03-06 08:56:41

Linux运算符文本

2019-02-25 15:36:52

Linux复制文件远程系统

2021-07-10 11:20:44

FreeDOS归档文件

2019-09-03 08:57:52

Linux命令软件

2017-05-03 15:30:38

LinuxMeld比较文件夹

2018-12-11 11:00:50

Linux字体命令

2020-04-21 14:21:31

LinuxPython

2021-10-12 08:43:19

LinuxSambaWindows

2021-10-02 10:10:47

LinuxBusyBox命令

2020-09-30 06:00:00

Linux误删除恢复文件

2015-03-30 11:34:19

LinuxFSlint

2019-09-16 11:40:49

Linux交换文件

2014-12-03 08:53:59

eCryptFS加密文件

2015-08-04 14:04:28

UbuntuPDF文件

2021-08-18 11:19:25

FedoraLinuxJava

2017-03-29 16:18:11

LinuxUbuntuRedmine

2021-09-11 15:41:55

UbuntuDropbox云服务
点赞
收藏

51CTO技术栈公众号