This paper is participating in the30 years of Linux”Topic essay activity

On Linux, the most common operation is to manipulate text. Common file operations display, search, sort, format conversion, data flow processing and so on. This article focuses on file search, analyzing the use of locate and find commands, and the principles of use and defects.

A, takeaway

The problem is simple. Linux, unlike Windows, is a file system, and files are organized in a very special way (inverted trees), which is inherently file friendly. We often use the ls command to display the contents of the file directory (file + directory). Its file directory is hierarchical, sometimes very deep, if we want to find a file, it is particularly troublesome to use the LS command alone.

ls [option -dir]
Copy the code

It’s hard to find your target through a bunch of files, so you need a search feature. Linux provides two common file search operations, Locate and find.

Two, locate location

Locate is a tool for finding files on a file system. It searches the pre-built local database of all files on the file system generated by the updatedb command. Note that the database must be updated because the latest files created in less than 24 hours are not updated to the database by default, and the database will be updated every 24 hours.

2.1 Application Practice

The syntax is as follows:

locate [OPTION]... PATTERN...
Copy the code

First create a new file locate. File under the /opt/test directory.

touch locate.file
Copy the code

Then use the locate command to locate the file location.

locate locate.file
Copy the code

The file was not found as scheduled, and an error was reported after execution.

locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
Copy the code

It is true that I can’t find the database file at the location of the error message, because this is the server I just installed, I have never used this command, I need to initialize the database file.

sudo updatedb
Copy the code

Update database with root user permission, execute locate command again, execute successfully, as shown in the figure, find.

Note: If the file is newly created and created within 24 hours, you also need to run the updatedb command.

2.1 Implementation Principles

Locate doesn’t scan the entire file system, it just looks in the file database. As shown, my current database file size is 5M.

For newly created files, it will not be indexed to the database in time, so we will use it at this timelocateIf you index this file, you won’t be able to find it.LinuxThe system generally updates the database file every day, so as long as you check the newly created file every 24 hours, you should be able to find the file in theory.

If you don’t want to wait that long, you can update the database as root and then query the file location.

2.3 Disadvantages

In my practice, there are some shortcomings:

  • Running this command will find all of them% file name %The files, many times, are not convenient to view.
  • As mentioned above, if we do not force the database update, we will not be able to query the newly created file in time, unless we wait 24 hours.
  • Because of the need to store an additional database file, when more and more files, the database file will become heavier and heavier, which will bring unnecessary storage burden.

Then we need a more powerful command, which is find.

Find STH

The find command is a more efficient search tool than locate. It is capable of recursively searching various files in any given path. Using the file command, we can search for files by name, owner, group, permission, type, size, time modification, date and various other criteria. The find command is very efficient, but this efficiency comes at the cost of time.

3.1 Application Practice

The syntax is as follows.

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...]  [expression]Copy the code

The H,-l, and -p options control the handling of symbolic links. If required, the option -h-L-p -d -o must appear before the first pathname. After the starting point list on the command line is the expression. This is a query specification that describes how we match files and how we process matched files. Syntax is very simple, is the **3W principle **, Where (Where to find, default is the current directory and its subdirectories), What (to find, can be a file name, size, permission, last access time), What (find What to do). The simplest use is to search for files by filename.

find / -name locate.file
Copy the code

The result is shown below.

Note that I choose to query the scope of the entire file system, if itself with root permission to query, there is no problem, but in theLinuxIn the system, many files and directories are owned by the root user or other users.

You may encounter it during your searchPermission deniedError, this time either to promote their own permissions, or to specify their own ownership scope of the directory to find the operation.

And for this type of lookup, we’re only going to find at mostlocate.fileFully matched file paths will not be foundlocate.file.1,otherlocate.fileAnd so on and so forth.

3.2 Implementation Principles

Unlike the locate command, find no longer looks for files in a file database. Instead, find finds the file location by traversing a specified disk. So obviously, if your disk is big enough and your files are large enough, the find command can take a long time to execute. Search for files directly in the file system, as shown in the figure. In this way, we can query the newly created file.

3.3 Advanced use

In addition to file name lookup, find can also find objects by size, file permissions, last accessed time, and restricted files/directories. Far more powerful, it can further manipulate the results of a lookup. Look it up by size

Find/opt-size +100M # Find/opt-size +100M # Find/opt-size +100M # Find/opt-size - 1KCopy the code

If there is no plus or minus sign, the file equal to the specified value is queried.

Lookup based on the last time the file was accessed

# find-name "*.yml" -atime-8 # find-name "*.yml" -atime-8Copy the code

– Atime -8 indicates that the atime is within 8 days. The minus sign indicates that the atime is less than.

Find only directories or files We create a test directory under the current directory and a test file under the test directory.

Find. -name "test" -type d # find. -name "test" -type d # find. -name "test" -type d # The result displays only the test file find. -name "test" -type dCopy the code
  • Type d: Searches only the directory type. D is the first letter of directory.
  • Type f: Searches only the file type. F is the first letter of file.

We’ve talked about how to find, but find also allows you to do more with the results of a search.

Delete the found file

# delete file find. -name new__file from the current directoryCopy the code

– Delete is risky and must be used with caution.

Invoking commands In the daily operation and maintenance process, we are likely to change the permission of the files queried. Let’s start by creating a file called echo.sh.

touch echo.sh
Copy the code

The permissions for the newly created file are shown in the figure below.

Then perform the find file and modify permissions operation.

find -name echo.sh -exec chmod 777 {} ;
Copy the code

  • -execThis is followed by a command, which is to perform an operation
  • chmod 777Indicates to modify the executable permission
  • {}Indicates that this operation is performed on all files searched
  • ;Is a mandatory command format

-exec is quite powerful, but without an acknowledgement operation, we can make it safer by replacing it with -ok, which will prompt an acknowledgement before executing the command for each queried file. There are many other operations to perform after searching for files, such as -print and so on, not to list one, interested to see the help manual.

3.4 Defects

One clear drawback of Find is that as the disk size increases and the number of files increases, the query time can be out of control.

Fourth, knowledge expansion

In addition to locate and find, another comparable command is which, which is special enough to search for a given filename in the directory defined in the $PATH environment variable. If a match is found, the full path to the executable is returned with the following syntax.

which [-a] filename ...
Copy the code

It is often used to find the path to an executable.