How to use updatedb command as an ordinary user?
The locate
command is very useful tool on Linux, but it seems only root can run updatedb
command which is very unconvinent to use it. So how to make ordinary user to have the priviledge to run updatedb command?
updatedb
is the command use to update the db used by locate command.
But there is the following error message when try to run updatedb as ordinary user:
[mirror@home code]$ updatedb
updatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db'
Or:
updatedb -o db
updatedb: can not change group of file `/var/lib/gforge/chroot/home/users/bigmeow/tmp/db.uhEZFQ': Operation not permitted
Edit the command to:
updatedb --require-visibility 0 -o ~/.locate.db
from "updatedb (8)":
-l, --require-visibility FLAG
Set the 'require file visibility before reporting it' flag in the generated database to FLAG.
If FLAG is 0 or no, or if the database file is readable by "others" or it is not owned by slocate, locate(1) outputs the database entries even if the user running locate(1) could not have read the directory necessary to find out the file described by the database entry.
If FLAG is 1 or yes (the default), locate(1) checks the permissions of parent directories of each entry before reporting it to the invoking user. To make the file existence truly hidden from other users, the database group is set to slocate and the database permissions prohibit reading the database by users using other means than locate(1), which is set-gid slocate.
Note that the visibility flag is checked only if the database is owned by slocate and it is not readable by "others".
You can just create database in home with -o
argument of updatedb
:
updatedb -o ~/.locate.db
And use it with slocate
like this:
slocate --database=~/.locate.db <pattern>
You probably want to define an alias for slocate --database=~/.locate.db
.
Here are all the steps to have a complete solution (tested in Centos 6.5)
1) generate the db:
updatedb --require-visibility 0 -o ~/.locate.db
2) use the db:
locate --database=/full/path/to/.locate.db (does not work with ~)
or
locate --database=.locate.db
3) create an alias:
alias mylocate='locate --database=/full/path/to/.locate.db'
4) use your locale locate db:
mylocate <my pattern>