File Search Command

Source: Internet
Author: User
Tags character classes control characters

Learning Syllabus
    • 1. File Search Command Locate
    • 2. Command Search command Whereis
    • 3. Command Search Command which
    • 3. File Search command find (a bit of resource-intensive)
    • 4. String Search command grep
    • 5. The difference between the Find command and the grep command
    • 6. Type command
/etc/updatedb.conf configuration file
" Yes "    #开启搜索限制    =                   #搜索时, do not search for   system files =                #搜索时, file types      not searched =                #搜索时, Paths that do not search
1. Locate command format

The locate command is actually another way of writing "Find-name", but much faster than the latter because it does not search for a specific directory, but instead searches for a database (/var/lib/locatedb), which contains all the local file information * *.
The Linux system automatically creates this database and updates it automatically once a day, so you can't find the latest changed files using the Locate command. To avoid this situation, you can manually update the database by using <span style= the color:red >updatedb</span> command before using locate.

Examples of use of the Locate command:

Locate /etc/sh  #搜索etc目录下所有以sh开头的文件.  Locate ~/m      #搜索用户主目录下, all files beginning with M.  Locate -I ~/m   #搜索用户主目录下, all files beginning with M and ignoring case.
2. Command Whereis for all commands


The Whereis command can only be used for search of program names , and only three files (specified by parameters) are searched. If the argument is omitted, all information is returned.

Whereis Command name # Search for the path to the command and the location of the Help document options:-B find only executable files -M only findHelp file -s only find source files


Example of use of the Whereis command: whereis grep

3. Command which for all commands

The purpose of the which command is to search for the location of a system command in the path specified by the path variable, and return the first search result. That is, with the which command, you can see whether a system command exists, and the command that executes exactly which location.

which filename Search Command path and alias

Example of use of the which command: $ which grep

PATH environment variable

PATH environment variable: Defines the paths of the system search command
Open command line: Enter echo $PATH

4. Find

Find is the most common and powerful look-up command you can use to find any file you're looking for.
Find uses the following format:

Find < Specify directory > < specify conditions > < specify action >-< Specify directory >: the directory to be searched and all its subdirectories.  The current directory is assumed by default.  -< Specify criteria >: The characteristics of the file to be searched. -< specify actions;: Specific processing of search results.


If no parameters are added, find searches the current directory and its subdirectories by default, and does not filter any results (that is, all files are returned) and displays them all on the screen.

Example:

Find " my* " #搜索当前目录 (with subdirectories, same as below), all files with the file name beginning with my.  Find"my*" -ls#搜索当前目录中, all files with the file name beginning with my, and display their details.  Find . -type f-mmin-#搜索当前目录中, all the normal files that have been updated in the last 10 minutes. If you do not add the-type f parameter, search for normal files + special files + directories.

5 String Search command grep

The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users. Powerful Text Search tool that can use regular expressions to search for text and print matching lines.
grep can be used for Shell scripting, because grep describes the status of the search by returning a status value, or 0 if the template search succeeds, or 1 if the search is unsuccessful, or 2 if the searched file does not exist. We can use these return values to do some automated text processing work.

Command format: grep [option] pattern file

Command function:
A specific character used for filtering/searching. The use of regular expressions can be used in conjunction with a variety of commands, the use of very flexible.

Command parameters:

-A--text #不要忽略二进制的数据. -a< Show rows >--after-context=< show rows >#除了显示符合范本样式的那一列之外, and displays the contents after the line. -B--byte-offset #在显示符合样式的那一行之前, indicating the number of the first character of the line. -b< Show rows >--before-context=< show rows >#除了显示符合样式的那一行之外 and displays the contents before the line. -C--Count #计算符合样式的列数. -c< Show rows >--context=< show rows > or-< show rows >#除了显示符合样式的那一行之外, and displays the contents of the line before it. -D < Actions >--directories=< Actions >#当指定要查找的是目录而非文件时, this parameter must be used, otherwise the grep command returns information and stops the action. -e < template styles >--regexp=< template styles >#指定字符串做为查找文件内容的样式. -E--extended-regexp #将样式为延伸的普通表示法来使用. -F < Rules file >--file=< Rule Files >#指定规则文件, whose contents contain one or more rule styles, so that grep finds the contents of the file that match the rule condition, in the form of a rule style per line. -F--fixed-regexp #将样式视为固定字符串的列表. -G--basic-regexp #将样式视为普通的表示法来使用. -H--no-filename #在显示符合样式的那一行之前, does not indicate the name of the file to which the line belongs. -H--with-filename #在显示符合样式的那一行之前 that represents the name of the file to which the row belongs. -I.--ignore- Case#忽略字符大小写的差别. -L--file-with-matches #列出文件内容符合指定的样式的文件名称. -L--files-without-match #列出文件内容不符合指定的样式的文件名称. -N--line-Number #在显示符合样式的那一行之前 that indicates the column number of the row. -Q--quiet OR--Silent #不显示任何信息. -R--recursive #此参数的效果和指定 "-d recurse "parameter is the same. -S--no-messages #不显示错误信息. -V--revert-match #显示不包含匹配文本的所有行. -V--version #显示版本信息. -W--word-regexp #只显示全字符合的列. -X--line-regexp #只显示全列符合的列. -y #此参数的效果和指定 the same as the "-i" parameter.

Rule Expressions for grep:

^ #锚定行的开始 such as:'^grep'matches all lines that begin with grep. $ #锚定行的结束 such as:'grep$'matches all lines that end with grep: #匹配一个非换行符的字符 such as:'GR.P'match GR followed by an arbitrary character, followed by P. * #匹配零个或多个先前字符 such as:'*grep'line that matches all one or more spaces followed by grep ...*#一起用代表任意字符. [] #匹配一个指定范围内的字符, such as'[Gg]rep'match grep and grep. [^] #匹配一个不在指定范围内的字符, such as:'[^a-fh-z]rep'Match does not contain a-r and T-Z begins with a letter that follows the rep line. \(.. \) #标记匹配字符, such as'\ (love\)', Love is labeled as 1. \< #锚定单词的开始, such as:'\<grep'matches the line that contains the word that begins with grep. \> #锚定单词的结束, such as'grep\>'matches a line that contains a word that ends with grep. X\{m\} #重复字符x, M times, such as:'0\{5\}'matches a row that contains 5 O. X\{m,\} #重复字符x, at least m times, such as:'o\{5,\}'matches a row with at least 5 O. X\{m,n\} #重复字符x, at least m times, not more than n times, such as:'O\{5,10\}'Match 5--10 rows of O. \W#匹配文字和数字字符, i.e. [a-za-z0-9], such as:'g\w*p'matches 0 or more literal or numeric characters followed by G and then p.   \w #\w The reverse form, matching one or more non-word characters, such as the dot period, and so on. \b #单词锁定符, such as:'\bgrep\b'Match grep only.


POSIX characters:
POSIX (The Portable Operating System Interface) adds special character classes to the character encodings in different countries, such as [: Alnum:] is another notation for [a-za-z0-9]. You can place them in the [] number to be a regular expression, such as [a-za-z0-9] or [[: Alnum:]]. grep under Linux supports POSIX character classes in addition to Fgrep.
[: Alnum:] #文字数字字符
[: Alpha:] #文字字符
[:d igit:] #数字字符
[: Graph:] #非空字符 (non-whitespace, control characters)
[: Lower:] #小写字符
[: Cntrl:] #控制字符
[:p rint:] #非空字符 (including spaces)
[:p UNCT:] #标点符号
[: Space:] #所有空白字符 (New line, Space, TAB)
[: Upper:] #大写字符
[: Xdigit:] #十六进制数字 (0-9,A-F,A-F)
```

Instance:

Example 1: Find the specified process ' Ps-ef|grep git '
Output:

```
1733820058 85435 65061 0 3:51 pm ttys000 0:00.00 grep--#查找出的进程
Color=auto--EXCLUDE-DIR=.BZR--exclude-dir=.cvs--exclude-dir=.git--#grep进程本身
EXCLUDE-DIR=.HG--exclude-dir=.svn git
```

Example 2: Find the specified number of processes

```
Ps-ef|grep svn-c
Ps-ef|grep-c SVN
```

Output:

```
? ~ Ps-ef|grep svn-c
1
```

Example 3: Reading keywords from a file search ' cat Test.txt | Grep-f Test2.txt '

Output:

```
[email protected] test]# cat Test.txt
Hnlinux
Peida.cnblogs.com
Ubuntu
Ubuntu Linux
Redhat
Redhat
LinuxMint

[email protected] test]# cat test2.txt #读取test2. txt text
Linux
Redhat

[email protected] test]# Cat Test.txt | Grep-f Test2.txt #找找test2中的关键字在test中查找
Hnlinux
Ubuntu Linux
Redhat
LinuxMint
[Email protected] test]#
```
The output Test.txt file contains the line of contents of the keyword read from the test2.txt file and displays the line number of each line

Example 5: Find the keyword ' grep ' result ' code.php ' from the file

```
grep ' result ' code.php #在code. Find result in PHP
$result = mysql_query ($sql);
```


Example 6: Find keywords from multiple files ' grep ' Linux ' Test.txt test2.txt '
Multiple files, when the output of the query to the information content line, the name of the file will be the first line output and ":" as the identifier

Instance 7:grep does not show itself process
Command:

```
PS Aux|grep \[s]sh
PS aux | grep SSH | Grep-v "grep"
```

Example 8: Find the line content that has u start ' cat test.txt |grep ^u '

Example 9: Output non-U-start line contents ' cat test.txt |grep ^[^u] '

Example 10: Output the line ending in hat ' cat test.txt |grep hat$ '

Example 11: Display content containing an ED or at character line ' cat test.txt |grep-e ' Ed|at '

Example 12: Displays all rows in the current directory that contain at least 7 consecutive lowercase characters for each string in a file ending in. txt

' grep ' [a-z]\{7\} ' *.txt '






##### #7 Type

The type command is not actually a lookup command, it is used to distinguish whether a command is brought by the shell or by a standalone binary file outside the shell. If a command is an external command, then using the-p parameter displays the path to the command, which is equivalent to the which command.

Use instances of the type command:

```
$ type cd #系统会提示, CD is the Shell's own command (build-in).
$ type grep #系统会提示, grep is an external command and displays the path to the command.
$ type-p grep #加上-P parameter, which is equivalent to the which command.
```



File Search Command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.