Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the file lookup command in the database

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to use the file search command in the database. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

File lookup:

Find eligible files on the file system

(file lookup is different from the grep we learned before. Grep filters text.)

File lookup implementation tool: locate,find

(the working rules and working modes between locate and find are not quite the same. The implementation mechanism of locate and the mechanism of locate search are based on the pre-built index to complete the search of files.

For example: locate passwd

When we execute the above command, we will find that although there is a file name that contains "passwd", the whole file name is not complete, just passwd,locate is a fuzzy match, even if the path name contains "passwd" locate will find it, and find it very fast. The search process of locate is not realized by traversing the entire file system, but by reading all the files on the current file system. Build a search database, and when locate searches for files, it makes a fuzzy match in the path according to the keywords given by the user.)

The locate implementation mechanism: / / means that if you want to use the locate command, you have to do the following

Relies on a pre-built index library: this index library can be implemented in two ways:

(1) automatic implementation of the system; (periodic task planning)

(2) manually update the database (updatedb)

With this lookup library, locate can query in this library.

The file ratio we found based on this index library is not necessarily accurate. This database is only the path to the file that is stored, not that the file actually exists, and this path is the path that has been updated at some point in the past.

For example, at some point after we updated the lookup database, we deleted or added some files, but the database was not updated in time, so the search operations carried out by locate in this database are not accurate.

For example:

Let's carry out the order:

[root@centos6~] # locate passwd / / if it doesn't work, let's update the data first

Updatedb

The following screenshot is displayed:

From the above display, we found that the above matching content, although all contain "passwd" but not only "passwd", so locate is a fuzzy match. Locate lookup is not to traverse the entire file to find, but to read out the previous database to build a lookup database.

Characteristics of locate work:

Search speed is fast.

Fuzzy search

Non-real-time search

(how the command runs:)

Format:

Locate [OPTION]... PATTERN...

Locate options:

-b: only matches the base name in the path; but it is still a fuzzy lookup

-c: only count the number of files that meet the criteria; (count the number of files only)

-r:BRE, based on basic regular expressions

Note: the index building process requires traversing the entire file system, which consumes a lot of resources; that is, execute the update command before you can use locate

Find

(find is very powerful. Compared with locate, there is no database in the execution of find. Instead, through real-time search, we should let our find work on some branch of our root file system.)

Find real-time lookup tool to complete file lookup by traversing the file system hierarchy under the specified starting path

Find operating characteristics:

The speed of searching is a little slower.

Precise search

Real-time search

For example:

[root@centos6~] # find / etc-name "passwd"

/ etc/pam.d/passwd

/ etc/passwd

[root@centos6~] #

Format:

Find [- H] [- L] [- P] [- D debugopts] [- Olevel] [path...] [expression]

Usage:

Find [OPTIONS] [find start path] [find condition] [process Action]

Find start path: specify the specific search target start path, default to the current directory

Search criteria: the specified search criteria can be carried out according to file name, size, type, dependency, permissions, etc. If you do not specify search conditions, the default is to find all the files under the specified path

Processing actions: actions taken on files that meet the search criteria, such as deletion, are output to standard output by default

For example:

[root@centos6~] # find. / / No search criteria are specified, look for all files in the current directory. The table shows that all the files in the current directory match

Search criteria (tests): finding conditions in find is the key point, in a directory-level mode, to read the file's

Expressions: options and test

Test: the result is usually Boolean ("true", "false")

Look up (or test) by file name:

-name "pattern"

-iname "pattern": indicates that the file name is case-insensitive

The above pattern supports golb-style wildcards; not regular expressions

Patterns are usually enclosed in quotation marks

*, [], [^]

If we want to use regular expressions for lookups in the find lookup mode, we will use an option "- regex", but this option is based on the entire pathname, which is equivalent to locate, not exact lookups.

-regex pattern: finds files based on regular expression patterns, matching the entire path, not the base name

The command regex is rarely used.

For example:

Example 1:

[root@centos6~] # find / etc-name "passwd" / / means to find a file named "passwd" precisely under the directory "/ etc".

/ etc/pam.d/passwd

/ etc/passwd

[root@centos6~] #

Or:

[root@centos6~] # find / etc-iname "passwd" / / iname is not case sensitive

/ etc/pam.d/passwd

/ etc/passwd

[root@centos6~] #

Find is a precise lookup filename, and if you want to find a filename that contains "passwd" and ends with passwd, the pattern says "* passwd". If it starts with passwd, it says "passwd*", and the asterisk is a glob-style wildcard. If you want to find a character after passwd, say: passwd? Add a question mark after that. A question mark can indicate a special symbol. If you are looking for a letter or number after a passwd instead of a special symbol, then we cannot use a question mark. It should say: "passwd [[: alnum:]]"

So the wildcards used in find can use the glob style: *? [] [^]

Add a character to passwd after you

Search according to the subordinate relationship of the file: (belong to the master group)

-user USERNAME: find all the conditions for the user specified by the owner

-groupGRPNAME: find all files that belong to the specified group

For example:

[root@centos6~] # find / tmp-user dong

/ tmp/dongdongfile

[root@centos6~] #

-uid UID: find all the files of the UID specified by the owner

-gid GID: find all files of the GID specified by the group

For example:

[root@centos6~] # find / tmp-uid 508

/ tmp/

[root@centos6~] #

When we create a file or directory with a user, and then we change the user and the user's home directory, we look at the files or directories previously created by this user. The original information of these files or directories shows that the master group is the uid number, and we can also look it up according to this number. Or search according to "nouser".

These files without owners and groups are very dangerous on the file system, because in the future we set up a user, and that user happens to set up an ID number, then the file naturally belongs to this user, and this file adds and uploads some private information of the user, so we should regularly find out which files on the current system do not have owners or groups. We find him a new owner or group, of course, a new group. The owner is generally root, because only root has the permission to change the master group of the file. )

-nouser: find files without owners

-nogroup: find files that do not belong to a group

Look up according to the type of file:

-type TYPE

F: normal file, displayed as "-" in the ls command

D: catalog file

L: symbolic link file

B: block device file

C: character device file

P: pipe fil

S: socket file

For example:

[root@centos6~] # find / dev-type b-ls / / find the block device file under the / dev directory, followed by "- ls" is the processing action, the default processing action of find is "- print" is displayed on the screen, if you use "- ls", it is equivalent to executing the "ls-l" command, executing the ls-l command for each file you will find. Show details of the file

[root@centos6~] # find / dev-type l-ls / / check what symbolic links are under the directory / dev, and what the links on them point to.

Combined testing:

To achieve a more complex search, it is generally through a combination of different conditions. )

When the find command looks up, if multiple conditions are combined, the and operation is performed by default, even if the join is made without a logical operator.

And:-a means that both conditions are met before they are displayed.

Or:-o

Non:-not.not!

For example:

[root@centos6~] # find / dev-nouser-type b-ls

Equivalent to: find / dev-nouser-a-type b-ls

Combined testing:

And:-a, default combinational logic

Or:-o (review, in the regular expression option in grep,-o indicates that only matching characters are displayed)

Non:-not or!

For example:

We look for files that are not ordinary files in the / tmp directory

Solution:

Find / tmp-not-type f-ls

Exercise:

1. Find out the files whose main part of the / tmp directory is not root and whose filename contains the fstab string

(find out all the files under the / tmp directory that are mainly non-root

Solution: find / tmp-not-user root-ls)

Solution:

Find / tmp-user root-a-name "* fstab*"

two。 Find the files in the / tmp directory that do not contain the fstab string in the file name

Solution:

Find / tmp-name "* [^ fstab] *"

Or:

Find / tmp-not-iname "* fstab*"

3. Find files whose / tmp directory is mainly non-root and whose filename does not contain a fstab string.

Solution:

Find / tmp-nouser root-a-name "* [^ fstab] *"

Or:

Find / tmp-not-user root-a-not-iname "* fstab*"

Or:

Find / tmp-not\ (- user root-o-not-iname\) "* fstab*"

(1) find / tmp-not-user root-a-not-iname "* fstab*"

Equivalent to

(2) find / tmp-not\ (- user root-o-not-iname\) "* fstab*"

The above two queries use de Morgan's law:

! a-a! Bobby! (a-o B)

! a muro! beer! (a-a B)

Look up according to the size of the file:

-size [+ | -] # UNIT

Parse: #: represents a number, UNIT: represents a unit

Commonly used units: K M G

Explanation: when searching according to the size, it is marked with a plus or minus sign, which means that the search is larger than or less than the specified size. If you do not add or subtract the sign, it represents an exact search.

# UNIT: (#-1 UNIT #]

Although it is an accurate search, it refers to a semi-open and semi-closed interval.

For example: when our search is 100K, the actual size of the file is "99K"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report