[LINUX] Find, locate command summary

Introduction

Most of the file operations are done with git grep, and I have neglected the basics, so I decided to look it up again.

find

As the name implies, find is a way to find files in a directory tree. The basic form is as follows.

$ find (Search start directory)(Search condition) (Action)

Search conditions

-name

$ find . -name file-1.txt -print
./file-1.txt

Search by name as the name suggests. However, if you use a wildcard (*?), Be sure to use single quotes or double quotes because it will be read by Bash variable expansion.

-type

$ find . -type d -print

As the name suggests, the search condition (-type) is also searched by type name. The d of-type dis the file type, and in the above case, it is an instruction to find only the directory from the current position.

It is also possible to use in combination as shown below. (-a is an AND search and can be omitted.)

$ find . -type f -a -name '*.txt' -print

Searchable file types

Specify File type
-type f regular file
-type d directory
-type l symbolic link

action

Currently all are displayed with -print, but when displayed with -ls, they are displayed in "ls" format.

find . -type f -a -name '*.txt' -ls
  671143      0 -rw-rw-r--   1 vagrant  vagrant         0 Apr 19 04:29 ./find/file-1.txt
  671215      0 -rw-rw-r--   1 vagrant  vagrant         0 Apr 19 04:29 ./find/file-3.txt
  671213      0 -rw-rw-r--   1 vagrant  vagrant         0 Apr 19 04:29 ./find/file-2.txt

locate Unlike find, locate is searched from the DB where the file path is stored, so it can be searched faster than find.

Installation

It's not included in Linux from the beginning, so you have to install it.

Check if it already exists

$ locate --version

If it is not included, install it.

Install on CentOS
$ sudo yum install mlocate
Install on Ubuntu
$ sudo apt-get install mlocate

Creating a database

$ updatedb

The database will be created by the above ʻupdatedb` command, and the database will be updated at regular intervals.

important point

Database updates are set to be updated once a day during installation. Therefore, it will be one day later that the file you just created will be caught in the search. Also, the file you just deleted will be caught in the search until one day later.

`locate is good for finding files some time after they were created (deleted). ``

Search by locate

$ locate bash

This will speed up all the files named bash.

Searchable file types

Options Effects
-i Ignore case
-b Search only file names (ignore directories)
-A and search (usually or search)

References

New Linux textbook

Recommended Posts

Find, locate command summary
Linux Command Summary
Docker command summary
[Linux] Basic command summary
[Infrastructure] While command summary
Image related command summary
[Learning memo] Django command summary
[Linux] User / group command summary
Jupyter Notebook Magic Command Personal Summary
Summary of "nl command Advent Calendar 2020"
[For competition professionals] Union Find Tree Summary
[Linux command summary] Command list [Must-see for beginners]
Migemo version of the: find command,: mfind