查找具有不同名称的相同文件

dmx*_*dmx 8 command-line files text-processing

是否有可能在不知道其名称的情况下找到它?

我用 LaTex 创建了一个文件,然后将其复制到另一个本地目录中并重命名了 pdf。我不再知道原始文件的位置,但我手头有重命名的文件。我想对我的乳胶文件进行一些修改并重新创建 pdf。

由于我知道原始文件除了名称外与重命名的文件完全相同,有什么办法可以找到我的原始文件吗?

pa4*_*080 5

唯一的区别是名称booth 文件应该具有相同的内容和大小。

1. 关于内容。我们可以通过命令比较两个文件diff file-1 file-2。我们也可以以这种方式使用此命令进行测试:

diff -q file-1 file-2 > /dev/null && echo 'equal' || echo 'different'
Run Code Online (Sandbox Code Playgroud)

2.关于尺寸。我们可以通过命令找到一定大小的文件(其中 12672 是文件大小,以字节为单位):

find /path/to/search -type f -size 12672c -printf '%p\n'
Run Code Online (Sandbox Code Playgroud)

或者我们可以这样使用一个范围(其中 12600-12700 是文件大小范围,以字节为单位):

find /path/to/search -type f -size -12700c -size +12600c -printf '%p\n'
Run Code Online (Sandbox Code Playgroud)

请注意,默认情况下,该命令以find递归方式工作。

3.结合两种方法file-1我们的模式文件在哪里):

find /path/to/search -type f -size -12700c -size +12600c -printf '%p\t' -exec sh -c 'diff -q file-1 "$1" > /dev/null && echo "equal" || echo "different"' sh {} \;
Run Code Online (Sandbox Code Playgroud)

4. 例子。假设我们有以下目录结构:

$ tree /tmp/test
/tmp/test
??? file-1   # this is the pattern file
??? file-2   # this is almost the same file but wit few additional characters
??? file-3   # this is exact copy of file-1
Run Code Online (Sandbox Code Playgroud)

上述命令的结果将是:

$ find /tmp/test -type f -size -12700c -size +12600c -printf '%p\t' -exec sh -c 'diff -q file-1 "$1" > /dev/null && echo "equal" || echo "different"' sh {} \; 
/tmp/test/file-2        different  # OK: here we have added few additional characters
/tmp/test/file-3        equal      # OK: this is exact copy of file-1
/tmp/test/file-1        equal      # OK: this is file-1 compared to its self
Run Code Online (Sandbox Code Playgroud)

或者我们可以通过以这种方式更改我们的命令来简化输出:

$ find /tmp/test -type f -not -name "file-1" -size -12700c -size +12600c \
  -exec sh -c 'diff -q file-1 "$1" > /dev/null && printf "%s\tis equal\n" "$1"' sh {} \;
/tmp/test/file-3        is equal
Run Code Online (Sandbox Code Playgroud)

从评论更新。以下命令查找与 , 大小相同的文件file-1,然后diff逗号与--brief--report-identical-files选项有关:

find /path -type f -not -name "file-1" -size $(stat -c%s file-1)c -exec diff -qs file-1 {} \;
Files file-1 and /tmp/test/file-3 are identical
Run Code Online (Sandbox Code Playgroud)

我们可以通过这种方式比较文件的md5sum

  • +1首先使用大小。顺便说一句,可以简化 diff 命令行:`diff -qs file-1 file-2` 将完成这项工作:-) (2认同)
  • @sudodus `-qs` 可能会加速 `diff`,因为它可以在发现第一个不匹配时停止比较文件。 (2认同)

Sim*_*hin 3

  • 您可以使用以下命令搜索特定字符串grep -rl "string" (-r 表示递归,在文件中查找字符串,-l 表示显示文件名,而不是字符串)


Zan*_*nna 3

这可能需要一段时间,但应该有效且可靠。它假设您正在使用 Bash。替换file为您重命名的文件的名称:

shopt -s globstar
for i in **; do [ -f "$i" ] && cmp --silent file "$i" && echo "$i"; done
Run Code Online (Sandbox Code Playgroud)
  • shopt -s globstar打开递归通配符**. 您可以使用 关闭它shopt -u globstar,但默认情况下它是关闭的,并且当您打开新 shell 时它也会关闭。
  • for i in **循环遍历该文件下面的所有文件。从可能包含该文件或该文件或该目录的目录的最高级别目录运行该命令...(对这句话应用递归!)
  • [ -f "$i" ] &&如果该文件是存在的常规文件那么...
  • cmp --silent file "$i" &&file如果和 正在检查的文件没有区别(即如果cmp成功退出),那么...
  • echo $i打印文件的相对路径(这也打印其自身的路径file,但我没有看到解决这个问题有多大好处)。

感谢Stack Overflow 上的这个答案,提供了cmp比较文件的方法。