如何在Linux中尋找目錄結構

如何在Linux中尋找目錄結構

我正在尋找一個 Linux 命令來RHEL v6.x搜尋當前目錄中的特定目錄結構。

雖然我知道下面的命令將搜尋當前目錄中的特定單一目錄,該目錄適用於單一目錄,但不適用於目錄結構。

在職的:

find /home/dir1/* -name "def"


不工作:

find /home/dir1/* -name "abc/def"

我也嘗試了下面的命令,但它也列出了該目錄中的文件,但我不想列出該目錄中的文件。我只想列出具有此abc/def目錄結構的所有目錄的完整路徑。

locate abc/def/

誰能幫幫我嗎。

提前致謝。

答案1

測試-name僅符合最後一個路徑組件。要匹配像abc/def你這樣的東西將需要-path

$ mkdir -p somedir/otherdir/abc/def/ghi
$ find somedir -path '*/abc/def'
somedir/otherdir/abc/def

答案2

這個怎麼樣。

find /home/dir1/ -type d | grep 'abc/def'

或者您只想列出達到一定深度的目錄。

find /home/dir1/ -maxdepth 2 -type d | grep 'abc/def'

-d - directory

-maxdepth levels of directories below the starting-points.

答案3

tree <base dir>
       -L level
             Max display depth of the directory tree.
       -P pattern
              List  only those files that match the wild-card pattern.

相關內容