![일치하는 디렉터리보다 한 수준 더 깊은 디렉터리 목록을 찾습니다.](https://linux55.com/image/106498/%EC%9D%BC%EC%B9%98%ED%95%98%EB%8A%94%20%EB%94%94%EB%A0%89%ED%84%B0%EB%A6%AC%EB%B3%B4%EB%8B%A4%20%ED%95%9C%20%EC%88%98%EC%A4%80%20%EB%8D%94%20%EA%B9%8A%EC%9D%80%20%EB%94%94%EB%A0%89%ED%84%B0%EB%A6%AC%20%EB%AA%A9%EB%A1%9D%EC%9D%84%20%EC%B0%BE%EC%8A%B5%EB%8B%88%EB%8B%A4..png)
특정 폴더에 포함된 디렉터리 목록을 얻으려고 합니다.
다음 예제 폴더가 제공됩니다.
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
찾은 디렉토리가 다음 위치에 드롭다운되지 않도록 하나만 추가하세요 .
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