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

方法三:使用Find命令解析" >
sudo add-apt-repository ppa:hsoft/ppasudo apt-get updatesudo apt-get install dupeguru*
Copy after login

方法三:使用Find命令解析

Find duplicate files using Linux

Aug 03, 2023 pm 03:51 PM
linux



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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

See all articles