8

I am a Windows user and coming from Windows with all the indexing and eternal wait times when searching for files, I find it surprising how fast does locate work or the autocompletion (that I know) work in linux.

Is there any indexing being done in the background or how is this achieved? I still have a pretty clean installation, so maybe this goes bad with time, but in Windows the moment you want to search on a folder which is not indexed you have to wait for some seconds.

dr_
  • 29,602
oli206
  • 759

2 Answers2

18

I find it surprising how fast does locate work or the autocompletion (that I know) work in linux. ... Is there any indexing being done in the background or how is this achieved?

This is actually two completely distinct questions.

locate uses an index (slocate stores it in /var/lib/slocate/), that is updated by a nightly cron job. This nightly job typically runs at about 1 or 2AM local time, and completely scans your entire system (including all connected drives). The resulting index is simply a list of filenames.

Auto-complete is handled by your shell. Most systems use bash, so bash-completion is the collection of scripts that manage how this works. (zsh has a similarly-named collection, and most of the other shells have some form of completion built-in.) When Tab is pressed, the shell runs a script that decides, based on what you've typed already, what, exactly, needs to be completed. The script then generates a list of possible completions, which may or may not be the list of files in the current directory, or the list of executable files in your $PATH. The locate command is normally not used for this.

greyfade
  • 640
  • 1
    Thanks! I see, so it's not so "magical" :) – oli206 Oct 10 '10 at 18:48
  • greyfade has ruined the magic forever -_- – Michael Mrozek Oct 10 '10 at 22:14
  • @Michael Mrozek: Sorry. :( – greyfade Oct 11 '10 at 02:30
  • The shell runs a script sounds not 100% correct, when implementing a shell people shouldn't be misguided in thinking they should write external scripts. It's simply a routine in the code itself, it may or not access external files for configuration convenience, but simple built-in completion works well. – Aki Apr 20 '12 at 17:14
  • @Aki: The majority of customized autocompletion in bash is implemented by scripts in /etc/bash_completion.d/. You're right, they need not be shell scripts, but most of the time they are. (zsh keeps its completion scripts in /usr/share/zsh/site-functions/) – greyfade Apr 20 '12 at 18:27
  • @greyfade bash_completion is not part of bash. It is an external project handled by Debian. It has nothing to do with what happens by default when you press Tab in a bash shell :-) – Chris Down Feb 14 '14 at 07:49
  • @ChrisDown: I'm aware of this. But bash-completion is offered in most distros, sometimes installed by default. I named the package specifically for those who don't have it and want to install it. – greyfade Feb 14 '14 at 16:41
  • So deleting of file is still being shown using locate. :-) Thank you – Nabin Jun 07 '16 at 12:46
  • I cannot find /var/lib/slocate on ubuntu 14.04 though – Nabin Jun 07 '16 at 12:48
  • 1
    @NabinKhadka Since my answer was written, I think Ubuntu changed to another locate service. I don't know which one - I don't use Ubuntu - but mlocate comes to mind. Check /etc/updatedb.conf. It may have the default path for whichever locate tool you have. – greyfade Jun 07 '16 at 16:39
5

Usually, locate uses an index which is generated once a day via a cron-job (/etc/cron.daily/mlocate on my system for example). It does nothing fancy, basically a complete filesystem traversal with some optimizations and the building of the index data structure.

Shells use probably some internal caching for command completion, but does not use a global index file. Besides, usually, Unix kernels maintain a dentry-cache, i.e.\ they cache file directory information used by directory listings etc. in memory (including the stat'ing of non-existent files - which is also called inverse caching).

maxschlepzig
  • 57,532