3

I'm new to linux. And I'm just exploring terminal.

  1. I created a file touch Desktop/vvv.
  2. Now I updated the database using sudo updatedb.
  3. The command locate vvv doesn't shows any result. While it just works fine with find / -name vvv .

I have found that is that locate doesn't seem to search in my home directory, as it shows results from usr, var, slib, opt, etc. directories.

Why I'm saying this is because when I try to locate some random file like complete which is in Download directory (which is not quite a unique name), it will show results from other directories but not Download.

Update: To demonstrate this, I've copied this from terminal:

aditya@ubuntu16:~$ cd /usr/

aditya@ubuntu16:/usr$ ls

bin  games  include  lib  local  locale  sbin  share  src

aditya@ubuntu16:/usr$ sudo touch aaaa

[sudo] password for aditya: 

aditya@ubuntu16:/usr$ ls

aaaa  bin  games  include  lib  local  locale  sbin  share  src

aditya@ubuntu16:/usr$ sudo updatedb

aditya@ubuntu16:/usr$ locate aaaa

/usr/aaaa

/var/cache/fontconfig/0bd3dc0958fa2205aaaa8ebb13e2872b-le64.cache-6

aditya@ubuntu16:/usr$ cd ../home/aditya/Desktop/

aditya@ubuntu16:~/Desktop$ touch aaaa

aditya@ubuntu16:~/Desktop$ updatedb

updatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db'

aditya@ubuntu16:~/Desktop$ sudo updatedb

sudo: unable to resolve host ubuntu16

aditya@ubuntu16:~/Desktop$ locate aaa

/usr/aaaa

/usr/share/app-install/desktop/jaaa:jaaa-alsa.desktop

/var/cache/fontconfig/0bd3dc0958fa2205aaaa8ebb13e2872b-le64.cache-6

/var/cache/fontconfig/bab58bb527bb656aaa9f116d68a48d89-le64.cache-6

aditya@ubuntu16:~/Desktop$
Aditya
  • 63

2 Answers2

3

By default find searches for your defined pattern within the all files and subdirectories you specified in the real time, so even after search has been started you can create a new file somewhere and if find didn't search that place yet it will find that file for you.

on the other hand, as you know locate uses a cached database to speed up this process, however it only caches some specific directories, you can use /etc/updatedb.conf file to define some directories to be excluded from being cached.

updating the data with sudo on a machine with multiple users is a security risk cause we will end up to a database which contains all users data and everyone can search for it.

For me sudo updatedb works fine, so check /etc/updatedb.conf to see if anything has been excluded in your machine.

If you want to create a database only for your user you can use:

updatedb -l 0  --output=/home/username/.mydb.db

then to search within it use:

locate -d ~/.mydb.db search-pattern

And use aliases to make it more convenience:

alias lupdatedb="updatedb -l 0  --output=/home/username/.mydb.db"
alias llocate="locate -d ~/.mydb.db"
xiota
  • 4,849
Ravexina
  • 55,668
  • 25
  • 164
  • 183
  • 1
    I checked /etc/updatedb.conf , here's what it shows: PRUNE_BIND_MOUNTS="yes"

    PRUNENAMES=".git .bzr .hg .svn"

    PRUNEPATHS="/tmp /var/spool /media /home/.ecryptfs /var/lib/schroot" PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre tmpfs usbfs udf fuse.glusterfs fuse.sshfs curlftpfs ecryptfs fusesmb devtmpfs"

    – Aditya Jun 03 '17 at 07:22
  • I'm the only user of my laptop, but still I tried it ( updatedb -l 0 --output=/home/username/.mydb.db )... here also unable to locate.... – Aditya Jun 03 '17 at 07:35
1

updatedb scans your whole filesystem, and as such, is a quite expensive operation. It is not meant to be run after every file creation, rather once a day or so to give you a rough idea about the system files.

As you can see it by running via sudo, it does not create or update a per user database, it creates or updates a common global one containing a manifest of your entire system. Your system has (or potentially might have) multiple users, each of them having read access to the database.

Hence, for privacy concerns, by default updatedb does not scan directories that are not readable by every user. It would be a privacy risk if you didn't grant read access to your home directory to others (as it is by default in most distributions), yet other users could get a list of your files via updatedb/locate.

For locating files under your home directory, or anywhere where reflecting recent changes is necessary, you should consider using find or other utilities that scan the filesystem just when you need it.

egmont
  • 8,225
  • 1
    Actually I'm the only user of my laptop. Except me only guest account is there, but I guess, everyone have that account. – Aditya Jun 03 '17 at 07:43
  • Thanks for sharing your concern (about multiple user), I hope my laptop isn't hacked!??? – Aditya Jun 03 '17 at 07:51
  • There's no way the updatedb program could know that you're the only user and you intend to keep it this way in the future. It would be stupid if it behaved differently based on whether at the moment of running you're the only user. I said your system might potentially have multiple users (Linux is a multi-user operating system, you know), and updatedb is prepared for its implications. – egmont Jun 03 '17 at 13:20