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

おすすめ記事