TARでフォルダを繰り返し、ファイル数を数えます。

TARでフォルダを繰り返し、ファイル数を数えます。

フォルダを循環し、TARで同じ名前のファイル数を数える必要があります。

私はこれを試しました:

find -name example.tar -exec tar -tf {} + | wc -l

しかし、失敗します。

tar: ./rajce/rajce/example.tar: Not found in archive
tar: Exiting with failure status due to previous errors
0

example.tarが1つしかない場合に機能します。

各ファイルに個別に番号を付ける必要があります。

ありがとうございます!

ベストアンサー1

tar -tf {} \;代わりに、各タールボールを個別にtar -tf {} +実行する必要がありますtar。 GNUでは、次のようにman find言います。

   -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 invocations 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 com- mand.  The command is executed in the
          starting directory.

あなたのコマンドは同じですtar tf example.tar example.tar。また、引数がありません。たとえば、BSD find[path...]の一部の実装はエラーをfind返します。find: illegal option -- n要約すると、次のようになります。

find . -name example.tar -exec tar -tf {} \; | wc -l

この場合、wc -lファイル数は見つかったすべてのファイルの中で計算されます example.tar。現在ディレクトリにあるファイルのみを-maxdepth 1検索できます。すべてを再帰的に検索し、各結果を個別に印刷したいexample.tar場合(これはexample.tar$コマンドラインプロンプト コマンドの一部ではなく、新しい行の始まりを示すために使用されます):

$ find . -name example.tar -exec sh -c 'tar -tf "$1" | wc -l' sh {} \;
3
3

そして、ディレクトリ名の前に以下を追加します。

$ find . -name example.tar -exec sh -c 'printf "%s: " "$1" && tar -tf "$1" | wc -l' sh {} \;
./example.tar: 3
./other/example.tar: 3

おすすめ記事