Is there a way to find a maximum depth of given directory tree? I was thinking about using find with incrementing maxdepth and comparing number of found directories, but maybe there is a simpler way?
3 Answers
One way to do it, assuming GNU find
:
find . -type d -printf '%d\n' | sort -rn | head -1
This is not particularly efficient, but it's certainly much better than trying different -maxdepth
s in turn.
Try this, for example trying to find max depth of tree under /
, using
find / -type d
will give every directory under /
irrespective of depth. So awk
the result with /
as delimiter to find the count, and count-1
would give max depth of tree from /
, so the command would be:
find / -type d | awk -F"/" 'NF > max {max = NF} END {print max}'
Consider using updatedb
and then locate
. It performs really fast, as locate
queries a database and it's much faster than asking for each file sepereately. updatedb
is fast too, since it only updates the database with changes instead of creating it anew. The only drawback is that you may need root privilages to run updatedb
by default, unless you make and preserve your own, user-space database.
Keep in mind that you may have newlines in your filenames, so that you need to be more careful about processing locate
's output. Best to use null byte as a filename separator.
To sum it up:
updatedb && locate -0 "$(pwd)" | awk -v RS='\0' -F"/" '{print NF-1}' | sort -n | tail -n1
Thanks @sai sasanka for providing me with a perfect awk script.
-
updatedb
runsfind
, so this can't be faster than runningfind
just to extract the desired information. Jun 7, 2017 at 21:25 -
1@Gilles it does not run
find
. Fromman updatedb
"If the database already exists, its data is reused to avoid rereading directories that have not changed.". We are counting on reusing the database. Moreover, it can be seen as preprocessing which allows you to answer quickly for many queries. On my system with HDD driveupdatedb
took 2 seconds to finish, so it's really fast compared to other solutions. Jun 7, 2017 at 22:10 -
Using updatedb would be useful to make multiple queries, which is not mentioned in the question. Even so,
updatedb
records all files, which is a waste: for this, only directories are useful, so running and storing the output offind -type d
would be faster. Jun 7, 2017 at 22:14 -
@Gilles from
man updatedb
"updatedb
is usually run daily bycron
(8) to update the default database.". Unless the OP has created and deleted many files in his filesystem it's much faster to update it and query the database than to run it again. And based on ext4 filesystem documentation it doesn't really matter if you apply-d
flag or not. You must read the whole directory table anyway to checkfiletype
flag of each file - and when reading this flag OS will load sequential bytes (with filename) to memory. Jun 8, 2017 at 12:25 -
Unless no directory has been created, moved or deleted since the last
updatedb
run, it's necessary to runupdatedb
again. It is possible to optimizefind -type d
to not read the whole directory table, and in particular to not read the content of a leaf directory: the hard link count on a directory indicates how many subdirectories it has. GNU find does not implement this optimization though. Jun 8, 2017 at 13:10
/
folder you are?/home
directory?