如何使用 find 命令查找文件名的目录并删除重复项?

How to use find command to find the directory of a filename and remove duplicates?

我正在使用 find / -name "*.dbf" 查找所有 .dbf 文件的目录。 它给了我目录和文件名。

输出应该只有没有重复的目录。我不需要查看文件名。

您可以通过 dirname 传递结果,然后像这样删除重复项:

find / -name \*.dbf -print0 | xargs -0 -n1 dirname | sort | uniq

另一个解决方案:find / -name "*.dbf" -exec dirname {} \; 2> /dev/null | sort -u

我可以从两个方面理解你的问题:

  1. 要仅查找与 <name_pattern> 匹配且没有重复的目录,您可以使用 find-type 选项通过管道传输到 sort | uniq :

    find / -name '<name_pattern>' -type d | sort | uniq

  2. 要查找所有文件,但只查找 return 包含匹配文件且没有重复的目录:

    find / -name '<name_pattern>' | perl -pe 's/(.*\/).*$//' | sort | uniq