GNU 検索 - foo というサブディレクトリを含まないディレクトリのリスト

GNU 検索 - foo というサブディレクトリを含まないディレクトリのリスト

私が持っているもの:

次のコマンドを使用して、次のファイル構造を作成しました。mkdir dir{1..5} && mkdir dir{1,3,4}/foo

├── dir1
│   └── foo/
├── dir2
├── dir3
│   └── foo/
├── dir4
│   └── foo/
└── dir5

私が望むもの:

私はこのディレクトリを含まないディレクトリをfindリストするためにGNUコマンドを使用したいと思います。foo/

これまで私の唯一の考えは次のとおりです。

$ find . -type d -name 'foo' -exec dirname {} \; && echo '---' && find . -maxdepth 1 -type d
./dir1
./dir4
./dir3
---
.
./dir1
./dir4
./dir3
./dir2
./dir5

その後、手動で比較してみてください。より良い方法があるでしょう!

ベストアンサー1

ディレクトリを参照するには、シェル(GNUbash以降、たとえばzshGNUにアクセスする可能性が高い)を使用する必要があります。find

# /-----+----- "for" loop: for iteration variable chosen from list after "in"
# |     |   
# |  /-------- iteration variable is called "dir" 
# |  |  |   
# |  |  |  /-- choose from list of all directories
# |  |  |  |
# v  v  v  v  
for dir in */ ; do
  [[ -d "${dir}/foo" ]] || echo "${dir}"
# ^^ ^^              ^^ ^^
#  \  |              /  |
#   --------+--------   |
#     |     |           |
#     |     \-------------  [[ ]] : check for condition
#     |                 |
#     \-------------------  -d : condition "path exists and is directory"
#                       |
#                       \-  || if condition fails, do what is after this

done

おすすめ記事