特定のフォルダに含まれているディレクトリのリストを取得しようとしています。
次のフォルダーが用意されています。
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/*
これもシェルグローブなので、引用する必要があります。
フルパスではなくディレクトリ名のみが必要な場合は、上記のコマンドの出力をパイプするか、ファイルパスにfind
GNU置換を-print
使用して改行文字が含まれていないと仮定できます(ファイルパスに有効な文字のみが含まれていると仮定)。-printf '%f\n'
awk -F / '{print $NF}'
sed 's|.*/||'
そしてzsh
:
printf '%s\n' **/wp-content/plugins/*(D/:t)
**/
zsh
すべてのレベルのサブディレクトリです(初期のNightiesで開始され、現在他のほとんどのシェルで見つかった機能(たとえば、、、、通常はいくつかのksh93
オプションtcsh
のfish
下にあります))、bash
種類のファイルのみyash
(/)
目次、D
隠された(ポイント)を含む:t
獲得尾(ファイル名)。