0

I wrote a simple shell script which uses the 'find' command so non-technical users can do a search on the file server through a web browser on the LAN. It works, but sometimes it has a long execution time, and then the follow-up searches are quick.

Is there a way to force a cache for 'find' or perhaps run something in the CRON to do this daily or a few times a day so users don't have to wait so long to get their first results?

Edward_178118
  • 955
  • 4
  • 15
  • 33

2 Answers2

3

The locate command is designed to do exactly what you want. The software package includes a (typically daily) cron job that creates a database of existing files, and then the locate command will use that database to search for files instead of crawling through the filesystem tree.

Your Linux distribution may even offer you several implementations of locate: in addition to the implementation included in GNU findutils, there may be newer implementations slocate and/or mlocate.

  • Both slocate and mlocate will filter out any search results the user running the search would not be able to access, according to file and directory permissions.

  • mlocate will also speed up database updates by detecting which directories have not changed after the previous database update.

telcoM
  • 4,448
  • 15
  • 25
  • 1
    So if I want to restrict locate to search for foobaz, this would work and won't be delayed by it collecting a cache? locate -ir '^/home/share/\([^.][^/]\+/\)\+foobaz[^/]*$' – Edward_178118 Mar 09 '20 at 09:27
1

The answer provided by @telcoM is probably the correct one you should follow. However, around ten years ago I once had no choice other than to keep the whole directory tree cached.

My solution back then was to put something like

*/5 * * * * /usr/bin/find / 2>&1 >/dev/null

to my cron, so the directory tree was scanned very often and remained cached.

Janne Pikkarainen
  • 31,852
  • 4
  • 58
  • 81