簡體   English   中英

進入幾個目錄並執行某些操作,不包括目錄名稱中帶有特定字符串的幾個目錄

[英]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 ,但您必須使用它來構建一個要循環的列表,或者將您的循環體變成一個您可以傳遞給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