BASH:检查文件是否在目录中重复? [英] BASH: Checking if files are duplicates within a directory?

查看:118
本文介绍了BASH:检查文件是否在目录中重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个管家脚本,并且文件位于我要清除的目录中. 我想将文件从源目录移动到另一个目录,因为有许多子目录,所以可能会有相同的文件.我想做的是使用CMP命令或MD5sum每个文件,如果它们不是重复项,则将其移动,如果它们相同则仅移动1.

I am writing a house-keeping script and have files within a directory that I want to clean up. I want to move files from a source directory to another, there are many sub-directories so there could be files that are the same. What I want to do, is either use CMP command or MD5sum each file, if they are no duplicates then move them, if they are the same only move 1.

因此,我的移动部件正常工作如下:

So the I have the move part working correctly as follows:

find /path/to/source -name "IMAGE_*.JPG" -exec mv '{}' /path/to/destination \;

我假设我将不得不遍历我的目录,所以我在想.

I am assuming that I will have to loop through my directory, so I am thinking.

用于/path/to/source中的文件 做 如果-name为"IMAGE _ *.JPG" 然后 md5sum(或cmp)$ files ...卡在这里(我担心这种方法将如何比较彼此之间的所有文件以及如何过滤掉它们)... 然后只要完成MV即可.

for files in /path/to/source do if -name "IMAGE_*.JPG" then md5sum (or cmp) $files ...stuck here (I am worried about how this method will be able to compare all the files against eachother and how I would filter them out)... then just do the mv to finish.

谢谢.

推荐答案

find . -type f -exec md5sum {} \; | sort | uniq -d

这将吐出所有具有重复的md5哈希值.那么只需弄清楚是哪个文件产生了这些重复的哈希即可.

That'll spit out all the md5 hashes that have duplicates. then it's just a matter of figuring out which file(s) produced those duplicate hashes.

这篇关于BASH:检查文件是否在目录中重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆