一致するディレクトリより 1 レベル深いディレクトリのリストを探します。

一致するディレクトリより 1 レベル深いディレクトリのリストを探します。

特定のフォルダに含まれているディレクトリのリストを取得しようとしています。

次のフォルダーが用意されています。

foo/bar/test
foo/bar/test/css
foo/bar/wp-content/plugins/XYZ
foo/bar/wp-content/plugins/XYZ/js
foo/bar/wp-content/plugins/XYZ/css
baz/wp-content/plugins/ABC
baz/wp-content/plugins/ABC/inc
baz/wp-content/plugins/ABC/inc/lib
baz/wp-content/plugins/DEF
bat/bar/foo/blog/wp-content/plugins/GHI

私は以下を返すコマンドが欲しい:

XYZ
ABC
DEF
GHI

デフォルトでは、wp-content/plugins/内のフォルダを探しています。

を使用するとfind最も近いですが、-maxdepthフォルダが検索した場所とは異なるため、できません。

すべてのサブディレクトリを再帰的に返すには、次のコマンドを実行します。

find -type d -path *wp-content/plugins/*

foo/bar/wp-content/plugins/XYZ
foo/bar/wp-content/plugins/XYZ/js
foo/bar/wp-content/plugins/XYZ/css
baz/wp-content/plugins/ABC
baz/wp-content/plugins/ABC/inc
baz/wp-content/plugins/ABC/inc/lib
baz/wp-content/plugins/DEF
bat/bar/foo/blog/wp-content/plugins/GHI

ベストアンサー1

-prune見つかったディレクトリが次の場所にドロップダウンしないように、1つだけを追加してください。

find . -type d -path '*/wp-content/plugins/*' -prune -print

*wp-content/plugins/*これもシェルグローブなので、引用する必要があります。

フルパスではなくディレクトリ名のみが必要な場合は、上記のコマンドの出力をパイプするか、ファイルパスにfindGNU置換を-print使用して改行文字が含まれていないと仮定できます(ファイルパスに有効な文字のみが含まれていると仮定)。-printf '%f\n'awk -F / '{print $NF}'sed 's|.*/||'

そしてzsh

printf '%s\n' **/wp-content/plugins/*(D/:t)

**/zshすべてのレベルのサブディレクトリです(初期のNightiesで開始され、現在他のほとんどのシェルで見つかった機能(たとえば、、、、通常はいくつかのksh93オプションtcshfish下にあります))、bash種類のファイルのみyash(/)目次D隠された(ポイント)を含む:t獲得(ファイル名)。

おすすめ記事