Gokul N K
Gokul N K

Reputation: 2458

List all directories that contain a file with a particular extension

I need list all the directories that contain a file with .info extension in the first level.

  --contrib
     --abc
       --ab.info 
     --def
       --de.info 
     --xyz
       --ab.gh
       --ab.ij

The command should list

abc, def

Upvotes: 2

Views: 1959

Answers (1)

Jakub Kotowski
Jakub Kotowski

Reputation: 7581

This should work if you run it from your contrib directory:

find . -maxdepth 2 -name "*.info" -exec dirname {} \;

It will need more tweaking if you actually want to run it from the parent of contrib.

The above will give you:

./abc
./def

Which is not exactly what you wanted. So maybe something more like this will help:

find . -maxdepth 2 -name "*.info" -exec sh -c 'F=$(dirname {}) ; basename $F' \;

It is more convoluted but the result is:

abc
def

Or without basename and dirname:

find . -maxdepth 2 -name "*.info" -exec bash -c '[[ {} =~ .*/(.*)/.* ]] && echo ${BASH_REMATCH[1]}' \;

Or with sed:

find . -maxdepth 2 -name "*.info" -exec echo {} + | sed 's|./\(\S*\)/\S*|\1,|g'

Result:

abc, def, 

Upvotes: 9

Related Questions