コマンドの-name
オプションはどのような正規表現構文をfind
期待していますか?と同じだろうという印象でした-regex
が、そうではないようです。
$ mkdir test && cd test
$ touch .sw.
$ touch .swp
$ touch .abc.swo
$ touch notswap.py
$ find . -name "*sw." -type f
./.sw.
$ find . -name "*sw*" -type f
./notswap.py
./.sw.
./.swp
./.abc.swo
$ find . -regex ".*sw." -type f
./.sw.
./.swp
./.abc.swo
FWIW、-regex
パス全体に一致し-name
、ファイル名のベースのみに一致することを認識しています。それがここでの問題ではないことは明らかだと思います。より具体的な質問として、オプションを使用して、.swx
whereで終わるすべてのファイルをx
任意の文字に一致させるにはどうすればよいですか-name
。
メタ情報:
$ find --version
find (GNU findutils) 4.5.11
...
$ echo $0
bash
-name pattern
Base of file name (the path with the leading directories
removed) matches shell pattern pattern. Because the leading
directories are removed, the file names considered for a match
with -name will never include a slash, so `-name a/b' will
never match anything (you probably need to use -path instead).
A warning is issued if you try to do this, unless the
environment variable POSIXLY_CORRECT is set. The
metacharacters (`*', `?', and `[]') match a `.' at the start
of the base name (this is a change in findutils-4.2.2; see
section STANDARDS CONFORMANCE below). To ignore a directory
and the files under it, use -prune; see an example in the
description of -path. Braces are not recognised as being
special, despite the fact that some shells including Bash
imbue braces with a special meaning in shell patterns. The
filename matching is performed with the use of the fnmatch(3)
library function. Don't forget to enclose the pattern in
quotes in order to protect it from expansion by the shell.
正規表現ではなくシェルパターンを使用します。
ソース: find(1)
名前の下のGNUマニュアルから:
名前が特定のパターンに一致するファイルを検索する方法は次のとおりです。これらのテストのパターン引数の説明については、シェルパターンマッチングを参照してください。
findおよびlocateは、ファイル名またはファイル名の一部をシェルパターンと比較できます。シェルパターンは、ワイルドカードまたはメタ文字と呼ばれる次の特殊文字を含む可能性のある文字列です。
シェルがメタ文字自体を展開しないようにするには、メタ文字を含むパターンを引用符で囲む必要があります。二重引用符と一重引用符はどちらも機能します。バックスラッシュでエスケープすることも同様です。
*
?
[string]
\
スラッシュ文字は、ワイルドカードが一致しないシェルとは異なり、検索と検索が行うシェルパターンマッチングでは特別な意味を持ちません。したがって、パターン「foobar」はファイル名「foo3 / bar」と一致でき、パターン「./srsc」はファイル名「./src/misc」と一致できます。
'locate'コマンドを使用して一部のファイルを検索したいが、完全なリストを表示する必要がない場合は、 '-limit'オプションを使用して、少数の結果のみを表示するか、 '-count'オプションを使用できます。一致の総数のみを表示します。
あなたの質問に答えるには:
find . -name "*.sw?" -type f
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加