如何创建仅包含两个源目录树的更改文件的目录树

如何创建仅包含两个源目录树的更改文件的目录树

我有两个包含相似文件的目录树(实际上是 macOS 卷),并且想要创建第三个目录树,仅包含在两个源树之间更改的文件。

我怎样才能做到这一点rsync?或者我应该使用其他工具来实现此目的?

答案1

  • 使用同步(基于这个答案):
    SOURCE_TREE_1="/some/where/tree_1"
    SOURCE_TREE_2="/some/where/tree_2"
    DIFF_DIR="/some/where/diff"
    
    rsync --dry-run \
          --recursive \
          --checksum \
          --itemize-changes \
          "$SOURCE_TREE_1/" "$SOURCE_TREE_2" | # trailing '/' in source directory is necessary!
      grep -E '^>fc' | # grep files with different checksums
      cut -d ' ' -f 2- |
    while read file; do
      subdir="${file%/*}";
      [ "$file" != "$subdir" ] && mkdir -p "$DIFF_DIR/$subdir";
    
      # Change `"$SOURCE_TREE_1/$file"` to `"$SOURCE_TREE_1/$file"` in the next
      # line if you want copy from source tree #2.
      cp -a "$SOURCE_TREE_1/$file" "$DIFF_DIR/$file";
    done
    
  • 使用git
    cd "$SOURCE_TREE_1"
    git init .
    git add .
    git commit -m 'Init' # Note: Here git may ask you to set name and email
    
    # replace all files with files from source tree #2
    rm -rf $(git ls-tree --name-only HEAD)
    rsync --archive "$SOURCE_TREE_2/" .
    
    # show changes briefly:
    git status -uno
    # show changes for some file:
    git diff "path/to/file"
    
    # restore source tree #1 state
    git restore .
    

相关内容