1

I want to pass multiple DB files to the locate command, like this:

locate -d ~/.a_locate.db:~/.b_locate.db: -Ai file_to_find

But this gives me this error:

locate: can not stat () `~/.b_locate.db': No such file or directory

The man page for locate says:

-d, --database DBPATH Replace the default database with DBPATH.

DBPATH is a :-separated list of database file names. If more than one --database option is specified, the resulting path is a concatenation of the separate paths.

I don't clearly understand what is meant by 'concatenation of separate', What am I doing wrong? I tried giving the full path (/home/user/.b_locate.db) and it worked. Can someone explain this behaviour?

(I'm using mlocate package in Arch linux)

2

The second ~ isn’t being expanded; try

locate -d "${HOME}/.a_locate.db:${HOME}/.b_locate.db:" -Ai file_to_find

instead, or, since this is zsh, just

locate -d $HOME/.a_locate.db:$HOME/.b_locate.db: -Ai file_to_find

The reason is that ~/ is only expanded at the beginning of a shell word. A shell word only ends at whitespace (as far as it matters here — the actual rules are much more complicated). ~/foo:~/bar is a single word, which begins with ~/ so the leading ~ is expanded to your home directory, but the middle ~ is nothing special so it stays a tilde. There's an exception on the right-hand side of an assignment: in PATH=~/foo:~/bar, ~/ is expanded after the = assignment sign and after a : on the right-hand side.

  • Are you psychic :p Yes, this works, and so does locate -d "~/.a_locate.db:${HOME}/.b_locate.db:" -Ai file_to_find. Would you happen to know the reason behind this behavior? – aksh1618 Mar 29 '18 at 20:17
  • @A.K.S.H. See the edited answer. – Gilles 'SO- stop being evil' Mar 29 '18 at 20:50
  • Multiple -d options are allowed. So I think it's ok with : locate -d ~/.a_locate.db -d ~/.b_locate.db: -Ai file_to_find . Note that the -A option dont' exist on OpenBSD. – ctac_ Apr 1 '18 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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