Thứ Ba, 7 tháng 4, 2015

[CLI] Locate _ just find files by name.

This tutorial is done on:
thanhnguyen@thanhnguyen:~$ lsb_release -ir
Distributor ID:    Ubuntu
Release:    12.04
What is locate.
NAME
locate - find files by name
So clearly, locate is the command line that can help you find files in your system base on the file name.

SYNOPSIS
locate [OPTION]... PATTERN...
How does locate do?
In your system, there are databases that contain the information of all files.
The first, you need update these databases with:
updatedb
thanhnguyen@thanhnguyen:~$ updatedb
updatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db'
thanhnguyen@thanhnguyen:~$ sudo updatedb
thanhnguyen@thanhnguyen:~$
This command will read / to look for information of files and archive them in the databases.
The locate will read these databases and list filename of files that matching at least one of the PATTERNs.

By default, locate just check filenames in the database but does not sure that the these files are really exist or not. Moreover, it never report the files created after the most recent update of databases. It is main reason that why we always have to update databases before.

Note: database is a binary file (non-text file). But you can read it with:
strings dbpath | less
For example:
/var/lib/mlocate/mlocate.db
.........
SM<L
/etc/depmod.d
ubuntu.conf
SM<L
/etc/dhcp
dhclient-enter-hooks.d
dhclient-exit-hooks.d
dhclient.conf
/etc/dhcp/dhclient-enter-hooks.d
avahi-autoipd
debug
resolvconf
samba
/etc/dhcp/dhclient-exit-hooks.d
....................
The basic syntax of locate is:
locate filename
When used without any options, locate display every absolute pathname of files that user have access permission.

For example, this command will list absolute path of all files named file1 and all directories named dir1:
locate file1 dir1
For example:
thanhnguyen@thanhnguyen:~$ locate des.csv github
/home/thanhnguyen/des.csv
/home/thanhnguyen/github
/home/thanhnguyen/github/pho
/home/thanhnguyen/github/pypitop
/home/thanhnguyen/github/python
/home/thanhnguyen/github/saltstack
/home/thanhnguyen/github/flaskr/achemy.sql
/home/thanhnguyen/github/flaskr/flaskr.py

2 nhận xét:

  1. > lsb_release -ir

    too verbose, lsb_release --help too see all of available options

    > locale
    why don't use "find"? when to use locale instead of find?

    Trả lờiXóa
    Trả lời
    1. Can be use:
      thanhnguyen@thanhnguyen:~/Downloads$ lsb_release -d
      Description: Ubuntu 12.04.5 LTS

      Xóa