
对于大多数计算机用户而言,查找并替换
重复的
文件是一个常见的需求。查找并移除重复文件真是一项令人不胜其烦的工作,它耗时又耗力。但如果你的机器上跑着GNU/Linux,那么查找重复文件会变得十分简单,这多亏了fdupes工具。 fdupes是啥东东? fdupes是Linux下的一个工具,它由Adrian Lopez用C编程语言编写并基于MIT许可证发行,该应用程序可以在指定的目录及子目录中查找重复的文件。fdupes通过对比文件的MD5签名,以及逐字节比较文件来识别重复内容,fdupes有各种选项,可以实现对文件的列出、删除、替换为文件副本的硬链接等 *** 作。 文件对比以下列顺序开始: 大小对比 >部分 MD5 签名对比 >完整 MD5 签名对比 >逐字节对比 安装 fdupes 到 Linux 在基于Debian的系统上,如Ubuntu和Linux Mint,安装最新版fdupes,用下面的命令手到擒来。
代码如下: $ sudo apt-get install fdupes 在基于CentOS/RHEL和Fedora的系统上,你需要开启epel仓库来安装fdupes包。 代码如下: # yum install fdupes # dnf install fdupes [在 Fedora 22 及其以后] 注意:自Fedora 22之后,默认的包管理器yum被dnf取代了。 fdupes命令如何使用 1、 作为演示的目的,让我们来在某个目录(比如 tecmint)下创建一些重复文件,命令如下: 代码如下: $ mkdir /home/"$USER"/Desktop/tecmint &&cd /home/"$USER"/Desktop/tecmint &&for i in {1..15}do echo "I Love Tecmint. Tecmint is a very nice community of Linux Users." >tecmint${i}.txt done 在执行以上命令后,让我们使用ls命令验证重复文件是否创建。 代码如下: $ ls -l total 60 -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint10.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint11.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint12.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint13.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint14.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint15.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint1.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint2.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint3.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint4.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint5.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint6.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint7.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint8.txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9.txt 上面的脚本创建了15个文件,名称分别为tecmint1.txt,tecmint2.txt……tecmint15.txt,并且每个文件的数据相同,如 "I Love Tecmint. Tecmint is a very nice community of Linux Users." 2、 现在在tecmint文件夹内搜索重复的文件。 $ fdupes /home/$USER/Desktop/tecmint /home/tecmint/Desktop/tecmint/tecmint13.txt /home/tecmint/Desktop/tecmint/tecmint8.txt /home/tecmint/Desktop/tecmint/tecmint11.txt /home/tecmint/Desktop/tecmint/tecmint3.txt /home/tecmint/Desktop/tecmint/tecmint4.txt /home/tecmint/Desktop/tecmint/tecmint6.txt /home/tecmint/Desktop/tecmint/tecmint7.txt /home/tecmint/Desktop/tecmint/tecmint9.txt /home/tecmint/Desktop/tecmint/tecmint10.txt /home/tecmint/Desktop/tecmint/tecmint2.txt /home/tecmint/Desktop/tecmint/tecmint5.txt /home/tecmint/Desktop/tecmint/tecmint14.txt /home/tecmint/Desktop/tecmint/tecmint1.txt /home/tecmint/Desktop/tecmint/tecmint15.txt /home/tecmint/Desktop/tecmint/tecmint12.txt 3、 使用-r选项在每个目录包括其子目录中递归搜索重复文件。 它会递归搜索所有文件和文件夹,花一点时间来扫描重复文件,时间的长短取决于文件和文件夹的数量。在此其间,终端中会显示全部过程,像下面这样。 代码如下: $ fdupes -r /home Progress [37780/54747] 69% 4、 使用-S选项来查看某个文件夹内找到的重复文件的大小。 代码如下: $ fdupes -S /home/$USER/Desktop/tecmint 65 bytes each: /home/tecmint/Desktop/tecmint/tecmint13.txt /home/tecmint/Desktop/tecmint/tecmint8.txt /home/tecmint/Desktop/tecmint/tecmint11.txt /home/tecmint/Desktop/tecmint/tecmint3.txt /home/tecmint/Desktop/tecmint/tecmint4.txt /home/tecmint/Desktop/tecmint/tecmint6.txt /home/tecmint/Desktop/tecmint/tecmint7.txt /home/tecmint/Desktop/tecmint/tecmint9.txt /home/tecmint/Desktop/tecmint/tecmint10.txt /home/tecmint/Desktop/tecmint/tecmint2.txt /home/tecmint/Desktop/tecmint/tecmint5.txt /home/tecmint/Desktop/tecmint/tecmint14.txt /home/tecmint/Desktop/tecmint/tecmint1.txt /home/tecmint/Desktop/tecmint/tecmint15.txt /home/tecmint/Desktop/tecmint/tecmint12.txt 5、 你可以同时使用-S和-r选项来查看所有涉及到的目录和子目录中的重复文件的大小,如下: 代码如下: $ fdupes -Sr /home/avi/Desktop/ 65 bytes each: /home/tecmint/Desktop/tecmint/tecmint13.txt /home/tecmint/Desktop/tecmint/tecmint8.txt /home/tecmint/Desktop/tecmint/tecmint11.txt /home/tecmint/Desktop/tecmint/tecmint3.txt /home/tecmint/Desktop/tecmint/tecmint4.txt /home/tecmint/Desktop/tecmint/tecmint6.txt /home/tecmint/Desktop/tecmint/tecmint7.txt /home/tecmint/Desktop/tecmint/tecmint9.txt /home/tecmint/Desktop/tecmint/tecmint10.txt /home/tecmint/Desktop/tecmint/tecmint2.txt /home/tecmint/Desktop/tecmint/tecmint5.txt /home/tecmint/Desktop/tecmint/tecmint14.txt /home/tecmint/Desktop/tecmint/tecmint1.txt /home/tecmint/Desktop/tecmint/tecmint15.txt /home/tecmint/Desktop/tecmint/tecmint12.txt 107 bytes each: /home/tecmint/Desktop/resume_files/r-csc.html /home/tecmint/Desktop/resume_files/fc.html 6、 不同于在一个或所有文件夹内递归搜索,你可以选择按要求有选择性地在两个或三个文件夹内进行搜索。不必再提醒你了吧,如有需要,你可以使用-S和/或-r选项。 代码如下: $ fdupes /home/avi/Desktop/ /home/avi/Templates/ 7、 要删除重复文件,同时保留一个副本,你可以使用-d选项。使用该选项,你必须额外小心,否则最终结果可能会是文件/数据的丢失。郑重提醒,此 *** 作不可恢复。 代码如下: $ fdupes -d /home/$USER/Desktop/tecmint [1] /home/tecmint/Desktop/tecmint/tecmint13.txt [2] /home/tecmint/Desktop/tecmint/tecmint8.txt [3] /home/tecmint/Desktop/tecmint/tecmint11.txt [4] /home/tecmint/Desktop/tecmint/tecmint3.txt [5] /home/tecmint/Desktop/tecmint/tecmint4.txt [6] /home/tecmint/Desktop/tecmint/tecmint6.txt [7] /home/tecmint/Desktop/tecmint/tecmint7.txt [8] /home/tecmint/Desktop/tecmint/tecmint9.txt [9] /home/tecmint/Desktop/tecmint/tecmint10.txt [10] /home/tecmint/Desktop/tecmint/tecmint2.txt [11] /home/tecmint/Desktop/tecmint/tecmint5.txt [12] /home/tecmint/Desktop/tecmint/tecmint14.txt [13] /home/tecmint/Desktop/tecmint/tecmint1.txt [14] /home/tecmint/Desktop/tecmint/tecmint15.txt [15] /home/tecmint/Desktop/tecmint/tecmint12.txt 代码如下: Set 1 of 1, preserve files [1 - 15, all]: 你可能注意到了,所有重复的文件被列了出来,并给出删除提示,一个一个来,或者指定范围,或者一次性全部删除。你可以选择一个范围,就像下面这样,来删除指定范围内的文件。 代码如下: Set 1 of 1, preserve files [1 - 15, all]: 2-15 [-] /home/tecmint/Desktop/tecmint/tecmint13.txt [+] /home/tecmint/Desktop/tecmint/tecmint8.txt [-] /home/tecmint/Desktop/tecmint/tecmint11.txt [-] /home/tecmint/Desktop/tecmint/tecmint3.txt [-] /home/tecmint/Desktop/tecmint/tecmint4.txt [-] /home/tecmint/Desktop/tecmint/tecmint6.txt [-] /home/tecmint/Desktop/tecmint/tecmint7.txt [-] /home/tecmint/Desktop/tecmint/tecmint9.txt [-] /home/tecmint/Desktop/tecmint/tecmint10.txt [-] /home/tecmint/Desktop/tecmint/tecmint2.txt [-] /home/tecmint/Desktop/tecmint/tecmint5.txt [-] /home/tecmint/Desktop/tecmint/tecmint14.txt [-] /home/tecmint/Desktop/tecmint/tecmint1.txt [-] /home/tecmint/Desktop/tecmint/tecmint15.txt [-] /home/tecmint/Desktop/tecmint/tecmint12.txt 8、 从安全角度出发,你可能想要打印fdupes的输出结果到文件中,然后检查文本文件来决定要删除什么文件。这可以降低意外删除文件的风险。你可以这么做: 代码如下: $ fdupes -Sr /home >/home/fdupes.txt 注意:你应该替换/home为你想要的文件夹。同时,如果你想要递归搜索并打印大小,可以使用-r和-S选项。 9、 你可以使用-f选项来忽略每个匹配集中的首个文件。 首先列出该目录中的文件。 代码如下: $ ls -l /home/$USER/Desktop/tecmint total 20 -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (3rd copy).txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (4th copy).txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (another copy).txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9 (copy).txt -rw-r--r-- 1 tecmint tecmint 65 Aug 8 11:22 tecmint9.txt 然后,忽略掉每个匹配集中的首个文件。 代码如下: $ fdupes -f /home/$USER/Desktop/tecmint /home/tecmint/Desktop/tecmint9 (copy).txt /home/tecmint/Desktop/tecmint9 (3rd copy).txt /home/tecmint/Desktop/tecmint9 (another copy).txt /home/tecmint/Desktop/tecmint9 (4th copy).txt 10、 检查已安装的fdupes版本。 复制代码 代码如下: $ fdupes --version fdupes 1.51 11、 如果你需要关于fdupes的帮助,可以使用-h开关。 $ fdupes -h Usage: fdupes [options] DIRECTORY... -r --recurse for every directory given follow subdirectories encountered within -R --recurse: for each directory given after this option follow subdirectories encountered within (note the ':' at the end of the option, manpage for more details) -s --symlinks follow symlinks -H --hardlinks normally, when two or more files point to the same disk area they are treated as non-duplicatesthis option will change this behavior -n --noempty exclude zero-length files from consideration -A --nohidden exclude hidden files from consideration -f --omitfirst omit the first file in each set of matches -1 --sameline list each set of matches on a single line -S --size show size of duplicate files -m --summarize summarize dupe information -q --quiet hide progress indicator -d --delete prompt user for files to preserve and delete all othersimportant: under particular circumstances, data may be lost when using this option together with -s or --symlinks, or when specifying a particular directory more than oncerefer to the fdupes documentation for additional information -N --noprompt together with --delete, preserve the first file in each set of duplicates and delete the rest without prompting the user -v --version display fdupes version -h --help display this help message 到此为止了。让我知道你以前怎么在Linux中查找并删除重复文件的吧?同时,也让我知道你关于这个工具的看法。在下面的部分中提供你有价值的反馈吧,别忘了为我们点赞并分享,帮助我们扩散哦。linux去重命令是什么呢?
在介绍uniq命令之前,我们先来新建在下面的案例中需要用到的文件/tmp/uniq.txt,内容如下
默认情况下uniq只会检索相邻的重复数据从而去重。在/tmp/uniq.txt中虽然“onmpw web site” 有三条,但是其中一条是和其他两条不相邻的,所以只去重了一条,同理“error php function”也是这种情况。
鉴于以上的检索机制,所以uniq一般情况下要和sort命令一块儿使用。
复制代码
# sort 1.txt | uniq
alpha css web
cat linux command
error php function
hello world
onmpw web site
recruise page site
repeat no data
wello web site
复制代码
现在再看是不是所有的重复项都已经经过去重处理了。
好了,小试牛刀一把以后,下面我们开始对uniq命令的选项进行简单的介绍。
-c 统计每一行数据的重复次数
复制代码
sort 1.txt | uniq -c
1 alpha css web
1 cat linux command
2 error php function
1 hello world
3 onmpw web site
1 recruise page site
1 repeat no data
1 wello web site
复制代码
我们看 “error php function”出现了两次,“onmpw web site”出现了三次。其余的都没有重复项所以为1。
-i 忽略大小写
在1.txt中添加一行数据 “Error PHP function”
复制代码
cat 1.txt
alpha css web
cat linux command
error php function
hello world
onmpw web site
onmpw web site
wello web site
Error PHP function
recruise page site
error php function
repeat no data
onmpw web site
复制代码
复制代码
sort 1.txt | uniq –c
1 alpha css web
1 cat linux command
2 error php function
1 Error PHP function
1 hello world
3 onmpw web site
1 recruise page site
1 repeat no data
1 wello web site
复制代码
我们看结果,uniq默认是区分大小写的。使用-i可以忽略掉大小写问题
复制代码
sort 1.txt | uniq –c –i
1 alpha css web
1 cat linux command
3 error php function
1 hello world
3 onmpw web site
1 recruise page site
1 repeat no data
1 wello web site
复制代码
现在再看是不是大小写已经忽略掉了。
-u 只输出没有重复的数据
复制代码
sort 1.txt | uniq –iu
alpha css web
cat linux command
hello world
recruise page site
repeat no data
wello web site
复制代码
看到没,结果中的“error php function”和“onmpw web site”都没有被输出。
-w N 表示从第一个字符开始只检索N个字符来判重。
复制代码
sort 1.txt | uniq –iw 2
alpha css web
cat linux command
error php function
hello world
onmpw web site
recruise page site
wello web site
复制代码
这里我们让uniq只对前两个字符进行检索,recruit 和 repeat前两个字符都是re,所以这两行也被认为是重复的。
-f N 表示略过前面N个字段,从第N+1个字段开始检索重复数据。以空格符或者tab键为分隔符。
复制代码
sort 1.txt | uniq –icf 2
1 alpha css web
1 cat linux command
3 error php function
1 hello world
4 onmpw web site
1 repeat no data
1 wello web site
复制代码
我们在结果中可以看到,这是略过前面的2个字段,从第三个字段开始判重的。“recruise page site” 和 “onmpw web site”的第三个字段相同,所以被认为是相同的数据。但是我们看到,“wello web site”和“onmpw web site”不但第三个字段相同,第二个也相同。那为什么它不被计入“onmpw web site”的重复数据中呢。对于这个问题就要回到前面说的,uniq只检测相邻的数据是否是重复的。
要解决这个问题还需要在sort命令上着手。还记得sort命令的-k选项吗,没错,我们就用它来解决。
复制代码
sort –k 2 1.txt | uniq –icf 2
1 alpha css web
1 cat linux command
1 repeat no data
1 recruise page site
3 error php function
4 onmpw web site
1 hello world
复制代码
我们看,是不是解决了。
-s N表示略过前面N个字符,关于这个选项的例子我们这里就不再举了,该选项和-f N的用法差不多。只不过-f N是略过前面N个字段;-s是略过前面N个字符。
-d 只输出有重复项的第一条的数据。
sort 1.txt | uniq -idw 2
repeat no data
error php function
onmpw web site
结果只有这三条。为什么会有“repeat no data”这条数据,这里注意-w 2的应用。
-D 对于重复项全部输出
复制代码
sort 1.txt | uniq –iDw 2
repeat no data
recruise page site
error php function
error php function
Error PHP function
onmpw web site
onmpw web site
onmpw web site
复制代码
好了,关于uniq的选项的所有常用的命令已经都介绍完了。关于uniq更详细的信息可以使用命令info uniq。
评论列表(0条)