Toxic Brain
Toxic Brain

Reputation: 83

How can I make shell script to recursively scan a directory and add certain files based on their name patterns to Git?

How can I make shell script to recursively scan a cloned Git directory and remove certain files based on their name patterns? If the file meet any of the below condition,

  1. the file name length is more than 150 characters. I don't mind the script include or exclude the extension. I don't need that much accuracy. An approximate number ~150 is enough.
  2. The file contains a : (colon) in its file name.

If any of the file matches, it should be executed with git rm </path/to/file/file name>

Thanks in advance.

Upvotes: 0

Views: 221

Answers (1)

Shawn
Shawn

Reputation: 52579

Use find for the scanning:

find . -regextype egrep \( -name .git -prune \) -o \
       \( -type f -regex '.*(:[^/]*|[^/]{151,})$' -exec git rm '{}' \+ \)

(Do a dry run without the -exec bit first to make sure it works as desired, of course)

Note: This assumes GNU find, since you tagged the question linux.

This tells find to skip any .git subdirectories, and for all other regular files in the directory tree, if the last /-separated component (The filename + extension if any) has a colon or 151 or more characters in it, pass it off to git rm.

Upvotes: 4

Related Questions