![UNIX 명령 트리는 패턴과 일치하는 디렉터리만 표시할 수 있습니까?](https://linux55.com/image/43963/UNIX%20%EB%AA%85%EB%A0%B9%20%ED%8A%B8%EB%A6%AC%EB%8A%94%20%ED%8C%A8%ED%84%B4%EA%B3%BC%20%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%A7%8C%20%ED%91%9C%EC%8B%9C%ED%95%A0%20%EC%88%98%20%EC%9E%88%EC%8A%B5%EB%8B%88%EA%B9%8C%3F.png)
tree
UNIX 명령 에서 특정 패턴과 일치하는 디렉터리만 표시 하도록 하는 방법이 있는지 알아내려고 노력 중입니다 .
% tree -d tstdir -P '*qm*' -L 1
tstdir
|-- d1
|-- d2
|-- qm1
|-- qm2
`-- qm3
5 directories
이것매뉴얼 페이지스위치에 대한 일부 정보를 표시합니다.
-P 모드는 와일드카드 패턴과 일치하는 파일만 나열합니다. 참고: 점
.' for matching. Valid wildcard operators are
*'(0개 이상의 문자),?' (any single character),
[...]'(대괄호 사이에 나열된 단일 문자) 로 시작하는 파일을 고려하려면 -a 옵션을 사용해야 합니다 (문자 범위를 나타내려면 선택 사항 -( 대시)). 예를 들어 [AZ]) 및[^...]' (any single character not listed in brackets) and
|'를 사용하여 대체 패턴을 구분할 수 있습니다.
나는 대략 추측하고있다...해당 파일만 나열합니다...문제입니다. 이 스위치가 파일에 대해서만 패턴 일치를 수행한다는 내 해석이 맞습니까?아니요목차?
편집 #1
@f-하우리매뉴얼 페이지에서 사용할 수 있는 스위치를 사용하면 이것이 예상대로 작동하지 않는 가장 좋은 이유가 있는 것 같습니다 tree
. 오류 부분에서 이걸 놓쳤네요.
BUGS
Tree does not prune "empty" directories when the -P and -I options are
used. Tree prints directories as it comes to them, so cannot accumu‐
late information on files and directories beneath the directory it is
printing.
이러한 제한을 고려하면 tree
포괄적인 필터 목록을 수행하는 가장 좋은 방법은 아니지만 배타적 필터 목록은 -I
이 스위치를 사용하는 대신 사용할 수 있습니다.
대신 쉘 와일드카드, find 명령 또는 Perl 스크립트가 이 작업을 수행하는 데 더 적합한 방법입니다. 바라보다작성자: @f-hauri이것은 이러한 대안 중 일부에 대한 훌륭한 답변입니다.
답변1
응 이건허점. 매뉴얼 페이지에서:
BUGS
Tree does not prune "empty" directories when the -P and -I options are
used. Tree prints directories as it comes to them, so cannot accumu‐
late information on files and directories beneath the directory it is
printing.
... 어쨌든 -d
질문 전환아니요문서 인쇄:
-d List directories only.
따라서 이를 사용하고 싶다면 다음을 수행할 수 있습니다.
tree tstdir -P '*qm*' -L 1 | grep -B1 -- '-- .*qm'
|-- id1
| `-- aqm_P1800-id1.0200.bin
--
|-- id165
| `-- aqm_P1800-id165.0200.bin
|-- id166
| `-- aqm_P1800-id166.0200.bin
--
|-- id17
| `-- aqm_P1800-id17.0200.bin
--
|-- id18
| `-- aqm_P1800-id18.0200.bin
--
|-- id2
| `-- aqm_P1800-id2.0200.bin
-L 1
전체적으로 , 당신이 사용한다면
-L level
Max display depth of the directory tree.
다음 구문을 (bash에서) 더 잘 사용할 수 있습니다.
cd tstdir
echo */*qm*
또는
printf "%s\n" */*qm*
dir만 필요한 경우:
printf "%s\n" */*qm* | sed 's|/.*$||' | uniq
어쨌든 만약에순수한 배쉬:
declare -A array;for file in */*qm* ;do array[${file%/*}]='';done;echo "${!array[@]}"
이는 다음과 같이 해석될 수 있습니다.
cd tstdir
declare -A array # Declare associative array, (named ``array'')
for file in */*qm* ;do # For each *qm* in a subdirectory from there
array[${file%/*}]='' # Set a entry in array named as directory, containing nothing
done
echo "${!array[@]}" # print each entrys in array.
...패턴과 일치하는 파일이 없으면 결과가 표시됩니다 *
. 따라서 작업을 완벽하게 완료하려면 다음도 수행해야 합니다.
resultList=("${!array[@]}")
[ -d "$resultList" ] || unset $resultList
(이것보다는 낫겠지
declare -A array
for file in */*qm*; do
[ "$file" == "*/*qm*" ] || array[${file%/*}]=''
done
echo "${!array[@]}"
)
답변2
당신은 내 것을 사용할 수 있습니다albo 명령. 설치하다:
ln -s "$PWD"/arbo.py ~/bin/arbo
이제 다음을 수행할 수 있습니다.
find tstdir -maxdepth 1 -iname '*qm*' |arbo --color
출력은 ls와 동일한 색상으로 다음과 같습니다.
git arbo
bedup
├─ __init__.py
├─ __main__.py
├─ platform/__init__.py
├─ termupdates.py
├─ test_bedup.py
└─ tracking.py
setup.py
tox.ini