[英]Go inside several directories and execute something, EXCLUDING few directories with certain string in the directory name
我有 100 多个名称中包含“this”、“that”或“nope”的目录。 例子:
./abc_3737_this_123 ./abc_9879_this_456 ./abc_2696_that_478 ./abc_8628_nope_958 ./abc_9152_nope_058
我想进入所有这些目录/子目录,不包括名称中包含“nope”的目录。 我只是希望代码中只留下带有“nope”的目录; 没有进入,没有检查子目录。
目前我将它用于所有目录:
对于 abc* 中的目录; 做一点事); 完毕
我想要类似的东西:
对于 dir in (abc this && abc that ); 做一点事); 完毕
我很抱歉这很愚蠢,我对脚本很陌生。 感谢您的时间。
好吧,你有很多选择。 你的壳是什么? 如果您使用的是 bash,则可以shopt -s extglob
然后执行以下任一操作:
for dir in abc_*_@(this|that)_*; do
具有包容性,或
for dir in abc_*_!(nope)_*; do
要独占。
如果您使用的是 zsh,您可以使用setopt kshglob
来完成上述工作,或者使用setopt extendedglob
来代替:
for dir in abc_*_(this|that)_*; do
for dir in abc_*_^nope_*; do
您也可以使用find
,但您必须使用它来构建一个要循环的列表,或者将您的循环体变成一个您可以传递给find
或xargs
执行的命令。
find . -maxdepth 1 \( \( -not -name \*nope\* \) -or -prune \) -print0 | xargs -r -0 do_something
while IFS= read -r dir; do
echo "$dir"
done < <(find ./*this* ./*that* -maxdepth 0 -type d)
# or < <(find . -maxdepth 1 -type d |grep -E '.*this.*|.*this.*')
# Example
$ while IFS= read -r dir; do echo " - $dir"; done < <(find /*m*e* /*oo* -maxdepth 0 -type d)
- /home
- /media
- /boot
- /root
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.