Как найти каталог с наименьшей глубиной? - PullRequest
4 голосов
/ 04 февраля 2020

Как найти каталог с наименьшей глубиной? Ниже скрипт находит dir bb с наименьшей глубиной, но я думаю, что должен быть лучший путь / один вкладыш? здесь он печатает путь с наименьшим числом / s в нем.

$ tree
.
├── a
│   └── bb
│       └── c
│           └── d
│               └── bb
├── m
│   └── n
│       └── k
│           └── bb
└── x
    └── y
        └── bb
            └── z

n=100
for f in $(find . -name bb -type d); do
  l=$(echo $f | tr -cd '/' | wc -c)
  if [[ $l -lt $n ]]; then
     n=$l
     shortest_bb=$f
  fi  
done
echo $shortest_bb

Ответы [ 2 ]

1 голос
/ 04 февраля 2020

Переводы строки разрешены в «однострочниках»:

# set up the test directories
mkdir -p a/bb/c/d/bb m/n/k/bb x/y/bb/z

# and find the shortest "bb" path
find . -type d -name bb |
  awk -F/ '{print NF "\t" $0}' |
  sort -k1,1n |
  head -1 |
  cut -f2
./a/bb
0 голосов
/ 04 февраля 2020

Вы можете сделать это одним вкладышем:

find . -type d | awk '{ print gsub(/\//,$0) " " $0 }' | sort -rn

И он выведет глубину в первом столбце:

   5 /test/test.xcodeproj/xcuserdata/Matias-Barrios.xcuserdatad/xcschemes
   5 ./test/test.xcodeproj/project.xcworkspace/xcuserdata/Matias-Barrios.xcuserdatad
   5 ./TestQueue/TestQueue.xcodeproj/xcuserdata/Matias-Barrios.xcuserdatad/xcschemes
   5 ./TestQueue/TestQueue.xcodeproj/xcuserdata/Matias-Barrios.xcuserdatad/xcdebugger
   4 ./TestQueue/TestQueue/Assets.xcassets/seven.imageset
   4 ./TestQueue/TestQueue/Assets.xcassets/question.imageset
   4 ./TestQueue/TestQueue/Assets.xcassets/poker.imageset

И так далее. Надеюсь, это поможет

...