简体   繁体   English

进入几个目录并执行某些操作,不包括目录名称中带有特定字符串的几个目录

[英]Go inside several directories and execute something, EXCLUDING few directories with certain string in the directory name

I have 100s of directories with either “this”, “that” or “nope” in their names.我有 100 多个名称中包含“this”、“that”或“nope”的目录。 Example:例子:

./abc_3737_this_123 ./abc_9879_this_456 ./abc_2696_that_478 ./abc_8628_nope_958 ./abc_9152_nope_058 ./abc_3737_this_123 ./abc_9879_this_456 ./abc_2696_that_478 ./abc_8628_nope_958 ./abc_9152_nope_058

I want to get inside all of these dir/subdir EXCLUDING the dir containing “nope” in their names.我想进入所有这些目录/子目录,不包括名称中包含“nope”的目录。 I simply want the code to leave directories with “nope” in them alone;我只是希望代码中只留下带有“nope”的目录; no getting inside, no checking subdir.没有进入,没有检查子目录。

Currently I am using this for all dir:目前我将它用于所有目录:

for dir in abc* ;对于 abc* 中的目录; do (something);做一点事); done完毕

I want something like:我想要类似的东西:

for dir in (abc this && abc that );对于 dir in (abc this && abc that ); do (something);做一点事); done完毕

I am sorry of this very silly, I am very new to scripting.我很抱歉这很愚蠢,我对脚本很陌生。 Thank you for your time.感谢您的时间。

Well, you have a number of options.好吧,你有很多选择。 What's your shell?你的壳是什么? If you're using bash, you can shopt -s extglob and then do either:如果您使用的是 bash,则可以shopt -s extglob然后执行以下任一操作:

for dir in abc_*_@(this|that)_*; do

to be inclusive, or具有包容性,或

for dir in abc_*_!(nope)_*; do

to be exclusive.要独占。

If you're using zsh, you can setopt kshglob to make the above work, or use setopt extendedglob and these instead:如果您使用的是 zsh,您可以使用setopt kshglob来完成上述工作,或者使用setopt extendedglob来代替:

for dir in abc_*_(this|that)_*; do


for dir in abc_*_^nope_*; do

You could also use find , but you'd have to either use it to build a list to loop over, or else turn your loop body into a single command that you ccould pass to to find or xargs to execute.您也可以使用find ,但您必须使用它来构建一个要循环的列表,或者将您的循环体变成一个您可以传递给findxargs执行的命令。

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.

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