2014-09-28 149 views
0

我寫了一些函數,它應該在父目錄中找到一個目錄,但問題是它需要很長時間,可能它也在子目錄中搜索。 這裏是我的代碼:在Bash腳本中使用find命令並排除子目錄

function findMspDir() { 
    mountedDir=/opt/SwDrop/ 
    dirToSearch=/opt/SwDrop/Repository/ 
    if [ ! -d $mountedDir ]; then 
     echo "The directory hasn't been found" 
     exit 1; 
    else 
     echo "The directory is mounted" 
     subDirToSearch="MSP-$versionNum" 
    # mspDir=`find $dirToSearch -name $subDirToSearch` 
     mspDir=$(find /opt/SwDrop/Repository/ -name 'MSP-1.5.1.4') 
     if [ "$mspDir" = "" ]; then 
      echo "The MSP directory hasn't been found" 
      exit 1; 
     fi 
    fi 
    echo "The found directory is: $mspDir" 
} 

我知道肯定是我要找的目錄/opt/SwDrop/Repository/下,它不可能是在子目錄。 任何想法如何解決它?

回答

1

find -maxdepth 1 -name "you_name" -a type d

+0

'-a'代表什麼? – user3502786 2014-09-28 12:21:20

+0

邏輯「AND」是-a,邏輯「OR」是-o,它是一種將多個條件合併爲一個的方法。 – 2014-09-28 12:24:08

3

隨意將-maxdepth 1添加到您的查找命令中(請參閱GNU Findutils)。

+0

它的工作非常感謝! – user3502786 2014-09-28 11:57:15