Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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).

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
Good catch. I forgot about slocate. –  uther Jun 4 '12 at 19:05
    
+1 locate -r /Dropnot$ worked for me –  Michael Durrant Jun 5 '12 at 0:27
add comment

I have mlocate installed. It is the default distributed by RedHat, so it will be on Fedora, RHEL, CentOS. From man 1 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.

Because \ is a globbing character, this disables the implicit replacement 
of NAME by *NAME*.
share|improve this answer
    
locate -b /Dropnot worked –  Michael Durrant Jun 5 '12 at 0:27
add comment

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'
share|improve this answer
add comment

In OSX, globbing is allowed but must be escaped. If no globbing is used, it is assumed to be on both sides of the search term. (This is all from the man pages).

In short,

locate '*/Dropnot'

works for me in OSX.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.