【问题标题】:how to copy diff of two directories to a new directory?如何将两个目录的差异复制到新目录?
【发布时间】:2011-07-12 14:07:37
【问题描述】:

假设我有两个目录:D1 和 D2。

D1 ( f1, f2, f3, f4 ) D2 ( f1, f2 )

我想将 D1 中的文件(不在 D2 中)复制到另一个目录 D3 :

D3 (f3, f4)

我如何在 linux 中做到这一点?

谢谢, 三位一体

【问题讨论】:

    标签: linux diff directory


    【解决方案1】:

    查看“dirdiff”包。它允许你想做什么。

    或者,这个 bash 命令行应该这样做:

    for i in `ls D1` ; do if [ -f D2/$i ]; then echo "skip $i" ; else cp D1/$i D3 ; fi  done
    

    注意ls D1 周围的反引号 - 不是单引号! (在美式键盘上,它位于 ~(波浪号)下方)

    【讨论】:

      【解决方案2】:

      从 D1 复制到 D3 时,使用 -exclude 指定 D2 它会完成......

      【讨论】:

        【解决方案3】:

        刚刚花了一天的大部分时间整理出类似的东西,并从一个较旧的question 的答案中挑选出来。我最终得到了一个相当精细的 bash 脚本:

        #!/bin/bash
        
        # setup folders for our different stages
        DIST=/var/www/localhost/htdocs/dist/
        DIST_OLD=/var/www/localhost/htdocs/dist_old/
        DIST_UPGRADE=/var/www/localhost/htdocs/dist_upgrade/
        
        cd $DIST
        
        find . -type f | while read filename
        do
        
            newfile=false
            modified=false
            if [ ! -e  "$DIST_OLD$filename" ]; then
                newfile=true
                echo "ADD $filename"
            elif ! cmp $filename $DIST_OLD$filename &>/dev/null; then
                modified=true
                echo "MOD $filename"
            fi
        
            if $newfile || $modified; then
        
                #massage the filepath to not include leading ./
                filepath=$DIST_UPGRADE$(echo $filename | cut -c3-)
        
                #create folder for it if it doesnt exist
                destfolder=$(echo $filepath | sed -e 's/\/[^\/]*$/\//')
                mkdir -p $destfolder
        
                #copy new/modified file to the upgrade folder
                cp $filename $filepath
            fi
        done
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-01-01
          • 2020-12-10
          • 1970-01-01
          • 2014-07-18
          • 2020-03-22
          • 1970-01-01
          • 2011-11-03
          相关资源
          最近更新 更多