我的目标是将一个目录中的所有文件(忽略文件中指定的文件除外(复制到另一个目录。然而,假设git没有安装(它使用Plastic SCM,但忽略格式与gitignore完全相同(。在bash你会怎么做?
这会从忽略文件中删除注释和空行:
grep -v '^#' ~/.config/git/ignore | grep '[^[:blank:]]'
用管道字符连接这些行,并在变量中捕获
ignores=$(grep -v '^#' ~/.config/git/ignore | grep '[^[:blank:]]' | paste -sd '|')
Enagle扩展的球形
shopt -s extglob nullglob
现在这将显示当前目录中未被忽略的文件
echo !($ignores)
查找未被忽略的文件,按目录排列,这可能会起作用(我不知道git的内部结构,这只是猜测(
# read the ignores into an array
readarray -t ign < <(grep -v '^#' ~/.config/git/ignore | grep '[^[:blank:]]')
# construct find options
find_opts=()
for i in "${ign[@]}"; do find_opts+=( -o -name "$i" ); done
# do it
find . -type f
-not '(' "${find_opts[@]:1}" ')'
-exec printf 'do something with: %sn' {} +