![实施变更捕获](https://linux22.com/image/143280/%E5%AE%9E%E6%96%BD%E5%8F%98%E6%9B%B4%E6%8D%95%E8%8E%B7.png)
我有两个目录,即 "before" 和 "after" ,每个目录都有它的子目录和文件。我想从深度 1 级别显示这两个文件夹之间的所有增量更改。
What is I mean is to compare the outputs of the following
commands and display the differences.
1. find before/ -mindepth 1
2. find after/ -mindepth 1
After cimparison I want to display the following:
a."A" before files/folders present ONLY in the after/
hierarchy(these will be deemed as newly added
components)
b. "D" before files/folders present ONLY in the before/
hierarchy(these will be deemed as deleted
components)
c. "M" before files/folders present in BOTH before/ and
after/ hierarchies(these will be deemed as modified
components)
答案1
也许比diff
-ing 更好的输出是这样的:
#!/bin/sh -eux
find before -mindepth 1 -printf "%p D\n" | cut -d/ -f2- | sort > files-before
find after -mindepth 1 -printf "%p A\n" | cut -d/ -f2- | sort > files-after
join -a2 -a1 files-before files-after | sed 's/D A$/M'
在哪里:
- 我们搜索所有
before
文件after
, - 省略
before
和after
目录本身 (-mindepth 1
), - 附加
D
到在 下找到的文件before
和A
下的文件after
, - 从所有找到的文件中删除路径的第一个组成部分(
cut
), - 对结果进行排序并存储在两个单独的文件中。
最后一条命令:
- 将谈论同一文件的行配对(请参阅
man join
),以便每个文件(相对于搜索目录,因为我们删除了它)仅出现一次,如果D
文件位于before
或A
如果它位于after
或D A
如果它同时位于两者中), - 我们包含仅出现在输入文件之一(
-a1 -a2
)中的文件名, - 最后,如果文件同时具有
D
和A
标志,我们将其更改为M
按要求。