findの「regex」および「name」ディレクティブ

findの「regex」および「name」ディレクティブ

-nameコマンドオプションにはどの正規表現構文が必要ですかfind?私はそれがforのようだと思いましたが、-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

-regexFWIWは、フルパスと-nameファイル名の基本のみが一致することを知っています。これがここの問題ではないことが誰にとっても明らかになることを願っています。より具体的な質問として、.swxwhere can be any Characterでx終わるすべてのファイルを一致させるためにそのオプションをどのように使用できますか-name

メタ情報:

$ find --version
find (GNU findutils) 4.5.11
...
$ echo $0
bash

ベストアンサー1

-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マニュアルから名前:

名前が特定のパターンに一致するファイルを検索する方法は次のとおりです。これらのテストのパターンパラメータの説明については、シェルパターンの一致を参照してください。

2.1.4 シェルパターンマッチング

検索と検索は、ファイル名またはファイル名の一部をシェルパターンと比較できます。シェルパターンは、ワイルドカードまたはメタ文字と呼ばれる特殊文字を含めることができる文字列です。

シェルが自己拡張するのを防ぐには、メタ文字を含むパターンを引用する必要があります。二重引用符と一重引用符の両方が大丈夫です。バックスラッシュもエスケープされます。

  • *
    0個以上の文字と一致します。
  • ?
    任意の文字と一致します。
  • [string]
    文字列 string で正確に 1 文字と一致します。これを文字クラスといいます。簡単に言えば、文字列には、間にダッシュがある2文字の範囲を含めることができます。たとえば、「[a-z0-9_]」クラスは小文字、数字、または下線に一致します。開き括弧の直後に「!」または、「^」を配置してクラスを無効にすることもできます。したがって、「[^AZ@]」は大文字またはat記号を除くすべての文字と一致します。
  • \
    次の文字の特別な意味を削除します。これは文字クラス内でも機能します。
    シェルパターンマッチング(「-name」、「-wholename」など)を実行する検索テストでは、パターンのワイルドカード文字は「.」と一致します。ファイル名の先頭に。検索の場合も同様です。したがって、「find -name '*macs」は、「locate '*macs」と同様に、.emacsというファイルと一致します。

スラッシュ文字は、ワイルドカードの不一致シェルとは異なり、ルックアップと位置決めのためのシェルパターンマッチングでは特に意味がありません。したがって、パターン 'foobar は、ファイル名 'foo3/bar' とパターン './sr' と一致できます。sc' はファイル名 './src/misc' と一致できます。

「locate」コマンドを使用して特定のファイルを検索したいがリスト全体を表示する必要がない場合は、「--limit」オプションを使用して少数の結果のみを表示するか、「--count」オプションを選択すると合計一致数だけが表示されます。


あなたの質問に答えてください:

find . -name "*.sw?" -type f

おすすめ記事