如何查找名称包含字符串的文件夹

时间:2019-04-30 18:14:03

标签: find sh busybox

编辑: 找到了一个可能的解决方案,请参阅下面的内容,但我还需要一些帮助使其更快。

我想在一个包含文件夹的文件夹中进行挖掘,并复制名称包含一些字符串的每个文件夹,但是我很困惑。 现在,我附带的结构是:

#!/opt/bin/sh

EXCLUSIVECOUNTRIES="it,ch"
ONLYFROMCOUNTRIES=$(echo $EXCLUSIVECOUNTRIES | tr "," "\n")
FORCEPROTOCOL= # can be tcp udp or empty, that catch both

if [ $(echo "$FORCEPROTOCOL" | tr '[:upper:]' '[:lower:]') == "tcp" ]; then # tcp protocol
    PROTOCOL="tcp"
elif [ $(echo "$FORCEPROTOCOL" | tr '[:upper:]' '[:lower:]') == "udp" ]; then
    PROTOCOL="udp"
else
    PROTOCOL="\w"
fi

for dirz in $(find /opt/serverconfigs/ -type d | grep -Ei $PROTOCOL); do
    if [ ! -z $ONLYFROMCOUNTRIES ]; then # exclusive contry present, copy only what I need
        echo "cp "$dirz" to
    else # no exclusive contry, so copy all
        for country in $ONLYFROMCOUNTRIES; do # for each contry
            if [ $(echo $(basename "$dirz") | sed -En 's/^([a-z\-]+).*/\1/p') == "$country" ]; then
                echo "$dirz"
            fi
        done
    fi
done

但是当我运行它时,我得到了

sh: ch: unknown operand
cp /opt/serverconfigs/de0515udp to
sh: ch: unknown operand
cp /opt/serverconfigs/de0119udp to
sh: ch: unknown operand
cp /opt/serverconfigs/nl-uk0002tcp to
sh: ch: unknown operand
cp /opt/serverconfigs/us2062udp to
sh: ch: unknown operand
cp /opt/serverconfigs/us3415udp to
sh: ch: unknown operand

我的文件夹结构是:

/opt/serverconfigs/br0031tcp
/opt/serverconfigs/be0097udp
/opt/serverconfigs/uk1088tcp
/opt/serverconfigs/us3270udp
/opt/serverconfigs/ca0313tcp
/opt/serverconfigs/tw-hk0003tcp
/opt/serverconfigs/ro0036tcp
/opt/serverconfigs/uk1004udp
/opt/serverconfigs/us3242tcp
/opt/serverconfigs/uk1174tcp
/opt/serverconfigs/nl0294udp
/opt/serverconfigs/ro0034tcp
/opt/serverconfigs/dk0120tcp
/opt/serverconfigs/us3129tcp
/opt/serverconfigs/us2491tcp
/opt/serverconfigs/ca0075tcp
/opt/serverconfigs/uk1009udp
/opt/serverconfigs/us3188tcp
/opt/serverconfigs/it0081tcp
/opt/serverconfigs/ca0378udp
/opt/serverconfigs/au0216udp
/opt/serverconfigs/us2283tcp
/opt/serverconfigs/us3293tcp

第一个问题似乎是由内而外的,我不明白为什么,第二个问题是未知的操作数,也许是连接的? 我搜索了一些未知的操作数,并发现[]和$()有点sh问题,但是每次我尝试使其起作用时,结果都是一样的

我做错了什么?

我的期望是找到名称中包含EXCLUSIVECOUNTRIES var和协议组合的所有文件夹,并将该文件夹复制到另一个位置

有更好的主意吗?

1 个答案:

答案 0 :(得分:0)

通过shellcheck和一些调整,我找到了一个可行的解决方案

#!/opt/bin/sh

EXCLUSIVECOUNTRIES="it,ch"
ONLYFROMCOUNTRIES=$(echo $EXCLUSIVECOUNTRIES | tr "," "\n")
FORCEPROTOCOL="" # can be tcp udp or empty, that catch both

if [ "$(echo "$FORCEPROTOCOL" | awk '{print tolower($0)}')" = 'tcp' ]; then # tcp protocol
    PROTOCOL="tcp"
elif [ "$(echo "$FORCEPROTOCOL" | awk '{print tolower($0)}')" = 'udp' ]; then # udp protocol
    PROTOCOL="udp"
else  # both protocols
    PROTOCOL="\w"
fi

for dirz in $(find /opt/serverconfigs/ -type d | grep -Ei $PROTOCOL); do
    if [ -n "$ONLYFROMCOUNTRIES" ]; then # exclusive country present, copy only what I need
        for country in $ONLYFROMCOUNTRIES; do # for each country
            if [ "$(echo "$(basename "$dirz")" | sed -En 's/^([a-z\-]+).*/\1/p')" = "$country" ]; then
                echo "$dirz"
            fi
        done
    else # no exclusive country, so copy all
        echo "cp $dirz to"
    fi
done

问题是那很慢,有什么方法可以改善它? 我必须搜索超过9k个文件夹