algorithm - 关联文件内容以查找移动是否有效?

标签 algorithm

我对检测文件更改(如文件夹同步程序)感到好奇。

如果文件的内容已更改但其路径和名称相同,则检测起来很简单。

如果内容相同,但文件已被移动,仍然可以通过类似逐个比较的方式检测到这一点。

如果文件的内容发生了轻微的变化并且它已经移动了怎么办?有没有可靠的方法来检测而不是将其视为单独的文件删除和创建事件?

编辑:在看到 Tobias 的回答后,我意识到我应该澄清一下,我说的是同步大量数据(100 GB,10,000 文件,许多它们是二进制文件)在合理的时间内(在普通个人计算机上为秒)。

最佳答案

您可以尝试将文件的内容与其 Levenshtein distance 进行比较.因此,您可以检测文件是否已被轻微更改和移动(如果您逐个比较)。

伪代码算法(来自维基百科)可能如下所示:

// len_s and len_t are the number of characters in string s and t respectively
int LevenshteinDistance(const char *s, int len_s, const char *t, int len_t)
{ 
  int cost;

  /* base case: empty strings */
  if (len_s == 0) return len_t;
  if (len_t == 0) return len_s;

  /* test if last characters of the strings match */
  if (s[len_s-1] == t[len_t-1])
      cost = 0;
  else
      cost = 1;

  /* return minimum of delete char from s, delete char from t, and delete char from both */
  return minimum(LevenshteinDistance(s, len_s - 1, t, len_t    ) + 1,
                 LevenshteinDistance(s, len_s    , t, len_t - 1) + 1,
                 LevenshteinDistance(s, len_s - 1, t, len_t - 1) + cost);
}

如果您只比较已删除的文件,则可以更快地完成,因此您不需要将新文件与文件系统中的每个文件进行比较。

关于algorithm - 关联文件内容以查找移动是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57229121/

相关文章:

python-3.x - 我如何改进此解决方案以使其使用 numpy 更快?

javascript - 洗牌算法公平吗? (JavaScript)

algorithm - 连接树中的节点

python - 文本生成算法

swift - 如何在 Swift 中获取数字的二进制倒数?

python - WIKIPEDIA 在 python 中所说的快速排序算法

c++ - Union-find 方法性能,迭代与递归

algorithm - 最小化数组中相关项之间的距离

sql - 加入两个窄格式表

algorithm - 为什么不总是使用堆排序