查找当前目录的重复文件

Linux|系统管理|WEB开发 2010-06-12 09:55:16 累计浏览 4,153 次

    Ubuntu下有一个fdupes程序可以用来打印当前目录下有多少文件是重复的。因为是二进制程序,所以效率上更高。

    而用Python实现的,效率就很低了,同样的目录,fdupes只用了2秒的样子,而Python程序就用了15s,大约7-8倍速度差距。

    所以这个代码仅仅只是一个演示罢了。代码参考《Python for Unix and Linux System Administration》一书。

#!/usr/bin/python -tt 
# -*- coding:utf-8 -*- 
__DOC__ = ’’’ 
    identifies duplicate files within given directories including subdirectories 
     pydupes.py uses files’ size and md5sums to find 
     duplicate files within a set of directories. 
    ’’’ 
__Author__ = ’wgzhao,wgzhao##gmail.com’ 

import os 
from hashlib import md5 
from sys import exit, argv 

class diskwalk(object): 
    """API for getting directory walking collections""" 
    def __init__(self, path): 
        self.path = path 
    def enumeratePaths(self): 
        """Returns the path to all the files in a directory as a list""" 
        path_collection = [] 
        for dirpath, dirnames, filenames in os.walk(self.path): 
            for file in filenames: 
                fullpath = os.path.join(dirpath, file) 
                path_collection.append(fullpath) 
        return path_collection 

    def enumerateFiles(self): 
        """Returns all the files in a directory as a list""" 
        file_collection = [] 
        for dirpath, dirnames, filenames in os.walk(self.path): 
            for file in filenames: 
                file_collection.append(file) 
        return file_collection 

    def enumerateDir(self): 
        """Returns all the directories in a directory as a list""" 
        dir_collection = [] 
        for dirpath, dirnames, filenames in os.walk(self.path): 
            for dir in dirnames: 
                dir_collection.append(dir) 
        return dir_collection 

def create_checksum(path): 
    """ 
    Reads in file. Creates checksum of file line by line. 
    Returns complete checksum total for file. 
    """ 
    fp = open(path) 
    checksum = md5() 
    while True: 
        buffer = fp.read(8192) 
        if not buffer: 
            break 
        checksum.update(buffer) 
    fp.close() 
    checksum = checksum.digest() 
    return checksum 

def findDupes(path = ’/tmp’): 
    dup = {} 
    record = {} 
    d = diskwalk(path) 
    files = d.enumeratePaths() 
    for file in files: 
        compound_key = (os.path.getsize(file),create_checksum(file)) 
        if compound_key in record: 
            if compound_key in dup: 
                dup[compound_key].append(record[compound_key]) 
            else: 
                dup[compound_key] = [ record[compound_key] ] 
            dup[compound_key].append(file) 
        else: 
            #print "Creating compound key record:", compound_key 
            record[compound_key] = file 
    return dup 

if __name__ == "__main__": 

    if len(argv) < 2: 
        try: 
            path = raw_input(’pls input dir:’) 
        except: 
            path = ’’ 
    else: 
        path = argv[1] 
    if not path: exit(1) 

    dupes = findDupes(path) 

    for k,v in dupes.items(): 
        print 
        for filename in v: 
            print filename

    代码运行类似如下:

$ pydupes Dropbox/
....
Dropbox/Public/rhce-ts-9.0-1.2.noarch.rpm
Dropbox/Public/Linux/rhce-ts-9.0-1.2.noarch.rpm

Dropbox/repos/bolebi/hooks/pre-revprop-change.tmpl
Dropbox/repos/lshc/hooks/pre-revprop-change.tmpl
....

Dropbox/Public/libflashplayer.so
Dropbox/Public/Linux/libflashplayer.so

同分类推荐文章

  1. NGINX Gateway Fabric 2.8: IP allow and deny lists, passive health checks, and cookie-based session persistence (2026-11-14 01:10:02)
  2. NGINX Ingress Controller 5.7: HTTP/2 to your upstreams, Opaque Secrets, and cert-manager challenges behind HTTPS redirects (2026-11-14 00:10:02)
  3. Hexo 文章一键发布到微信公众号 (2026-11-13 19:10:02)

查看更多 DevOps 文章 →

建议继续学习

  1. 用Hyer来进行网站的抓取 (累计阅读 158,836)
  2. 配置Nginx+uwsgi更方便地部署python应用 (累计阅读 109,256)
  3. 程序员技术练级攻略 (累计阅读 36,061)
  4. python实现自动登录discuz论坛 (累计阅读 33,280)
  5. python编程细节──遍历dict的两种方法比较 (累计阅读 21,007)
  6. 每个程序员都应该学习使用Python或Ruby (累计阅读 18,309)
  7. Chrome和goagent的配置方法,你懂的 (累计阅读 17,490)
  8. Linux如何统计进程的CPU利用率 (累计阅读 16,953)
  9. 我的 RHCA 之路 (累计阅读 14,367)
  10. 30分钟3300%性能提升――python+memcached网页优化小记 (累计阅读 14,112)