使用-type-regex和-prune查找
我有一组目录(在当前目录中),我正在将文件排序到其中。源文件要么在当前目录中,要么在它也包含的其他目录中(通常是许多级别) 我将使用find获取要处理的文件列表,使用-type-regex和-prune排除目标目录中的文件,并使用另一个-regex选择文件 至少,这是我的意图,文件列表是正确的——只有一个例外:目标目录出现在列表中(但不是它们已经包含的文件——这是所需的行为) 我有一个变通方法:在随后的循环中,我将丢弃任何不是文件的内容 我确信在目录排除正则表达式中有一个简单的错误,或者我没有理解-prune应该做什么 以下是我的代码(我使用的是Mac,因此使用了-E选项):使用-type-regex和-prune查找,regex,bash,Regex,Bash,我有一组目录(在当前目录中),我正在将文件排序到其中。源文件要么在当前目录中,要么在它也包含的其他目录中(通常是许多级别) 我将使用find获取要处理的文件列表,使用-type-regex和-prune排除目标目录中的文件,并使用另一个-regex选择文件 至少,这是我的意图,文件列表是正确的——只有一个例外:目标目录出现在列表中(但不是它们已经包含的文件——这是所需的行为) 我有一个变通方法:在随后的循环中,我将丢弃任何不是文件的内容 我确信在目录排除正则表达式中有一个简单的错误,或者我没有理
。。。还有最后一个问题:如何使文件选择正则表达式不区分大小写?以下是
查找手册页的相关部分:
-print This primary always evaluates to true. It prints the pathname of the current file to standard output. If
none of -exec, -ls, -print, -print0, or -ok is specified, the given expression shall be effectively replaced
by ( given expression ) -print.
find -E . \
-type d -regex './(?i:DVD|quarantine|720|high|low|error)' -prune -o \
-type f -regex '.*\.(?i:avi|wmv|mp4|m4v|mov|mkv)' -print
因此,由于您的命令行没有指定-exec
、-ls
、-print
、-print0
、或-ok
,因此与您的命令相同:
find -E . \
\( -type d -regex './(DVD|quarantine|720|high|low|error)' -prune -o \
-type f -regex '.*.(avi|wmv|mp4|m4v|mov|mkv)' \) -print
解决方案是在-o
的右侧显式-print
(或-print0
):
find -E . \
-type d -regex './(DVD|quarantine|720|high|low|error)' -prune -o \
-type f -regex '.*\.(avi|wmv|mp4|m4v|mov|mkv)' -print
此外,如注释中所述,可以使用-iregex
使正则表达式不区分大小写
或者,如果您愿意,也可以在表达式本身中嵌入大小写不敏感(请参见re_格式
手册页):
编辑:否,-iregex是实现不区分大小写的唯一方法。以下是查找手册页的相关部分:
-print This primary always evaluates to true. It prints the pathname of the current file to standard output. If
none of -exec, -ls, -print, -print0, or -ok is specified, the given expression shall be effectively replaced
by ( given expression ) -print.
find -E . \
-type d -regex './(?i:DVD|quarantine|720|high|low|error)' -prune -o \
-type f -regex '.*\.(?i:avi|wmv|mp4|m4v|mov|mkv)' -print
因此,由于您的命令行没有指定-exec
、-ls
、-print
、-print0
、或-ok
,因此与您的命令相同:
find -E . \
\( -type d -regex './(DVD|quarantine|720|high|low|error)' -prune -o \
-type f -regex '.*.(avi|wmv|mp4|m4v|mov|mkv)' \) -print
解决方案是在-o
的右侧显式-print
(或-print0
):
find -E . \
-type d -regex './(DVD|quarantine|720|high|low|error)' -prune -o \
-type f -regex '.*\.(avi|wmv|mp4|m4v|mov|mkv)' -print
此外,如注释中所述,可以使用-iregex
使正则表达式不区分大小写
或者,如果您愿意,也可以在表达式本身中嵌入大小写不敏感(请参见re_格式
手册页):
编辑:不,-iregex
是实现不区分大小写的唯一方法。GNU find有-iregex
选项,类似于-regex
,但不区分大小写。GNU find有-iregex
选项,类似于-regex
,但不区分大小写。谢谢。这很好地解释了我的误解。我遇到了一个问题。我只是在文件选择部分使用了嵌入的大小写不敏感,我得到了这个错误:find:-regex:.*\(?I:avi | wmv | mp4 | m4v | mov | mkv):重复操作符操作数invalidUsing-iregex修复了它。也许这是另一个与mac相关的东西?@Lorccan啊,我误读了re_格式
手册页。(?i:..)
构造仅用于“增强的扩展”正则表达式,而-E
仅启用“扩展”正则表达式。谢谢。很高兴知道原因,谢谢。这很好地解释了我的误解。我遇到了一个问题。我只是在文件选择部分使用了嵌入的大小写不敏感,我得到了这个错误:find:-regex:.*\(?I:avi | wmv | mp4 | m4v | mov | mkv):重复操作符操作数invalidUsing-iregex修复了它。也许这是另一个与mac相关的东西?@Lorccan啊,我误读了re_格式
手册页。(?i:..)
构造仅用于“增强的扩展”正则表达式,而-E
仅启用“扩展”正则表达式。谢谢。很高兴知道原因。