简体   繁体   English

如何使 shell 脚本递归扫描目录并根据名称模式将某些文件添加到 Git?

[英]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?如何使 shell 脚本递归扫描克隆的 Git 目录并根据名称模式删除某些文件? If the file meet any of the below condition,如果文件满足以下任一条件,

  1. the file name length is more than 150 characters.文件名长度超过 150 个字符。 I don't mind the script include or exclude the extension.我不介意脚本包含或排除扩展名。 I don't need that much accuracy.我不需要那么高的准确性。 An approximate number ~150 is enough.大约 150 个就足够了。
  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>如果任何文件匹配,则应使用git rm </path/to/file/file name>

Thanks in advance.提前致谢。

Use find for the scanning:使用find进行扫描:

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) (当然,先在没有-exec位的情况下进行试运行以确保它按预期工作)

Note: This assumes GNU find , since you tagged the question linux .注意:这假定 GNU find ,因为您标记了问题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 .这告诉find跳过任何.git子目录,对于目录树中的所有其他常规文件,如果最后一个/分隔的组件(文件名 + 扩展名,如果有)有冒号或 151 个或更多字符,则将其忽略到git rm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作 bash 脚本,在其中我可以将某些文件移动到基于文件中的字符串命名的某些文件夹? - How can I make a bash script where I can move certain files to certain folders which are named based on a string in the files? 如何使用Shell脚本基于目录的数字名称值删除目录? - How can I delete directories based on their numeric name value with a shell script? 如何在没有find的情况下在linux shell脚本中根据日期查找和删除文件? - How can I find and delete files based on date in a linux shell script without find? shell脚本,用于根据目录中的现有文件创建文件 - shell script to create files based on existing files in a directory 如何在Linux上递归搜索多个模式? - How can I recursively search for multiple patterns on linux? 递归查找目录并在Shell脚本中重命名 - Recursively find a directory and rename it in Shell Script 如何在目录shell脚本上列出文件 - How to list files on directory shell script 如何使用shell脚本在目录中打印文本文件的哪些行,以及以数字或点开头的子目录? - How can I print mathing lines of text files in a directory and it's subdirectories which begin with a number or a dot, using shell script? Shell脚本修改具有同一目录中多个文件的文件名的变量 - Shell script to modify variable with file name of multiple files in same directory 如何创建可以扫描文件中特定单词的Shell脚本? - How to create a shell script that can scan a file for a specific word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM