简体   繁体   English

Linux find 命令:搜索包含括号的文件名

[英]Linux find command: searching for a filename containing parentheses

I need to find files with filenames like this:我需要找到文件名如下的文件:

<some regex-matched text> (1).<some regex-matched text>

ie I want to search for filenames containing即我想搜索包含的文件名

  • text ending in a space以空格结尾的文本
  • then an opening bracket (parenthesis)然后是一个左括号(括号)
  • followed by the numeral 1后跟数字 1
  • followed by a closing bracket后跟一个右括号
  • possibly followed by a dot followed by some more text...可能后跟一个点,后跟一些更多的文字......

I first went find . -regex '.* \\(1\\)\\..*'我先find . -regex '.* \\(1\\)\\..*' find . -regex '.* \\(1\\)\\..*' . find . -regex '.* \\(1\\)\\..*' But the brackets are sort of ignored: files matching .* 1\\..* are returned.但是括号有点被忽略:匹配.* 1\\..*被返回。

In the course of my attempt to find an answer I found this page covering Linux find .在我试图找到答案的过程中,我发现了这个页面,涵盖了 Linux find Here I find this phrase:我在这里找到了这句话:

"Grouping is performed with backslashes followed by parentheses '\\(', '\\)'." “使用反斜杠后跟括号 '\\(', '\\)' 进行分组。”

[NB to show you the reader a single backslash, as shown in that page, I have doubled the backslashes to write the single backslashes above!] [注意向读者展示一个反斜杠,如该页面所示,我将反斜杠加倍以写出上面的单反斜杠!]

I wasn't sure what to make of that, ie how to escape ordinary brackets in that case.我不知道该怎么做,即在这种情况下如何逃避普通括号。 I thought maybe doubling up the backslashes in the find expression might work, but it didn't.我想也许在find表达式中加倍反斜杠可能会起作用,但它没有。

Even if I try to do it without using a regex, find seems to have some problems with brackets and/or a dot in this place:即使我尝试在不使用正则表达式的情况下执行此操作, find似乎在此位置的括号和/或点存在一些问题:

mike@M17A .../accounts $  find . -name *(1).pdf
[... finds stuff OK]
mike@M17A .../accounts $  find . -name *(1).*
find: paths must precede expression: ..
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec|time] [path...] [expression]
mike@M17A .../accounts $  find . -name *(1)\.*
find: paths must precede expression: ..
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec|time] [path...] [expression]

NB putting a space after in the initial * in these attempts also fails...注意在这些尝试中的初始 * 后面放一个空格也失败了......

That is because you don't need to escape these parenthesis.那是因为您不需要转义这些括号。 This should work :这应该工作:

find . -regex '.* (1)\(\..*\)?'

Though a capture group is used (escaped parenthesis) \\(\\..*\\) so that we can make the last match optional ( possibly followed by a dot followed by some more text ) with ?虽然捕获组使用(括号转义) \\(\\..*\\)这样我们就可以做出最后一场比赛中可选的(可能后面跟着一个点,接着一些文字)用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM