Home > Enterprise >  Bash filter out files with a given extension
Bash filter out files with a given extension

Time:10-13

I've added a pre-commit hook to run Rubocop against any files that are being staged for commit. However, Rubocop errors out when you give it a .png or .svg file. I've added those file extensions to the exclude block in .rubocop.yml but because I'm actually explicitly supplying files by name that doesn't seem to do the trick.

Here's my pre-commit script:

#!/usr/bin/env bash

set -e

git diff --staged --diff-filter=d --name-only | xargs bundle exec rubocop

I think the approach is grabbing the list of files from that git command and looping through them, filtering out any that are .png or .svg, but honestly I don't know how to do that. Any suggestions on filtering the files by extension?

CodePudding user response:

One idea might be to filter out those extensions using grep.

git diff --staged --diff-filter=d --name-only \
  | grep --invert-match '\.\(png\|svg\)$' \
  | xargs bundle exec rubocop
  • Related