I was wondering how to restrict the search range of "locate" to some directory?
For example, how to find files/directories with string "math" in their names under some directory "/home/tim/science/"?
Thanks and regards!
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
UPDATE: A note about using locate's regex option vs the shell's filename expansion in relation to locate's args ...
SHELL FILENAME EXPANSION
When you type
locate /bin/b* (note there are no quotes), several things happen.
locateas many individual commandline args.
locatethen tests each of these args against each of the files in its database, and outputs matches.
Take note however, that 'locate' has no concept of the current working directory. It only matches your args against the fully-qualified file-names in its database.
This means that it will match both
You have some degree of control this way, but locate's regex search offers more; as do some other of locate's options.
LOCATE'S REGEX OPTION
When you type
locate -r "/bin/b.*" different things happen than do with simple args.. (shell-expanded-args are just a list of simple args)
-roption tells locate to treat your arg as a regex pattern.
locate -r "^/bin/b.*"produces file names beginning with /bin/b ... eg2.
locate -r ~/"bin/b.*"produces file names beginning with /home/user/bin/b .. Note that the ~/ is not protected by "quotation-marks" and is therefore subject to shell tilde-expansion which converts ~/ into /home/user/
Summary: "shell filname expansion" is quite different to 'regex'
Now back to the original post:
locate typically lists the entire database to stdout, so to limit it to a particular directory you need to filter it... You can do this via locate's options.
You can use locate's regex capability, rather than just shell-globbing ...(
locate /dir/myfile* is shell-globbing)...
From info locate:
note: -i = ignore case
. . . . . -r = regex
. . . . . ~/ will be expanded by the shell to /home/user (when ~/ is not in quotes)
. . . . . '\ ' (baskslash+space; no quotes) is a literal space (to avoid shell expansion). You can put the string in quotes, but be sure to leave the ~/ outside the quotes:
~/my\ regex or
locate -ir ~/".*"la\ bella\ vita is the same as
locate -ir ~/".*la bella vita" and will search your entire home directory for "La Bella Vita" occurring anywhere.
You can also limit the search to just the file name but using the
--basename .. in which case the regex start-of-line
^ applies to only the basename...
I most commonly find myself using
Locate is so fast, that I have never used
find ... nor have I used nautilus search (too slow)
With 'locate', if you are looking for something which is newly added, just run
sudo updatedb to refresh locate's database... (but if you've newly added it, you probably know where it is :)....
You can use
locate /rootpath/*filenamespec.I hadn't tried it before, but it appears to work. To do your example, it would be
You may want to look at the
find command rather than
locate for that sort of behaviour. The syntax would be
find rootforsearch -name filenamepattern -print. In this case, your example would require
find /home/tim/science -name *math* -print.Not as fast as locate as there's no database to search ... it actually searches the filesystem. There are lots of options you can use other than print as well, if you intend to actually do something with the file.