0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

検索条件に合うファイルの一覧をテキストファイルに出力

Last updated at Posted at 2020-10-21

検索条件に合うファイルをテキストファイルに出力

grepやfindで結果を出力し、それをteeでファイルに書き出す、というだけ。

検索条件に合うファイルを出力

参照【find・grep】特定の文字列を含むファイルのリストを取得する方法

ファイル名に特定文字列が含まれているファイルのリストを取得

・指定フォルダ直下のみ

$ ls [検索対象フォルダのパス] | grep "[検索したい文字列]"

・指定フォルダ配下を再帰検索

$ find [検索対象フォルダのパス] -type f -name "*[検索したい文字列]*"

ファイルを開いた中身に、特定文字列が含まれているファイルのリストを取得

・指定フォルダ配下を再帰検索

$ grep [検索したい文字列] -rl [検索対象フォルダのパス]

結果の出力フォーマットを指定する

参照【 find 】コマンド(応用編その2)――検索したファイルを指定したフォーマットで表示する

ファイルの名前だけを出力

(例として拡張子が.jsのものを出力する)

$ find [対象ディレクトリ] -type f -name "*.js" -printf "%f\n"

ファイルの名前とサイズを出力

$ find [対象ディレクトリ] -type f -name "*.js" -printf "%f %s\n"

\nは改行。これをつけないと、改行無しで羅列されて見にくい。

テキストを出力

teeコマンドでファイル出力

teeコマンドには様々なオプションがあるが、今回は単純にファイル名を引数に持たせるだけ。

$ tee [ファイル名]

組み合わせ

出力結果とteeコマンドを「|」(パイプ)で連結する。

$ find [対象ディレクトリ] -type f -name "*.js" -printf "%f %s\n"|tee [ファイル名]

これで[対象ディレクトリ]内から再帰的に拡張子「.js」のファイルを検索し、ファイル名とサイズを[ファイル名]に書き出すことができる。

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?