Linux commands 4-find, locate

PIDA 2021-03-01 15:27:55
linux commands 4-find locate


Linux command 4-find、locate

  • find
  • locate

find- Find files

Command profile

find The command is used to find a file in a specified directory , The command format is :

find [ Specify the search directory ] [ Find the rules ] [ Actions performed after finding ]
  1. Find the path : Specify the specific target path , Default to current directory
  2. Search for conditions : Specified search criteria , It could be a file name 、 size 、 type 、 Authority and other standards ; The default is to find all the files in the specified path
  3. Executive action : Operate on qualified files , Default output to screen
find pathname [ Options ] [-print] [-exec -ok command] {} \;

The main parameters are :

Parameters

explain

pathname

The file path you are looking for ; Use point . To represent the current directory , use / To represent the system root

-print

Output matching files to standard output

-exec

find Command to execute the shell command . The form of the corresponding command is command{} ; Be careful {} and \; Space between

-ok

and -exec similar , Just ask the user before the operation

Description of the main options :

Options

explain

-name

-name filename: Search by name

-perm

According to the execution authority to find

-user

-user username: Find... By file properties

-group groupname

-group groupname: Find... By group

-mtime -n、+n

-n、+n: According to the file change time ,-n Express n Within days ,+n refer to n Before the day

-atime

According to the visit time

-ctime

Check according to the creation time ;-n、+n:-n refer to n Within days ,+n refer to n Before the day

-newer f1 !f2

Find change time ratio f1 new , But compared to f2 Old documents

-size n[c]

The search length is n block (n Bytes ) The file of

-type b/d/c/p/l/f

Find block devices 、 Catalog 、 Character device 、 The Conduit 、 A symbolic link 、 Ordinary documents

General search conditions

find . # List all files and folders in the current directory and subdirectories
find /root -name "*.txt" # lookup root Directory to txt Final document
find /root -iname "*.txt" # ditto , But ignore case
find . -name dog* -o -name cat* # Search in the current directory to dog perhaps cat Start file
# find . \( -name dog* -o -name cat* \)
find /home ! -name "*.md" # find home It's not in the directory md Final document
find . -type [fldcbs] # [ Type parameter : f Ordinary documents l A symbolic link d Catalog c Character device b Block device s Socket ]
find -name hello* # lookup hello Opening file
find -name hello* fprint file # Find in current directory hello Opening file , And output to the file In file
find . -type f -exec ls -l {} \; # Find all normal files in the current directory
find . -type f -name ".md" -delete # find md All the files at the end , And delete 

Find... By file path

find /root -path "*local*"

Find... By filename

find . -name "*.md" # Looking to md Final document
find . -iname "*.md" # Case insensitive 

Pay attention to the use of wildcards :

find . -name "hello?" # Match any single character
find . -name "[ab].sh" # Match a character in parenthesis
find . -name "*hello*" # Match any one or more characters 

Search by depth

find . -maxdepth 3 -type f # The maximum search down limit is 3
find . -mindepth 2 -type f # Search all files at least two directories away from the current directory 

according to uid or gid Search for

find . -uid 700 # lookup uid yes 700 The file of
find . gid 1000 # lookup gid yes 1000 The file of 

Find... By time

Linux Every file in a file system file has 3 A time stamp :

  • Access time (-atime/ God ,-amin/ minute ): Last time user visited .
  • Modification time (-mtime/ God ,-mmin/ minute ): Last modification time of document .
  • Time of change (-ctime/ God ,-cmin/ minute ): File data elements ( Such as authority ) Last modification time .

Use stat Command to view the time information of a file :

stat /etc/passwd

Examples of looking up files over time :

find . -type f -atime -7 # Search for recent 7 Days of files that have been accessed
find . -type f -atime 7 # The search happens to be 7 All the files that were accessed a day ago
find . -type f -atime +7 # Search more than 7 Days of files being accessed
find . -type f -amin +10 # exceed 10 Files accessed in minutes
find . -type f -newer hello.txt # Find out the ratio hello.txt All documents with longer modification time 

Find by size

Linux File size unit in the system :

Symbol

size

b

block ,512 byte

c

byte

w

word (2 Bytes )

k

kilobytes

M

Megabytes

G

Gigabyte

find . -type f -size +10k # type The specified type is f( file ), Greater than 10k
find . -type f -size 10k # type The specified type is f( file ), Just for 10k
find . -type f -size -10k # type The specified type is f( file ), Less than 10k
find . -empty # List the length as 0 The file of 

Look up according to the logical judgment condition

according to 3 There are two kinds of logical judgment conditions to find :

  • -a: Connecting two different conditions , At the same time meet the conditions
  • -o: Connecting two different conditions , Satisfy one of them
  • -not: Invert the condition
 And :-a
or :-o
Not :-not, !
find . -name "*.md" -a -user root # and
find /tmp -not -user root # tmp Under the table of contents , The user is not root

According to the user and user permissions to find

Linux File with 3 Species jurisdiction : read 、 Write 、 Executable

jurisdiction

Impact on documents

Impact on catalog

Corresponding size

r( Read )

Can read the contents of the file

You can list the contents of the catalog

4

w( write in )

The contents of the document can be modified

Delete content can be created in the directory

2

x( perform )

Can be executed as a command

Access to directory content

1

The directory must have x jurisdiction , Otherwise, you can't view its contents

Linux The permissions in are assigned to 3 Users :user、group、other

  • U:User
  • G:Group
  • O:Other

The permissions for each file are based on UGO Set it up ; Authority in groups of three (rwx), Three roles need to be authorized at the same time (UGO);

Each file has a user and group to which it belongs , Corresponding UGO, It does not belong to the user or group to which the file belongs O To express .

drwxr-xr-x 5 peter staff 160 3 5 2020 good-good-study

Explain the above directory properties :

  • d: Presentation directory , If the same location is - Is represented as a normal file
  • rwxr-xr-x: Is assigned to 3 There are two kinds of user rights , So it is ugo; Corresponding to :user Authority rwx(4+2+1=7),group Authority r-x(4+0+1=5),other Authority r-x(4+0+1=5)
  • 5: Represents the number of links to the folder , It can be understood as the number of directory subdirectories
  • peter: Represents the user name ;staff: Express peter The name of the group to which it belongs ; Other characters are not displayed by default
  • 160: Represents the number of bytes occupied by the folder
  • 3 5 2020: File creation time
  • good-good_study: Directory name or file name

Understand the above Linux Middle user ( Group ) And user rights , You can find related files :

find . -type f -perm 777 # The search permission is 777 The file of
find . -type f -name ".md" ! -perm 644 # Find all not in the current directory 644 Of md file
find . -type f -user peter # lookup peter All the files you have
find . -type f -group staff # Find user groups staff All the documents of 

Symbol {} Use

ind Command to execute the shell command . The form of the corresponding command is ’command’ { } ;, Be careful { } and \; Space between .

  • Through the first find The command finds out
  • Re pass -exec and {} \; Output the results
find . -type f -exec ls -l {} \; # The front is to find out the file , The following is the output file to the terminal ()
find ./ -mtime -1 -type f -exec ls -l {} \; # Find the currently modified file ( Don't ask )
find ./ -mtime -1 -type f -ok ls -l {} \; # ok Will ask if output 

summary :

  • -exec: Direct action , Don't ask
  • -ok: Ask before performing the action

locate

Command profile

locate It can quickly search whether there are specified files in the file system .locate Because it's just to retrieve the index database , So in comparison to find Full search is much faster .

locate Through update The program will first create an index database of all files and catalog data in the hard disk , stay perform loacte Find the index directly , The query speed will be faster

locate The command is not to find the latest data , If you want to query, you need to update the database first :

updatedb # Update the database 

Command format

locate The general format of the command is :

locate [option] [pattern]

Command options

Options

explain

-A, --all

Show only items that match all patterns

-b, --basename

Match only the base name of the pathname

-c, --count

Show only the number of items found

-d, --database DBPATH

Use DBPATH, Instead of default data ( namely :/var/lib/mlocate/mlocate.db)

-e, --existing

Show only the currently existing file entries

-L, --follow

Follow the spread of symbolic links when files exist ( Default )

-h, --help

Display help information

-i, --ignore-case

Ignore case differences

-l, --limit, -n LIMIT

Limit to LIMIT Output of the project ( or Count )

-m, --mmap

Be ignored , For backward compatibility

-P, --nofollow, -H

Don't follow the extended symlinks when checking files

-0, --null

Output with NUL Separate items

-S, --statistics

Don't search for items , Show statistics about each used database

-q, --quiet

Do not report error messages about reading the database

-r, --regexp REGEXP

Search for basic regular expressions REGEXP Not the pattern

--regex

Pattern is an extended regular expression

-s, --stdio

Be ignored , For backward compatibility

-V, --version

Display version information

-w, --wholename

Match the full pathname ( Default )

Use cases

$ locate /home/hello # Find... In the specified directory hello Opening file
$ locate hello # Inquire about hello Opening file
$ locate -n 10 python # Limit output 10 And python Related items
$ locate -i -n 5 passwd # Case insensitive
$ locate -r ^/var/lib/rpm # Using regular expressions :^ The symbol for the beginning
$ locate -r zip$ # With zip Final document 

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[PIDA]所创,转载请带上原文链接,感谢
https://javamana.com/2021/03/20210301120922332i.html

  1. K8s命令篇-Kubernetes工作实用命令集结号
  2. K8s command chapter - kubernetes working practical command collection number
  3. Hbase(二)Hbase常用操作
  4. San Wai doesn't even know how to change MySQL table DDL
  5. HTTP series - detailed explanation of message structure
  6. MySQL 常见存储引擎的区别
  7. Flink 1.12 集成 Hive 之快速入门
  8. Java - BlockingQueue学习
  9. MySQL 存储引擎 MyISAM 与 InnoDB 区别
  10. Linux three swordsmen (1) - grep
  11. 《算法竞赛入门经典——训练指南》实用数据结构
  12. Practical data structure of "Introduction to algorithm competition classic training guide"
  13. Hive的数据压缩介绍及使用
  14. Redis:23---info命令总结
  15. Redis:23---info Command summary
  16. Quick start kubernetes (k8s) - resource list
  17. Java方法签名
  18. Linux命令4-find、locate
  19. linuxNote1_用户管理
  20. Brief description of Tencent cloud redis console parameters
  21. Java中正则表达式
  22. MySQL 事务隔离级别和多版本并发控制MVCC
  23. 再见,MySQL!性能被 MariaDB 吊打 ?
  24. Regular expression in Java
  25. Linux text processing
  26. Goodbye, MySQL! Performance is being beaten by MariaDB?
  27. Don't download JDK any more: elasticsearch's first big hole in the domestic arm environment
  28. Introduction to MySQL architecture
  29. Linux foundation day02
  30. [javap6 outline] Spring Court: how to select the technology of fuse framework? Sentinel or hystrix?
  31. Chinese user guide of parallel SSH under Linux
  32. Top of BPF: insight into Linux system and application performance
  33. View the JDK version of class
  34. JQuery level selector (descendant, descendant selector)
  35. Interesting JavaScript - precision loss and implicit type conversion
  36. Database: built in functions of MySQL