Breadth-first list is important, here. Also, limiting the depth searched would be nice.
$ find . -type d
/foo
/foo/subfoo
/foo/subfoo/subsub
/foo/subfoo/subsub/subsubsub
/bar
/bar/subbar
$ find . -type d -depth
/foo/subfoo/subsub/subsubsub
/foo/subfoo/subsub
/foo/subfoo
/foo
/bar/subbar
/bar
$ < what goes here? >
/foo
/bar
/foo/subfoo
/bar/subbar
/foo/subfoo/subsub
/foo/subfoo/subsub/subsubsub
I'd like to do this using a bash one-liner, if possible. If there were a javascript-shell, I'd imagine something like
bash("find . -type d").sort( function (x) x.findall(/\//g).length; )
My feeling is that this is a better solution than previously mentioned ones. It involves grep and such and a loop, but I find it works very well, specifically for cases where you want things line buffered and not the full find buffered.
It is more resource intensive because of:
This is good because:
You can also fit it onto one line fairly(?) easily:
But I prefer small scripts over typing...
Here's a possible way, using find. I've not thoroughly tested it, so user beware...
The
find
command supports-printf
option which recognizes a lot of placeholders.One such placeholder is
%d
which renders the depth of given path, relative to wherefind
started.Therefore you can use following simple one-liner:
It is quite straightforward, and does not depend on heavy tooling like
perl
.How it works:
If you want to do it using standard tools, the following pipeline should work:
That is,
To limit the depth found, add the -maxdepth argument to the find command.
If you want the directories listed in the same order that find output them, use "sort -n -s" instead of "sort -n"; the "-s" flag stabilizes the sort (i.e., preserves input order among items that compare equally).
You can use find command, find /path/to/dir -type d So below example list of directories in current directory :
Without the deserved ordering: find -maxdepth -type d
To get the deserved ordering, you have to do the recursion yourself, with this small shellscript:
Then you can call this script with parameters base directory and depth.