Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all leaf subdirectories in linux

Tags:

bash

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.

like image 273
amol Avatar asked Oct 15 '09 19:10

amol


2 Answers

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
like image 84
mivk Avatar answered Sep 18 '22 05:09

mivk


find . -type d | sort | awk '$0 !~ last "/" {print last} {last=$0} END {print last}'
like image 27
Brian Avatar answered Sep 22 '22 05:09

Brian