bash/zsh の .gitignore でファイルパターンを再利用するにはどうすればいいですか?

bash/zsh の .gitignore でファイルパターンを再利用するにはどうすればいいですか?

以下は.gitignore

cron.yaml
build
target
webview
*.pyc
*.sublime-workspace
.idea
*.rej
.coverage
app/tools/temp_*.py
app/tools/*/temp_*.py

現在、次のスクリプトを使用してローカル フォルダー内のファイルを反復処理しています。

find . -type f | grep -v -E "(.idea|.git)" | while read file
do
  # Do something with $file
done

$fileこの変数が のパターンと一致する場合、さらにフィルタリングしたいです.gitignore。これらのファイル パターンを理解できる既存のユーティリティまたは bash 組み込み機能はありますか?

答え1

grep-f別名 ( ) オプションをプロセス置換とともに使用して、一部のパターンを「正規表現化」できる場合があります--file。例:

find . -type f | grep -Ev '(\.idea|\.git)' | 
    grep -v -f <(sed 's/\([.|]\)/\\\1/g; s/\?/./g ; s/\*/.*/g' .gitignore) | 
    while IFS= read -r file ; do 
      # Do something with "$file"
    done

関連情報