简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM