Linux learning (2) - File Search Command

table of Contents

Linux command in the File Search

File Search command: locate

Command search command: whereis and which

File Search command: find

String Search command: grep


 

Video tutorial lesson but learning Mu: https://www.imooc.com/video/4018

This section summarizes the study and made notes.

 

Linux command in the File Search

File Search command: locate

Search speed is very fast, and the find command relatively resource-intensive, will start at the root of all the search again, the locate command can only search for file names .

locate 文件名
	在后台数据库中按文件名搜索,搜索速度更快。
	locate命令是在/var/lib/mlocate中搜索的后台数据库,大概每天数据库更新一次,如果没有更新今天的,则无法搜索到,需要等到它更新完成。
	优点是快,但缺点是只能按照文件名搜索。
updatedb
	强制更新locate数据库。

/etc/updatedb.conf configuration file, locate the search command by this rule, you can view the contents by vi /etc/updatedb.conf command.

  • PRUNE_BIND_MOUNTS = "yes" open search restrictions

  • When PRUNEFS = search does not search the file system

  • When PRUNENAMES = search, file type search

  • When PRUNEPATHS = search path is not searched

Example 1: locate five command , search the directory named five

Example 2: locate install.log command , search for the file named install.log.

Example 3: updatedb command , you can see to create a temporary file named test.log, but can not use the search to locate the command, because the database is not updated, so use updatedb command to update the database, will be able to successfully search results.

 

Command search command: whereis and which

whereis 命令名
	搜索某个系统命令所在路径及帮助文档所在位置,不能搜索文件。[whoami]查看属于谁。[whatis 命令名]查看该命令是做什么的。[whereis -b 命令名][whereis -m 命令名]
	选项:
		-b 只查找可执行文件
		-m 只查找帮助文件
which 命令名
	能看到命令的所在路径,及命令的别名

Example 1: whereis ls command , ls command to find the path where the path and help documentation.

Example 2: whereis -b ls command to find the directory where the class file execute ls command.

Example 3: which ls command , ls command to view the path and alias.

 

File Search command: find

grammar:

find [搜索范围] [搜索条件]
	该命令用来搜索文件。
	如[find / -name install.log]命令,但使用该命令时应该避免大范围搜索,因为会非常耗费系统资源。find是在系统当中搜索符合条件的文件名,如果需要匹配,则使用通配符匹配,通配符完全匹配。

Linux in the wildcard

  • An asterisk (*): Matches any content

  • Question mark (?): Matches any one character

  • Square brackets ([]): match any one character in brackets

other

  • [Find / root -iname filename] is not case sensitive

  • [Find / root -user owner name] in accordance with the owner to search

  • [Find / root -nouser] used to search for files that have no owner, usually junk files, but there are special circumstances.

  • [Find / root -mtime +10] find files modified 10 days ago, in which the "/ root" refers to the search.

    • -1010 days to modify the file

    • 1010 Tian day modified files

    • +10 10 days before the modified file

    • atime file access time

    • ctime change file attributes

    • mtime modify the contents of the file

  • [Find / root -size 25k] to find the file size is 25KB file, use the Find trillion M, where "/ root" refers to the search.

    • -25k files smaller than 25KB of

    • 25k equal to the file of 25KB

    • + 25k files larger than 25KB of

  • [Find / root -inum 26244] i node is a lookup file 262 422, wherein "/ root" refers to a search range.

  • [Find / etc -size + 20k -a -size -50k] Find / etc files larger than / greater than 50KB and 20KB of directory

    • -a and, logic, two conditions are satisfied

    • -o or, logic, or, to a two conditions are satisfied

  • [Find / etc -size + 20k -a -size -50k -exec ls -lh {} \;] Find / ect / directory, files larger than larger than 20KB and 50KB and display details. [-Exec command {} \;] This is the standard format, is the result of the processing of the first command.

Example 1: find / root -name install.log command , look for a file named install.log in the root directory, where "/ root" directory to look for, "- name" means the search by file name, "install. log "file to be looking for.

Example 2: find / root -user root command , find the owner of the file in the root directory is the root of the file.

Example 3: find / root -mtime +10 command , to find the last modified file in the root directory is 10 days before the

You can execute exec command to view a specific date

Example 4: find / root -size -10k command , find files root directory of the file size is less than 10k

Example 5: find / root -size + 10k -a -size -60k command , it finds greater than less than 60k 10k file in the root directory

Specific view the file size:

Example 6: find / etc -size + 20k -a -size -50k -exec ls -lh {} \; command , to find / ect / directory, files larger than larger than 20KB and 50KB and display details.

 

String Search command: grep

grep [选项] 字符串 文件名
	在文件当中匹配符合条件的字符串
	选项:
		-i 忽略大小写
		-v 排除指定字符串

The difference between command and find the grep command

  • find command: Searching for qualifying system in which the file name, if you need to match, using wildcard matching, the wildcard is the exact match.

  • grep command: Searching for qualifying string in the file. If you need to match, regular expression matching, regular expressions containing matches.

Example 1: grep "ab" install.log command to find content "ab" in the install.log file.

Example 2: grep -v "ab" install.log command , search for all the strings in the file install.log except "ab" string.

Published 500 original articles · won praise 77 · views 160 000 +

Guess you like

Origin blog.csdn.net/cnds123321/article/details/104941306