我有两个目录new
和old
,目录结构几乎相似,如下所示,但有一些差异。
old
|----1
| |-1a.cpp
| |-1b.cpp
|----2
| |-2c.cpp
|----3
| |-3a.cpp
| |-3b.cpp
|----4
| |-4a.cpp
| |-4b.cpp
|----5
| |-5a.cpp
| |-5b.cpp
------------
new
|----1
| |-1a.cpp
| |-1b.cpp
|----4
| |-4a.cpp
|----5
| |-5a.cpp
| |-5b.cpp
目录new
包含已修改的文件。但它维护old
的目录结构。
如何编写shell脚本以使用diff
和old
目录的new
实用程序生成修补程序。 patch
应仅包含new
目录中仅有文件的差异。它不应包含old
目录中的其他文件。
答案 0 :(得分:1)
mkpatch() {
new=$1
old=${1/new/old}
patch=${1}.patch
diff "$old" "$new" > "$patch"
}
export -f mkpatch
# cd to parent directory of old and new
find new -type f -exec bash -c 'mkpatch/{}' \;
答案 1 :(得分:0)
以下应该按照我的想法做到。
#!/bin/sh
OLD_DIR=old
NEW_DIR=new
# Make list of files, stripping off first directory in path
find "$OLD_DIR" -type f -print | cut -d/ -f2- | sort > files-old
find "$NEW_DIR" -type f -print | cut -d/ -f2- | sort > files-new
# Get files that are common
comm -1 -2 files-old files-new > files-common
# Create list of files to diff
sed "s/^/$OLD_DIR\//" files-common > files-to-diff-old
sed "s/^/$NEW_DIR\//" files-common > files-to-diff-new
# Do the diff
paste -d ' ' files-to-diff-old files-to-diff-new | xargs -n 2 diff -u
# Cleanup. Temp file handling should probably be done better by using
# mktemp to generate file names and using trap to make sure the files
# always are deleted etc.
rm -f files-common files-new files-old files-to-diff-new files-to-diff-old
这只是一个简单,直接的方法,使用几个临时文件。如果你愿意的话,你可能会逃避其中的一些。