List all leaf subdirectories in linux

If you want only the leaf directories (directories which don't contain any sub-directory), look at this other question. The answer also explains it, but in short it is:

find . -type d -links 2

I can't think of anything that will do this without a loop. So, here are some loops:

This displays the leaf directories under the current directory, regardless of their depth:

for dir in $(find -depth -type d); do [[ ! $prev =~ $dir ]] && echo "$dir" ; prev="$dir"; done

This version properly handles directory names containing spaces:

saveIFS=$IFS; IFS=$'\n'; for dir in $(find -depth -type d ); do [[ ! $prev =~ $dir ]] && echo "${dir}" ; prev="$dir"; done; IFS=$saveIFS

Here is a version using Jefromi's suggestion:

find -depth -type d | while read dir;  do [[ ! $prev =~ $dir ]] && echo "${dir}" ; prev="$dir"; done

find . -type d | sort | awk '$0 !~ last "/" {print last} {last=$0} END {print last}'

If you're looking for something visual, tree -d is nice.

drinks
|-- coke
|   |-- cherry
|   `-- diet
|       |-- caffeine-free
|       `-- cherry
|-- juice
|   `-- orange
|       `-- homestyle
|           `-- quart
`-- pepsi
    |-- clear
    `-- diet

Tags:

Bash