How to use locate only for a directory

directorylocate

This finds a large number of files that are under various subdirectories of "Dropnot"

$ locate Dropnot

Can I find just the directory location with locate? (which directory "Dropnot" is in)

So if Dropnot is in /home/me/, that's the only entry that gets returned.

If so, what's the simplest / shortest way ?

Preferably through a flag or symbol rather than piping out and greping for it, etc, but I'd take anything as an option.

Maybe some sort of Dropnot$ for end of line? (but didn't work).

Best Answer

There is no option to use locate to find selected type of file (like directory), but you can use syntax from your question - Dropnot$ to find lines that ends with Dropnot. For that you must use -e option to locate to turn on POSIX regular expression.

In this case you should use:

locate -e Dropnot$

It is important what version of locate you have. In my system (Gentoo Linux) I have Secure Locate:

$ locate --version
Secure Locate 3.1 - Released March 7, 2006

in which there is no --basename option from uther's answer. This option is provided by GNU Locate from findutils package:

$ ./locate --version
locate (GNU findutils) 4.4.2

If you want to use regexp with GNU Locate you should use -r switch instead -e.

Related Question