Linux 如何使用-不在查找中?
假设我有一个这样的目录结构Linux 如何使用-不在查找中?,linux,bash,Linux,Bash,假设我有一个这样的目录结构 mkdir -p test/1 mkdir -p test/2 mkdir -p test/3 touch test/1/touch touch test/2/touch touch test/3/touch 如何在test/中查找除test/2中的文件以外的所有文件?使用-prune: find test -path 'test/2' -prune -or -print 使用-prune: find test -path 'test/2' -prune -or
mkdir -p test/1
mkdir -p test/2
mkdir -p test/3
touch test/1/touch
touch test/2/touch
touch test/3/touch
如何在
test/
中查找除test/2中的文件以外的所有文件?使用-prune
:
find test -path 'test/2' -prune -or -print
使用-prune
:
find test -path 'test/2' -prune -or -print
搜索时有一些用于目录排除。搜索时有一些用于目录排除。您可以通过以下方式使用-not
:
find -not -wholename './test/2*'
您可以通过以下方式使用-not
:
find -not -wholename './test/2*'
另一个shortfind variant可以完成此任务:
find test ! -path "test/2*"
输出
test
test/1
test/1/touch
test/3
test/3/touch
另一个shortfind variant可以完成此任务:
find test ! -path "test/2*"
输出
test
test/1
test/1/touch
test/3
test/3/touch
可能更适合Serverfault或Unix&Linux。可能更适合Serverfault或Unix&Linux。这对我很有用,但你能解释一下吗?我个人对-find
和为什么这样的东西(-或-print
)有时会起作用有困难…@DanFego:恐怕我写不出比-prune
手册中的-prune
示例更好的解释了。哦,我突然想到了。如果路径是'test/2'
,那么它是true&&true | | print
,在这种情况下print
不会发生?否则它是false&&true | | | print
,在这种情况下它是这样的。@DanFego:没错。这只是手册页中的解释:-)。此外,-prune
还有一个明显的副作用,就是不会递归到子路径中。所以-o
是针对目录本身的,-prune
是针对其子目录的。这对我很有用,但你能解释一下吗?我个人对-find
和为什么这样的东西(-或-print
)有时会起作用有困难…@DanFego:恐怕我写不出比-prune
手册中的-prune
示例更好的解释了。哦,我突然想到了。如果路径是'test/2'
,那么它是true&&true | | print
,在这种情况下print
不会发生?否则它是false&&true | | | print
,在这种情况下它是这样的。@DanFego:没错。这只是手册页中的解释:-)。此外,-prune
还有一个明显的副作用,就是不会递归到子路径中。因此,-o
用于目录本身,-prune
用于其子目录。@DanFego:Yes。如果有9个子目录以上,则应将其命名为01、02…:-)触碰!不过,有人会发现这一点,并开始询问如何重命名其所有目录:P@DanFego:是的。如果有9个子目录以上,则应将其命名为01、02…:-)触碰!不过,有人会发现这一点,并开始询问如何重命名其所有目录:P