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

Linux file search tools-find, locate

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

Share

Shulou(Shulou.com)06/01 Report--

File lookup

Find eligible files on the file system

File lookup: locate, find

Non-real-time lookup (database lookup): locate

Real-time search: find

1. Non-real-time lookup (database lookup): locate

Query the pre-built file index database on the system (search based on this database)

Database: / var/lib/mlocate/mlocate.db

Rely on pre-built indexes

The index is built automatically when the system is idle (periodic tasks).

Administrator manually updates the database: updatedb

(the newly created files need to be updated in the database before they can be searched, so it is suitable to find more stable ones.

Unchanged system internal configuration file)

The index building process requires traversing the entire root file system, which consumes a lot of resources.

Job characteristics:

Search speed is fast.

Fuzzy search

Non-real-time search

Search for the full path of the file, not just the file name

It is possible to search only directories where the user has read and execute permissions

Locate command

Locate KEYWORD

Common options

-I perform case-sensitive search

-n N enumerates only the first N matching items

-r search using regular expressions

Eg:

Locate foo

Search for files with "foo" in their name or path

Locate-r'\ .Foo $'

Use Regex to search for files that end with ".foo"

two。 Real-time file lookup tool: find

Find

Real-time search tool to complete file search by traversing the specified path

Job characteristics:

The speed of searching is a little slower.

Precise search

Real-time search

It is possible to search only directories where the user has read and execute permissions

Automatic recursive search when searching a directory

Syntax:

Find [OPTION]... [search path] [search condition] [processing action]

Find path: specify a specific target path; default is the current directory

Search criteria: the specified search criteria, which can be file name, size, type,

Permissions and other standards; the default is to find all files under the specified path

Processing action: operate on files that meet the criteria, and output to the screen by default

(pay attention to the format in the grammar and use a syntax format that is consistent with regular expressions)

Search condition

Look up by file name and inode:

-name "file name" (precise search): support the use of glob

*,?, [], [^] support wildcard-> Note: double quotation marks are required.

-iname "file name": case-insensitive

-inum n search by inode number

-samefile name files with the same inode number

-links n Files with n links

-regex "PATTERN": matches the entire file path word with PATTERN

String, not just file name

Look up according to the owner and group:

-user USERNAME: find files whose owner is the specified user (UID)

(note: the system searches by UID)

-group GRPNAME: find files that belong to the specified group (GID)

-uid UserID: find the file whose owner is the specified UID number

-gid GroupID: look for files with the specified GID number in the belonging group

-nouser: find files without owners

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

(multiple conditions can be directly combined to represent the relationship, that is,-a,-a can be omitted)

Eg find /-nouser-nogroup)

Look up by file type:

-type TYPE:

F: ordinary files

D: catalog file

L: symbolic link file

S: socket file

B: block device file

C: character device file

P: pipe fil

Combination conditions:

And:-a

Or:-o

Non:-not!

Find /-name "abc.sh"-a-name "bsd"

Find-not-name "adb"

De Morgan's law:

(non-P) or (non-Q) = non (P and Q)

(non-P) and (non-Q) = non (P or Q)

Example:

! a-a! B =! (a-o B)

! a-o! B =! (a-a-B)

twelve

Find example

Find-name snow.png

Search for a file named snow.png

Find-iname snow.png

Search regardless of case for names snow.png, Snow.png,

Files for SNOW.PNG, etc.

Find /-name "* .txt"

Find / var-name "* log*"

Find-user joe-group joe

Search for files owned by user joe and group joe

Find-user joe-not-group joe

Find-user joe-o-user jane

Find-not\ (- user joe-o-user jane\)

Find /-user joe-o-uid 500

Find the files in the / tmp directory where the owner is not root and the file name does not start with f

Find / tmp\ (- not-user root-a-not-name 'fags'\)-ls

Find / tmp-not\ (- user root-o-name 'favored'\)-ls

Exclude directories-prune (do not search for this directory)

Example: find all files with the .conf suffix under / etc/ except the / etc/sane.d directory

Find / etc-path'/ etc/sane.d'-a-prune-o-name "* .conf"-print

(ignore the / root/bin directory and do not search for it,)

Look up according to the file size:

-size [+ | -] # UNIT (imprecise size)

Common units: K, M, G

# UNIT: #-1, #]

For example, 6k means (5kpjm 6k)

-# UNIT: [0memorie muri 1]

For example,-6k means [05k]

+ # UNIT: (# ∞)

For example, + 6k means (6k, ∞)

According to the timestamp:

In terms of "days"

-atime [+ | -] #

#: [#, # + 1) [7Jing 8)

+ #: [# + 1, ∞] [0JE7)

-#: [0meme #) [8pr + infinity)

-mtime

-ctime

In minutes:

-amin

-mmin

-cmin

Find based on permissions:

-perm [/ | -] MODE

MODE: exact permission matching

/ MODE: any kind of object can have only one permission, or relationship, starting with centos7

Elimination

-MODE: each type of object must have the specified permissions at the same time.

0 indicates that the permissions in this location are not concerned about

Find-perm 755 will match files whose permission pattern happens to be 755 (exact match)

Find-perm + 222will match as long as anyone has write permission

Find-perm-222will match only if everyone has write permission.

Find-perm-002will match only if someone else (other) has write permission.

(+: or relationship

-: and relationship)

Find-perm-020-> only files with write permissions on the group match the processing action

-print: default processing action, which is displayed to the screen; (output is available by default)

-ls: similar to the "ls-l" command on the found file

Note whether the previous command needs to be enclosed

-delete: delete the found file

-fls file: the long format information of all found files is saved to the specified file

-ok COMMAND {}\; execute for each file found by the

The command specified by COMMAND; ({} represents the searched file)

The user is asked to confirm interactively before executing the command for each file

-exec COMMAND {}\; execute (risky) for each file found

Commands specified by COMMAND

{}: used to reference the found file name itself

When find passes the found file to the command specified later, it finds all matches

The conditional file is passed to the following command at one time

Some commands cannot accept too many parameters, and the execution of the command may fail.

Noodles can avoid this problem.

Find | xargs COMMAND

Processing action

Xargs-0 (zero) line wrap

Find example

Find-name "* .conf"-exec cp {}. Org\

Back up the configuration file and add the .orig extension

Find / tmp-ctime + 3-user joe-ok rm {}\

Prompt to delete temporary files of joe that have existed for more than 3 days

Find ~-perm-002-exec chmod Omurw {}\

Look in your home directory for files that can be written by other users

Find / data-type f-perm 644-name "* .sh"-exec

Chmod 755 {}\

Find / home-type d-ls

Practice

1. Find all the files under the / var directory whose master is root and whose group is mail.

Find / var-user root-group mail

2. Find all the files in the / var directory that do not belong to root, lp or gdm

Find / var-not\ (- user root-o-user lp-o-user gdm\)

3. Find files whose contents have been modified in the last week in the / var directory, and the owner is neither root nor postfix.

Find / var-not\ (- user root-o-user postfix\)-mtime-7

4. Find files that have no owners or groups on the current system and have been accessed in the most recent week

Find\ (- nouser-o-group\)-atime-7

5. Find all files in the / etc directory that are larger than 1m and whose types are ordinary files

Find / etc-size + 1m-type f

6. Find files in the / etc directory that all users do not have write permission to

Find / etc-not-perm / 222

8. Find / etc/init.d directory, all users have execution permissions, and other

Files for which the user has write permission

Find / etc/init.d-perm-113

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