Search code examples
unixls

unix ls directory with sub-directory exists


In Unix, is it possible to use one command ONLY to list the directory if a sub-directory exists?

For example, I would like to list the directory name if it contains a sub-directory called "division_A"

/data/data_file/form_100/division_A
/data/data_file/form_101/division_A
/data/data_file/form_102/division_A

The desired result would be

form_100 
form_101 
form_102

I can only use 2 command lines to realize the goal.

cd /data/data_files
echo `ls -d */division_A 2> /dev/null | sed 's,/division_A,,g'`

So I would like to ask if anyone can use one command to proceed it.

Many Thanks!


Solution

  • Using find:

    find /data/data_file -type d -name division_A -exec sh -c 'basename `dirname {}`' \; 2> /dev/null