开发者

Comparing two folders based on last modified date

I have two folders A an开发者_如何学编程d B, which contain identical folder structure and files, but Folder B contains modified files. Both folders contain subfolders too. I want to check which files are modified in folder B and copy it to a different folder C. How can I achieve this using a cmd/shell script?


AFAIK rsync and unison can't handle your needs, since you want the changes to go to a third folder C.

This code is untested:

#python
import os
import shutil
a_dir=...
b_dir=...
c_dir=...
len_a_dir=len(a_dir)
for root, dirs, files in os.walk(a_dir):
    dirs.sort()
    for file in sorted(files):
        a_file=os.path.join(root, file)
        b_file='%s%s' % (b_dir, file[len_a_dir:])
        if os.path.getmtime(a_file)!=os.path.getmtime(b_file):
            c_file='%s%s' % (c_dir, file[len_a_dir:])
            shutil.copyfile(b_file, c_file)


Try this:

rsync -r --compare-dest=/path/to/A /path/to/B/ /path/to/C
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜