2016-08-10 157 views
-3

如何在不知道位於何處的情況下在Linux中打開目錄?我想在腳本中打開它,但我不能使用cd myDirectory,因爲我不知道它在哪裏(並且它不在當前目錄中)。 非常感謝!如何在不知道位置的情況下打開目錄

+2

「打開目錄」是什麼意思?想在劇本中指出它嗎? –

+1

這可能是一個XY問題。你想做什麼?爲什麼你需要「打開」一個你不知道的目錄? –

+0

假設我們有一個目錄。我想打開它,但我不知道位於何處。我怎樣才能做到這一點?它存在100%。 –

回答

0
find /location/to/search -type d -name "directoryName" -print > /file/to/save/found/directory/paths.txt 

說明:

  • -type d:僅匹配目錄,
  • -name "directoryName":匹配的目錄,只有指定的名稱;接受*通配符,
  • -print:打印每個找到的路徑,輸出的
  • > /file/to/save/found/directory/paths.txt重定向到文件/file/to/save/found/directory/paths.txt

要在腳本中使用它,你可以通過匹配循環:

while read -r line; do 
    echo "$line contains $(ls -F $line | wc -l) files" 
done < <(find /location/to/search -type d -name "directoryName" -print) 
相關問題