
特定のフォルダに含まれているディレクトリのリストを取得しようとしています。
次のフォルダーが用意されています。
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
獲得尾(ファイル名)。
答え2
次のような再帰を持つことができますfind
。
find / -type d -path *wp-content/plugins -exec find {} -maxdepth 1 -mindepth 1 -type d \;
答え3
強く打つ:
shopt -s globstar
printf "%s\n" **/wp-content/plugins/*
印刷:
bat/bar/foo/blog/wp-content/plugins/GHI
baz/wp-content/plugins/ABC
baz/wp-content/plugins/DEF
foo/bar/wp-content/plugins/XYZ
または
shopt -s globstar
for d in **/wp-content/plugins/*; do printf "%s\n" ${d##*/}; done
印刷:
GHI
ABC
DEF
XYZ
答え4
このtree
コマンドはまさにこの目的のために設計されています。フラグを使って深さを制御できます-L
。以下は、私が管理しているローカルWordPressサイトの例です。
$ tree -L 1 wp-content/
wp-content/
├── index.php
├── plugins
└── themes
2 directories, 1 file
$ tree -L 2 wp-content/
wp-content/
├── index.php
├── plugins
│ ├── akismet
│ ├── contact-form-7
│ ├── index.php
│ └── wordpress-seo
└── themes
├── index.php
├── twentyfifteen
└── twentysixteen
11 directories, 3 files
$ tree -L 1 wp-content/plugins/
wp-content/plugins/
├── akismet
├── contact-form-7
├── index.php
└── wordpress-seo
5 directories, 1 file