Как найти каталог в каталоге?

Вы можете просто добавить запятую в конце каждой строки, а в последней строке удалить ее:

sed -e 's/$/,/' -e '$s/,$//'
4
01.11.2020, 14:20
4 ответа

Используйте GNU findс -path, который ищет совпадения по всему пути:

$ find. -path '*/c/e'
./a/c/e

Это будет соответствовать любому файлу или каталогу с именем e, который находится в каталоге с именем c.

В качестве альтернативы, если у вас нет GNU findили любого другого, поддерживающего -path, вы можете сделать:

$ find. -type d -name c -exec find {} -name e \;
./a/c/e

Хитрость заключается в том, чтобы сначала найти все c/каталоги, а затем искать только в них то, что называется e.

17
18.03.2021, 22:53

Поскольку вы пометили Bash, альтернативой является использование globstar:

shopt -s globstar # Sets globstar if not already set
# Print the matching directories
echo **/c/e/
# Or put all matching directories in an array
dirs=(**/c/e/)
5
18.03.2021, 22:53

Использование инструмента Fd:

fd -t d --full-path /c/e$

https://github.com/sharkdp/fd

0
18.03.2021, 22:53

В дополнение к решению @terdon я привожу здесь альтернативную версию для тех, у кого нет GNU find (, которую я смог найти, только следуя его идее!):

find. -type d -name 'c' -exec find '{}/e' -type d \( -name 'e' -ls -o -prune \) \; 2>/dev/null 

Кажется, это работает на моей машине

Для проверки:

# add files under each directories as otherwise some solutions would 
# list also files under any "c/e" subdirs... 
# in a subdir : do  : 
mkdir -p a b c a/b a/c a/c/e a/c/d/e a/c/d/e/c/e/f/g
for i in $(find */ -type d -ls); do ( cd "$i" && touch a b c d e ) ; done 
# this will creates several files under each subdirs, wherever it can (ie, when they don't match a subdir's name).
# then test:
find. -type d -name 'c' -exec find '{}/e' -type d \( -name 'e' -ls -o -prune \) \; 2>/dev/null 
# and it answers only the 2 matching subdirs that match name "c/e":
inode1 0 drwxr-xr-x   1 uid  gid   0 nov.  2 17:57./a/c/e
inode2 0 drwxr-xr-x   1 uid  gid   0 nov.  2 18:02./a/c/d/e/c/e
2
18.03.2021, 22:53

Теги

Похожие вопросы