On small systems where there is no locate installed, How would an alias look like that gets the same result as locate?

I can imagine find can produce the same output so an alias could look like

alias locate="find / -name"

But that doesn't seem to work the same as locate:

locate test

will only find files with the name exactly called test while locate will find all files containing that.



locate *test*
  • it will work only if you pass on the regular expression. locate converts filename to *filename* by default , but find / -name filename will search for exact filename , so find will require find / -name *filename*
    – amisax
    Jun 25, 2015 at 8:05
  • Aliases do not allow (positional) arguments. Use a function instead. Read man bash and search for ALIASES and/or FUNCTIONS for further details
    – Lambert
    Jun 25, 2015 at 8:09
  • Really, if you need locate, why not just install it?. The mlocate implementation is in the findutils package on savannah.gnu.org, git://git.savannah.gnu.org/findutils.git
    – jthill
    Jul 4, 2015 at 23:31

4 Answers 4


To improve the huge speed impact on find you could simulate something like locate

alias locate="if [ ! -e /tmp/locate.db -a ! -e /tmp/locate.lockdb ]
then touch /tmp/locate.lockdb
trap \"rm /tmp/locate.lockdb; rm /tmp/locate.db; exit\" SIGHUP SIGINT SIGTERM
find /|tee /tmp/locate.db
chmod 666 /tmp/locate.db
rm /tmp/locate.lockdb
elif [ -e /tmp/locate.lockdb ]
then find /
else cat /tmp/locate.db
fi|grep "

Of course there is an issue with permissions! It would be better to write some setuid commands for tee and cat to write the database in super user mode and at a better location but /tmp.

A cheap alternative on most single or few user systems would be to write a per-user locate.db somewhere near $HOME.

Another nice alias is able to update/find. Hmm finally I think this alias works better than the original locate ;)

alias relocate="if [ ! -e /tmp/locate.lockdb ]
then rm /tmp/locate.db
locate "

Edit I actually though that relocate should just be used like the locate alias above. If you use the relocate without an argument you get an error. The idea is to use relocate "no file to search for" if you don't want to search but just update the database.

Ok, the find must be setuid'ed too. But then you can throw away your locate package. The grep argument should be passed through sed to quote the . dots.

NOTE FOR THE NOOBS: When I'm talking about setuid here DON'T SET THE SETUID FLAG ON TOOLS LIKE tee,cat or find. This would be a security breach of your system! What I mean is to write secure alternatives for these simple commands that work in setuid mode and that work in a restricted way, just for the purpose to provide fitted tools for this alias.

  • You could name your relocate alilas updatedb like the original
    – rubo77
    Jul 6, 2015 at 16:34
function locate_f()
        find / -path "*$1*" //Edit:path (as Gilles stated)
alias locate=locate_f
  • 3
    Or -path instead of -name, if your find supports it, to be closer to locate which allows locate foo/bar. Jun 25, 2015 at 22:38
  • 1
    To mimic locate even more, one can exclude certain folders. Tested on Arch Linux the function becomes: find / -regextype posix-extended -regex "/(afs|media|mnt|net|sfs|sys|tmp|proc|udev)" -prune -o -path "*$1*" -print
    – Pro Backup
    Dec 31, 2019 at 18:42


alias locate="find / | grep "

That should work exactly as locate - but of course it will be much slower


1) Add Alias globally for all users

echo 'alias locate="grep $1 /var/locate/locate.db"' >> /etc/bashrc

you could essentially point it to a shell script which you can do more intuitive argument selections... This should be adequate if you're just looking for very simple search.

2) Add Cron Job to run Daily.

if [[ ! -d /var/locate ]]; then
     mkdir /var/locate
rm -f $locatedb
for DIRS in `ls /|grep -v "proc"`
   find /$DIRS -type f >> $locatedb 2>&1 /dev/null

You'll have the same results as mlocate w/o installing mlocate... I just tested on CentOS.... and the results are ALMOST exactly identical.

  • This is more than just an alias, you need cron also, so this is not the answer to the question. Though this is a nice thing, you created here ;)
    – rubo77
    Jul 9, 2015 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .