Find duplicate files using Linux

Release: 2023-08-03 15:51:00
forward
1837 people have browsed it


Find duplicate files using Linux
#Method 1: Use the Find command

This section is an extended usage description of the powerful function of find. On the basis of find, we can combine it with other basic Linux commands (such as the xargs command) to create unlimited command line functions. For example, we can quickly find files in a Linux folder and its subfolders. List of duplicate files. The process to implement this function is relatively simple. Just search and traverse all the files, and then use the command to compare the MD5 of each file.

It sounds abstract, but in fact there is only one command:

find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
Copy after login

  • find -not -empty -type f -printf "%sn" means use find The command searches out all non-empty files and prints out their sizes.

  • sort -rn Needless to say, this command is based on file size. Reverse sort

  • ##uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 means only duplicate lines are printed , used here means to print out files with the same file name

  • ##uniq -w32 –all-repeated=separate Finally, this means to print out the first 32 bytes of MD5 In contrast, the entire process of filtering out duplicate files using the command line is so simple and easy.
  • Method 2: Use dupeGuru tool
DupeGuru is a cross-platform application, available in Linux, Windows and Mac OS X versions. It can pass file size, Various standards such as MD5 and file names are used to help users find duplicate files in Linux. Ubuntu users can install it directly by adding the following PPA source:

sudo add-apt-repository ppa:hsoft/ppasudo apt-get updatesudo apt-get install dupeguru*
Copy after login

方法三:使用Find命令解析

在工作生活当中,我们很可能会遇到查找重复文件的问题。比如从某游戏提取的游戏文本有重复的,我们希望找出所有重复的文本,让翻译只翻译其中一份,而其他的直接替换。那么这个问题该怎么做呢?当然方法多种多样,而且无论那种方法应该都不会太难,但笔者第一次遇到这个问题的时候第一反应是是用Linux的Shell脚本,所以文本介绍这种方式。

先上代码:

find -not -empty -type f -printf "%sn" | sort -rn |uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate | cut -b 36-
Copy after login

大家先cd到自己想要查找重复文件的文件夹,然后copy上面代码就可以了,系统会对当前文件夹及子文件夹内的所有文件进行查重。

下面分析一下上面的命令。

首先看第一句:

find -not -empty -type f -printf "%sn"
Copy after login

find是查找命令;-not -empty是要寻找非空文件;-type f是指寻找常规文件;-printf “%sn”比较具有迷惑性,这里的%s并非C语言中的输出字符串,它实际表示的是文件的大小,单位为bytes(不懂就man,man一下find,就可以看到了),n是换行符。所以这句话的意思是输出所有非空文件的大小。

搜索公众号GitHub猿后台回复“UML”,获取一份惊喜礼包。

通过管道,上面的结果被传到第二句:

sort -rn
Copy after login

sort是排序,-n是指按大小排序,-r是指从大到小排序(逆序reverse)。

第三句:

uniq -d
Copy after login

uniq是把重复的只输出一次,而-d指只输出重复的部分(如9出现了5次,那么就输出1个9,而2只出现了1次,并非重复出现的数字,故不输出)。

第四句:

xargs -I{} -n1 find -type f -size {}c -print0
Copy after login

这一部分分两部分看,第一部分是xargs -I{} -n1,xargs命令将之前的结果转化为参数,供后面的find调用,其中-I{}是指把参数写成{},而-n1是指将之前的结果一个一个输入给下一个命令(-n8就是8个8个输入给下一句,不写-n就是把之前的结果一股脑的给下一句)。后半部分是find -type f -size {}c -print0,find指令我们前面见过,-size{}是指找出大小为{}bytes的文件,而-print0则是为了防止文件名里带空格而写的参数。

第五句:

xargs -0 md5sum
Copy after login

xargs我们之前说过,是将前面的结果转化为输入,那么这个-0又是什么意思?man一下xargs,我们看到-0表示读取参数的时候以null为分隔符读取,这也不难理解,毕竟null的二进制表示就是00。后面的md5sum是指计算输入的md5值。

第六句:sort是排序,这个我们前面也见过。

第七句:

uniq -w32 --all-repeated=separate
Copy after login

uniq -w32是指寻找前32个字符相同的行,原因在于md5值一定是32位的,而后面的--all-repeated=separate是指将重复的部分放在一类,分类输出。

第八句:

cut -b 36-
Copy after login

由于我们的结果带着md5值,不是很好看,所以我们截取md5值后面的部分,cut是文本处理函数,这里-b 36-是指只要每行36个字符之后的部分。

我们将上述每个命令用管道链接起来,存入result.txt:

find -not -empty -type f -printf "%sn" | sort -rn |uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate | cut -b 36- >result.txt
Copy after login

虽然结果很好看,但是有一个问题,这是在Linux下很好看,实际上如果有朋友把输出文件放到Windows上,就会发现换行全没了,这是由于Linux下的换行是n,而windows要求nr,为了解决这个问题,我们最后执行一条指令,将n转换为nr:

cat result.txt | cut -c 36- | tr -s 'n'
Copy after login

The above is the detailed content of Find duplicate files using Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Linux中文社区
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!