简体   繁体   English

删除两个文件夹之间的差异

[英]Remove differences between two folders

can anyone please suggest a method (ruby, python or dos preferable) to remove only the different files and sub-folders between two given folders? 有人可以建议一种方法(红宝石,python或dos优先)来删除两个给定文件夹之间的不同文件和子文件夹吗?

I need it to recurse through sub-directories and delete everything that is different. 我需要它来遍历子目录并删除所有不同的内容。

I don't wanna have to install anything, so a script would be great. 我不需要安装任何东西,因此脚本会很棒。

Thanks in advance 提前致谢

Wouldn't rsync be the better solution? rsync会不会是更好的解决方案? It supports everything you want and does it fast. 它支持您想要的一切,并且快速完成。

You can use Python's difflib to tell what files differ, then os.unlink them. 您可以使用Python的difflib告诉哪些文件不同,然后os.unlink它们。 Really, if all you need is to tell if the files differ at all, you can just compare their text with: 确实,如果您只需要告诉文件是否完全不同,则可以将它们的文本与以下内容进行比较:

for file1, file2 in files:
    f1 = open(file1, 'r').read()
    f1.close()
    f2 = open(file2, 'r').read()
    f2.close()
    if f1 != f2:
        os.unlink(file1)
        os.unlink(file2)

You can use os.walk to get lists of files. 您可以使用os.walk来获取文件列表。 The above code is written without new things like with , since you didn't want to install things. 上面的代码编写不喜欢新鲜事物with ,因为你不想安装的东西。 If you have a new Python installation, you can make it a bit nicer. 如果您安装了新的Python,则可以使其更好一点。

In Python, you can get the filenames using os.walk . 在Python中,您可以使用os.walk获取文件名。 Put each full pathname into a set and use the difference method to get the files and folders that are different. 将每个完整路径名放入set然后使用difference方法获取difference的文件和文件夹。

Ruby 红宝石

folder1=ARGV[0]
folder2=ARGV[1]
f1=Dir["#{folder1}/**"].inject([]){|r,f|r<<File.basename(f)}
Dir["#{folder2}/**"].each{|f2|File.unlink(f2) if not f1.include?(File.basename(f2))}

This is the kind of thing I have done when I wanted to diff directories: 这是我想比较目录时所做的事情:

#!/usr/bin/env python

import os, os.path
import stat

def traverse_path(start_dir='.'):
    for root, dirs, files in os.walk(start_dir, topdown=False):
        for f in files:
            complete_path = os.path.join(root, f)
            try:
                m = os.stat(complete_path)[stat.ST_MODE]
                if stat.S_ISREG(m):
                    yield complete_path[len(start_dir):]
            except OSError, err:
                print 'Skipping', complete_path
            except IOError, err:
                print 'Skipping', complete_path

if __name__ == '__main__':
    s = set(traverse_path('/home/hughdbrown'))
    t = set(traverse_path('/home.backup/hughdbrown'))
    for e in s - t:
        print e
    print '-' * 25
    for e in t - s:
        print e

Notice that there is a check for regular files. 请注意,这里会检查常规文件。 I seem to recall that I encountered files used as semaphores or which were written to by one process and read by another or something. 我似乎还记得我遇到过用作信号量的文件,或者是由一个进程写入并由另一个进程或某些事物读取的文件。 It turned out to be important. 事实证明这很重要。

You can add code to delete files, according to whatever rules you like. 您可以根据自己喜欢的任何规则添加代码以删除文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM