我有一个文件系统
/x/./
directoryx包含目录a, b, c, y. 我想找到所有内容/x/./,但不是在/x/y/./
我应该如何写?
我试过
find /x/./ -path "/x/y/" -prune -type f
但它不起作用。为什么?
find很字面意思。它不认为/x/y/是 below /x/./,因为它不是/x/./y/,即使这两个路径引用同一个目录。当没有给出明确的逻辑连接词时,您可能还会对它组合操作的方式有疑问,我永远记不起它的工作方式(始终使用显式连接词更容易)。
当.不是整个路径名本身时,将其省略总是安全的,在这种情况下,尾部斜杠也是不必要的。试试吧
find "/x" -path "/x/y" -prune -o -type f -print
在这种情况下,双引号在技术上也是不必要的,但如果路径名包含任何特殊字符,则它们是必需的。
编辑: 如果您知道要查找的文件向下两层,则告诉您find从向下两层开始搜索。有两种可能:你知道包含你想要的所有文件的子目录的名称——
# by definition nothing in /x/a/foo can be under /x/y
find "/x/a/foo" -type f -print
——或者你不——
# The stars in the first argument have to be outside the quotes,
# so the shell expands them. The stars in the -path argument have to
# be inside quotes so the shell *doesn't* expand them.
find "/x/"*/* -path "/x/y/*" -prune -o -type f -print
逻辑连接词很难解释。 -path whateverand表现-type f得像if条件,而-pruneand表现-print得像条件块内的东西,-o在这种情况下,表现得更else像or; 但这是过于简单化了,细节很重要。请阅读整个 GNU 查找手册。如果您在执行此操作后仍不确定某事,请在此处提出新问题。
find /x -type d -path /x/y -prune -o -type f -print
这将排除 y 目录。