Is there an easy way to list only directories under a given directory in Linux? To explain better, I can do:
find mydir -type d
which gives:
mydir/src
mydir/src/main
mydir/bin
mydir/bin/classes
What I want instead is:
mydir/src/main
mydir/bin/classes
I can do this in a bash script that loops over the lines and removes previous line if next line contains the path, but I'm wondering if there is a simpler method that does not use bash loops.
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
find . -type d | sort | awk '$0 !~ last "/" {print last} {last=$0} END {print last}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With