Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create a customized user-centric locate database UNIX
If you have ever used the locate command within Terminal to find a file of yours that, for instance, ends in .doc, you might find that you end up with more than 60,000 hits on your system that you didn't create, and didn't want to know about. Hence, it may be beneficial to create a user-level database for locate that searches only your local directory structure, so that you only see what belongs to you. To do this, and have my new database updated automatically, I modified a copy of the locate database update program that came with the Mac, set up a crontab to update this database hourly, and then created an alias for my local locate, called llocate. Now when I type llocate .doc , I only find 584 .doc files, and I can rest assured that they are all mine! Read on for the how-to...

Step 1: Copy and modify database update program I first copied the original program to a new directory, with this command:
cp /usr/libexec/locate.updatedb ~/bin/llocate.updatedb
I then modified the permissions on my copy:
chmod 755 ~/bin/llocate.updatedb
Finally, I made a few changes to the location of the new database directory, changed the pruned paths, and added vfat to my available filesystems, so that lines 52 to 54 now looked like this:
${FCODES:=$HOME/Library/llocate.database}  # the database
${SEARCHPATHS:="$HOME"}  # directories to be put in the database
${PRUNEPATHS:="$HOME/tmp $HOME/Library "}  # unwanted directories
Step 2: set up crontab I created the file $HOME/bin/cron/crontab that contains the following line to get my llocate to update every hour at the 25 minute mark:
25 * * * *        $HOME/bin/llocate.updatedb
Then I load the command into my crontab using crontab $HOME/bin/cron/crontab, and make sure it worked by typing crontab -l.

Step 3: Create the alias Finally, I set up the alias to get the command to work automatically by adding the following command to my $HOME/.bashrc file:
alias llocate='locate -d $HOME/Library/llocate.database'
You should now be able to easily "llocate" your own files!
    •    
  • Currently 1.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (6 votes cast)
 
[10,432 views]  

Create a customized user-centric locate database | 10 comments | Create New Account
Click here to return to the 'Create a customized user-centric locate database' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create a customized user-centric locate database
Authored by: enigmamf on Apr 08, '08 09:17:12AM

Personally, I disabled the script that updates locatedb -- I got tired of it running at the worst possible times.



[ Reply to This | # ]
Create a customized user-centric locate database
Authored by: norz on Jul 05, '08 04:38:55AM
For those looking to disable updating of the locate database, commenting the appropriate lines in /private/etc/periodic/weekly/500.weekly should work.
To check what configuration file is used, read the source of the /usr/sbin/periodic script.

Other solution: set the weekly maintenance script to run at a better time. Use lingon to edit launchd configuration files.

[ Reply to This | # ]
Create a customized user-centric locate database
Authored by: monztre on Jan 24, '13 05:26:04AM

This 500.weekly file located in /private/etc/periodic/weekly/ no longer seems to exist in 10.8 ... there are two files there, but neither contain anything about the locate database to comment out. Perhaps something changed with Mountain Lion? I would like to skip the updating of the locate database altogether. Fairly useless to me.



[ Reply to This | # ]
Create a customized user-centric locate database
Authored by: CarlRJ on Apr 08, '08 11:08:16AM

Interesting approach, but you could skip steps 1 and 2 (and eliminate the overhead of the cron job running the extra process to build a duplicate-but-smaller locate database), and simply replace step 3's alias with:

function llocate { locate $* | egrep "^$HOME/"; }

That'll filter the 60,000ish lines returned for ".doc" through grep, returning the 584 that start with /Users/your_name/.

That said, I normally disable the weekly run of the Apple-supplied locate.updatedb, then install GNU findutils under /usr/local (I prefer GNU's find, xargs, and locate over BSD's), and set up /etc/daily.local so it updates the database every night (I get cranky with week-old results, and want all files locatable, rather than just those visible to user "nobody", since I'm the only user on the system). If I want up-to-the-minute results, there's always find or Spotlight.



[ Reply to This | # ]
Create a customized user-centric locate database
Authored by: avn on Apr 08, '08 02:38:48PM

While the method you suggested should work as well, it requires that the original locate command be set up and running on all user directories. Mine was not set to do this by default. Likewise, because of this others can search your files quite easily. Something that you may not want.

Also, because this script is set up to run by itself, it doesn't require that you run the system-wide locatedb program, and hence can be run for very frequent updates if your user account is not bloated. I chose to run mine every hour, but one could always choose the weekly updates.



[ Reply to This | # ]
Create a customized user-centric locate database
Authored by: locateUser on Sep 19, '11 02:00:19PM

Your approach assumes that your permissions are not by default restrictive. Locate's view of the filesystem is supposed to be of the user nobody, otherwise it would override the systems permissions.



[ Reply to This | # ]
Create a customized user-centric locate database
Authored by: daaku on Apr 09, '08 09:47:54AM

There's an easier way to generate the locate db:

25 * * * * find -H $HOME -type f | /usr/libexec/locate.mklocatedb > $HOME/Library/llocate.database



[ Reply to This | # ]
yes, but...
Authored by: avn on Apr 09, '08 09:56:28PM

you don't have the option to modify it for other needs. One such option, was that I removed my library by putting it in my prune path.

Also, I would note that your method wasn't really that much easier since you still needed to create the crontab and create an alias (which you are not explaining).



[ Reply to This | # ]
slocate
Authored by: sjk on Apr 20, '08 08:23:56PM
I prefer slocate, installable with MacPorts:

% port info slocate
slocate 3.1, Revision 1, sysutils/slocate (Variants: universal)
http://slocate.trakker.ca/

Secure locate provides a secure way to index and quickly search for files on your system. It uses incremental encoding just like GNU locate to compress its database to make searching faster, but it will also check file permissions and ownership so that users will not see files they do not have access to. The big advantage is that slocate will find files in your ~, even if you made it unreadable by nobody (who traditionally performs the locate database update), without letting another user find files there.

Platforms: darwin
Maintainers: pguyot@kallisys.net


[ Reply to This | # ]
Forgo the alias and read the man page.
Authored by: locateUser on Sep 19, '11 02:12:58PM
If you set the cron job to make your own version of the locate database, you don't need to use an alias to access it with a different command. In the man page for locate the use of the environment variable LOCATE_PATH is documented. Assuming that you have placed your version of the locate database in $HOME/Library/llocate.database, then modify your environment in one of your standard dot files (.bashrc is one):
    export LOCATE_PATH=$HOME/Library/llocate.database:
Note the trailing colon--this will allow locate to use your custom database for your own files and fall back to the system locate database. If you have reasonably restrictive permissions on your machine, then this will allow you to use the power of locate to search both your own filespace as well as what you should be able to see on the system as a regular user.

[ Reply to This | # ]