各検索結果に対して特定のコマンドをどのように実行できますか?

各検索結果に対して特定のコマンドをどのように実行できますか?

このコマンドを使用して見つかったfindすべてのファイルに対して特定のコマンドをどのように実行できますか?質問の目的に基づいてで見つかったすべてのファイルを削除したいとしますfind

ベストアンサー1

編集する:以下の回答は一般的な使い方を説明していますが、ファイルとディレクトリを削除することは特別なケースであることに注意してください。その設定を使用する代わりに、次のように1を-execdir rm {} ';'使用します-delete

find -iname '*.txt' -delete

これは、エラーを回避し、いくつかのセキュリティ問題を解決するために削除する必要があるファイルやディレクトリの順序など、考えられなかったいくつかの極端なケースに対処することができます。他のユースケースの場合...

結果を見つけるためのコマンドの実行を処理する最善の方法は、通常、コマンドにさまざまな-exec/-ok述部を使用することですfind。特に1は、見つかったファイルのディレクトリ内で動作し、通常は他の述語よりも安全であるため、可能な限り1を使用する必要があります-execdir(愚かな間違いが災害を防ぐという意味で)。

述語-execの後に実行するコマンドが続き、{}見つかったファイルを含める必要がある場所を示し、各ファイルに対してコマンドを1回実行するか、一致するすべての引数のリストを置き換えて終了しますfind。セミコロン終端は引用符で囲まれているため、シェルはそれを新しいコマンドを指す区切り文字として認識しません。の派生や以前のバージョンなどの一部のシェルでは、引用が必要になる場合があります。シェルに応じて、またはなどの代替引用符形式を使用できます。 Bourneやcshなどのシェルには慣用的です。';'+{}rcfish{}";"\;$';'\;

すべてのテキストファイルを探しているとします。

find -iname '*.txt' -execdir rm {} ';'

以下はGNUマニュアルの関連セクションですfindman findGNUシステムの場合):

   -exec command ;
          Execute  command;  true  if 0 status is returned.  All following
          arguments to find are taken to be arguments to the command until
          an  argument  consisting of ‘;’ is encountered.  The string ‘{}’
          is replaced by the current file name being processed  everywhere
          it occurs in the arguments to the command, not just in arguments
          where it is alone, as in some versions of find.  Both  of  these
          constructions might need to be escaped (with a ‘\’) or quoted to
          protect them from expansion by the shell.  See the EXAMPLES sec-
          tion for examples of the use of the -exec option.  The specified
          command is run once for each matched file.  The command is  exe-
          cuted  in  the starting directory.   There are unavoidable secu-
          rity problems surrounding use of the -exec  action;  you  should
          use the -execdir option instead.


   -exec command {} +
          This  variant  of the -exec action runs the specified command on
          the selected files, but the command line is built  by  appending
          each  selected file name at the end; the total number of invoca-
          tions of the command will  be  much  less  than  the  number  of
          matched  files.   The command line is built in much the same way
          that xargs builds its command lines.  Only one instance of  ‘{}’
          is  allowed  within the command.  The command is executed in the
          starting directory.


   -execdir command ;

   -execdir command {} +
          Like -exec, but the specified command is run from the  subdirec-
          tory  containing  the  matched  file,  which is not normally the
          directory in which you started find.  This a  much  more  secure
          method  for invoking commands, as it avoids race conditions dur-
          ing resolution of the paths to the matched files.  As  with  the
          -exec action, the ‘+’ form of -execdir will build a command line
          to process more than one matched file, but any given  invocation
          of command will only list files that exist in the same subdirec-
          tory.  If you use this option, you must ensure that  your  $PATH
          environment  variable  does  not  reference  ‘.’;  otherwise, an
          attacker can run any commands they like by leaving an  appropri-
          ately-named  file in a directory in which you will run -execdir.
          The same applies to having entries in $PATH which are  empty  or
          which are not absolute directory names.

-inameまたは同様に、-execdirこれは標準オプションではありませんが、多くのfind実装でこれをサポートしています。サポートされている場合を意味します-depthGNUのマニュアルでは、次の使用時に明示的に追加することをfindお勧めします。-depth-deleteこの事実を忘れないように。

おすすめ記事