Question

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!

Was it helpful?

Solution

Using find:

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

OTHER TIPS

If you don't mind the weird .., you can just do:

$ ls -d /data/data_file/*/division_A/..

It will output something like /data/data_file/form_100/division_A/.. and you can access it like normal folders.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top