How can I use locate only for a directory
I have mlocate
installed. It is the default distributed by RedHat, so it will be on Fedora, RHEL, CentOS. From man locate
-b, --basename
Match only the base name against the specified patterns. This
is the opposite of --wholename.
So if you run locate -b '/Dropknot'
, it will only report files or directories with exactly that string.
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
.
With GNU locate (other locate implementations might differ):
locate '*/Dropnot'
locate Dropnot | grep '/Dropnot$'
When there is no wildcard in the argument, locate
looks for a path having the specified search term as a substring. When the argument is a shell pattern containing one or more wildcard characters, locate
looks for a complete match. If you don't want to output the /Dropnot
at the end:
locate -0 '*/Dropnot' | xargs -0 -n1 dirname
locate Dropnot | sed -n 's:/Dropnot$::p'