使用 "find" 命令查找包含未知空格和下划线组合作为单词分隔符的文件?
Using "find" command to find files with unknown combination of spaces and underscores as word separators?
我正在编写一个脚本,它遍历数据库并将数据库中的项目与文件系统中的相应文件进行匹配。我遇到的问题是项目标题(来自数据库)中的 space 可能对应于 中的 space 或一个或多个下划线 对应的文件名。 (例如 "Some name" 可能是 "Some name.jpg"、"Some_name.jpg" 或 "Some__name.jpg"。大小写也可能不同,所以我需要搜索不区分大小写。
我一直在使用以下命令,它在上面的前两种情况下工作正常,但无法找到 space 由双下划线表示的文件:
find . -iname '*Some[ _]name*' -print
...我已经尝试了各种我期望的方法,比如 [ _]+
和 ([ _]+)
,但没有成功。如果有人能告诉我如何使用 find
指定 "some spaces or underscores",我将不胜感激。谢谢。
-iname
的参数是 pattern/glob 而不是正则表达式。
您想改用 -iregex
。
像这样:
find . -iregex '.*Some\( \|_+\)name.*' -print
或:
find . -regextype posix-awk -iregex '.*Some( |_+).*' -print
either a space or one or more underscores
find . -regextype sed -iregex '.*Some\( \|_\+\)name.*'
我正在编写一个脚本,它遍历数据库并将数据库中的项目与文件系统中的相应文件进行匹配。我遇到的问题是项目标题(来自数据库)中的 space 可能对应于 中的 space 或一个或多个下划线 对应的文件名。 (例如 "Some name" 可能是 "Some name.jpg"、"Some_name.jpg" 或 "Some__name.jpg"。大小写也可能不同,所以我需要搜索不区分大小写。
我一直在使用以下命令,它在上面的前两种情况下工作正常,但无法找到 space 由双下划线表示的文件:
find . -iname '*Some[ _]name*' -print
...我已经尝试了各种我期望的方法,比如 [ _]+
和 ([ _]+)
,但没有成功。如果有人能告诉我如何使用 find
指定 "some spaces or underscores",我将不胜感激。谢谢。
-iname
的参数是 pattern/glob 而不是正则表达式。
您想改用 -iregex
。
像这样:
find . -iregex '.*Some\( \|_+\)name.*' -print
或:
find . -regextype posix-awk -iregex '.*Some( |_+).*' -print
either a space or one or more underscores
find . -regextype sed -iregex '.*Some\( \|_\+\)name.*'