linuxbashlocate

Linux's locate command returning the path of a deleted file


I am writing a bash shell script which removes a file using the following command

rm abc.xsl

I handled the case of getting the prompts and providing the required 'y' or 'n' inputs.

In order to verify that the file has been removed, I validate the same by running the locate command in the following manner

locate */abc.xsl

Note : the desired file is present at a specific directory, so in my case this particular command is not giving me any other file's address

I noticed that despite of file being deleted, the locate command is still reflecting the deleted file's path.

I debugged the issue by manually executing each of the required commands

FYR

admin@sharad-server$ pwd
/usr/local/myserver/myapplication/lib
admin@sharad-server$ rm abc.xsl
rm: remove write-protected regular file 'abc.xsl'? y
admin@sharad-server$ locate */abc.xsl
/usr/local/myserver/myapplication/lib/abc.xsl

The file was deleted as it was not reflected when I executed the ls command and also the cat command as follows

admin@sharad-server$ cat /usr/local/myserver/myapplication/lib/abc.xsl
cat: /usr/local/myserver/myapplication/lib/abc.xsl: No such file or directory

FYR my application is deployed on GCP and my server's platform details are as follows:

admin@sharad-server$ uname -a
Linux sharad-server 3.10.0-1127.8.2.el7.x86_64 #1 SMP Tue May 12 16:57:42 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

The details of the shell where all these commands were executed in a standalone manner are as follows:

admin@sharad-server$ ps -p $$
  PID TTY          TIME CMD
32296 pts/0    00:00:00 bash

Why the locate command is still reflecting the deleted file's path?

Update:

As per one of the suggestion I tried the updatedb command as well but it gave me an error.

admin@sharad-server$ updatedb
updatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db'

Final Update:

The updatedb command worked with the root user and has successfully updated the locate command functioning.


Solution

  • For performance reasons, Locate use an index where all file tree structure is stored to find something... This file need to be updated with the command updatedb #root permision needed. If you want to find a file or directory performing "live analyses" you'll need to use find.