Linux file, content lookup (recursive), grep, Find__linux

Source: Internet
Author: User
Tags arithmetic print print printable characters egrep

Content Lookup (Recursive)

grep/etc/httpd/modules/-lr ' 51ditu ' *

File name lookup (recursive, not suitable to find modules)

find/etc/httpd/-name httpd.conf

Reference:

The grep command searches for patterns specified by the pattern parameter and writes each matching row to the standard output. These patterns are qualified regular expressions that use the Ed or egrep command style. The grep command uses a compressed, indeterminate algorithm.

If more than one name is specified in the file parameter, thegrep command displays the name of the file that contains the matching row. Characters that have special meaning to the shell ($, *, [, |, ^, (,),/) must be enclosed in double quotes when they appear in the pattern parameter. If the pattern parameter is not a simple string, it is usually necessary to enclose the entire mode in single quotes. In expressions such as [A-z],-(minus) CML can specify a range based on the sequence currently being sorted. The collation order can define the equivalent class for use in a character range. If no files are specified,grep assumes the standard input.

Note: do not run the grep command on special files, which can produce unpredictable results. The input line should not contain null characters. The input file should end in line breaks. The newline character does not match the regular expression. Although some flags can be specified at the same time, some of these flags will overwrite other flags. For example, the-l option takes precedence over all other flags. In addition, if you specify the- e and- F flags at the same time, the one that you specify will have precedence. sign

-B Add the block number where the row is found before each row. Use this flag to help locate the disk block number through the context. The- b flag cannot be used for input from standard inputs and pipes.
-C Only the count of matching rows is displayed.
-E View each specified pattern as an extended regular expression (ERE). ERE the null value will match all the rows.

Note: the grep command with the- e flag is equivalent to the egrep command, except that their errors and usage information are different and that the- s flag does not function.

- e Patternlist Specifies one or more search patterns. It acts as a simple pattern, but this is useful when the pattern starts with-(minus). Patterns should be separated by line breaks. You can specify an empty pattern by using two newline characters in a row or by adding a newline character ("n") after quotation marks. Each pattern is treated as a basic regular expression (BRE) unless the- e or- f flags are specified at the same time. grep can accept multiple- e and- f flags. All specified patterns are used when matching rows, but the order of evaluation is not specified.
-F Treat each specified pattern as a string rather than as a regular expression. An empty string can match all rows.

Note: the grep command with the- F flag is equivalent to the fgrep command, except that their errors and usage information are different and the- s flag has different effects.

- F Patternfile Specifies the file that contains the search pattern. Patterns should be separated by line breaks, and empty rows are considered empty patterns. Each pattern is treated as a basic regular expression (BRE), unless the- e or- f flags are specified.
-H Disables the name of the file that contains this row after the row is matched. When more than one file is specified, the file name is blocked.
-H If the- R or- r option is specified and a symbolic link to the reference file type directory is specified on the command line, grep searches for the directory file referenced by the symbolic link and all files below it in the file hierarchy.
-I. Ignores the case of letters when comparing.

-L Lists only (once) the names of files that contain matching rows. File names are separated by line breaks. If you search for standard input, the path name (standard input) is returned. The- l flag is used in conjunction with any combination of the- C and- n flags, and is similar to using only the- l flag.
-L If the- R or- r option is specified, and the symbolic link referencing the file type directory is specified on the command line or encountered during a file hierarchy transfer, the grep Searches for the directory file referenced by the symbolic link and all files below it in the file hierarchy. If both- H and- lare specified, the most recently specified option on the command line takes effect.
-N Place the associated line number in the file before each row. The starting line number for each file is 1, and the row counter is reset when each file is processed.
- P [Separator] Displays the entire paragraph that contains the matching row. Paragraphs are delimited by the paragraph delimiter specified by the Separator parameter, which is the pattern in the same format as the search pattern. Rows that contain paragraph separators are used only as delimiters and are not included in the output. The default paragraph separator is a blank line.
-Q Disables all operations written to the standard output, whether or not it is a matching row. If the input row is selected, it exits at a zero state. The- q flag is used in conjunction with any combination of- C and- l,-n flags, and is similar to using the- Q flag only.
-R Search the directory recursively. By default, follow the link to the directory.
-R Search the directory recursively. By default, the link to the directory is not followed.
-S Suppresses error messages that are usually written because the file is not present or unreadable. Other error messages are not prohibited.
-V Displays all rows that do not match the specified pattern.
-W Perform a word search.
-X Displays rows that exactly match the specified pattern without any other characters.
-Y Ignores the case of characters when comparisons are made.
Patternlist Specifies one or more patterns that will be used in the search. These patterns will be treated as if they were specified using the- e flag.
File Specifies the name of the file that will be searched for mode. If a File variable is not given, the standard input is used

Introduction and regular expression of find and grep commands

Two more useful commands and regular expressions

Before we start learning new shell programming knowledge, let's look at two more useful two commands, which, while not part of the shell, are often used in shell programming. Then we'll take a look at the regular expression.

Find Command

We first looked at the find command. This command is quite useful for us in locating files, but it is difficult for Linux novices to use because of the options, tests, action type parameters that he brings, and the execution of a parameter that affects the next parameter.
Before we dive into these options and parameters, let's look at a very simple example. If there is a file wish on our machine. We're going to run it as root when we do this, so that we can search the entire machine:

# Find/-name Wish-print
/usr/bin/wish
#

As we can think of, he will print out the results of the search. It's simple, isn't it?
However, he needed a certain amount of time to run, because he would also search for disks on the window machine on the network. The Linux machine mounts the file system of a large block of window machines. He will also be in those locations, although we know that the files we are looking for are located on the Linux machine.
This is where the first option comes in. If we specify the-mount option, we can tell the find command not to search for the mounted directory.

# Find/-mount-name Wish-print
/usr/bin/wish
#

So we can still search for this file, but this time there is no search for the mounted file system.

The complete syntax for the Find command is as follows:

find [path] [options] [tests] [actions]

Path is a very simple part: we can use absolute paths, such as/bin, or use relative paths, for example. . If we need to, we can also specify multiple paths, such as Find/var/home

Some of the main options are as follows:
-depth to search the contents of the directory before viewing the directory itself
-follow Follow Symbolic link
-maxdepths n searches for a directory at most n-tier
-mount (or-xdev) do not search for other file systems

Here are some options for test. We can specify a large number of tests for the Find command, and each test will return true or false. When the finding command works, he examines the files that are found in order, and carries out the tests that they define sequentially on the file. If a test returns a fake, The Find command stops the file that he is currently examining and continues with the following actions. We have listed in the table below just some of the tests we use most often, and we can view the manual page to get the possible extension list items that we can take advantage of the Find command.

-atime n n days previously accessed files
-mtime n N days ago modified files
-name pattern In addition to the path, the filename that matches the specified type. To ensure that the specified type is passed to the find command rather than being immediately assigned by the shell, the specified type must be quoted.
-newer Otherfile to a new file compared to the Otherfile file
-type c C Type of file, where C can be specified as a type. The most commonly used is that D represents the directory, and F refers to ordinary files. For other file types, we can view the man page.
-user username files owned by the specified user
We can also use the combination of operators for testing. Most of these are in two formats: Short form and long format.
! -not Test of anti-
-a-and all tests must be true.
One of the-o-or tests is true

We can use parentheses to force the order of tests and operators. Because these are special to the shell, we also need to use backslashes to reference them as a whole. In addition, if we specify a match type for the filename, we must also quote it in quotation marks. This prevents them from being extended by the shell so that they can be passed directly to the Find command. So if we're going to write a test like this, to find a file that's closer to the X file or start with a range, we'll write the following form:

/(-newer x-o-name "_*"/)

Now we're going to try to find files in the current directory that are closer to the last modified date than While2, and we can use the following command:

$ find. -newer While2-print
.
./elif3
./words.txt
./words2.txt
./_trap
$

The commands we used above looked good, but we also searched for the current directory file, which was not what we wanted, and we were interested in just regular files. So we can add another test-type F:

$ find. -newer While2-type F-print
./elif3
./words.txt
./words2.txt
./_trap
$

working principle:

How do these orders work? We specify that the find command should be found in the current directory (.), and we are looking for files that are newer than While2 (-newer while2), and if you have passed the test, test whether the file is a regular file (- TYPE-F). Finally, we use the actions we used before,-print, just to verify the files we found.

The file we are looking for is either a new file that starts with an underscore or is newer than the While2 file, but it must also be a regular file. This example shows us how to test the mix:

$ find. /(-name "_*"-or-newer while2/)-type F-print
./elif3
./words.txt
./words2.txt
./_break
./_if
./_set
./_shift
./_trap
./_unset
./_until
$

Now we can see that this is not a very difficult thing, is it not? We have to escape the parentheses so that he is not protected by the shell and quotes * in quotation marks so that he can pass it directly to the Find command.

Now that we can reliably locate files, let's take a look at some of the collaborations that we can do when we look for the specified file. Again, we're going to emphasize that we're listing just the most common options here, and we can see the full collection of the man pages.

The-exec command executes one of the commands. This is the most common action we perform.
The-ok command is similar to the-exec, except that he prompts the user to confirm the command before executing the command that will be executed.
-print Print out file name
-ls use the LS command to list the current file
The-exec and-OK commands are part of the parameter sequence of one line, until a non-terminal/sequence is encountered. For-exec and-ok, the string {} is a special type and will be replaced by the absolute path of the current file.

Such explanations may not be easy to understand, but an example might be a good way to illustrate them.

As a simple example of the following:

$ find. -newer while2-type f-exec ls-l {}/;
-rwxr-xr-x 1 Rick Rick 275 Feb 8 17:07./elif3
-rwxr-xr-x 1 Rick Rick 336 Feb 8 16:52./words.txt
-rwxr-xr-x 1 Rick Rick 1274 Feb 8 16:52./words2.txt
-rwxr-xr-x 1 Rick Rick 504 Feb 8 18:43./_trap
$

As we can see now, the Find command is quite useful. It only takes a few simple exercises to get this command right. However, such exercises may have to pay a price, so we should do some find order experiments.

grep Command

The second very useful command we're going to see is the grep command, which is an uncommon name and is the abbreviation for a generic regular expression parser (general Regular Expression Parser). We use the Find command in our system to locate the required files, But we're going to use the grep command to find the specified string in the file. In fact, the most common practice is when we use the Find command to pass grep as a command to-exec.

The grep command can take options, match patterns, and the files we want to find in them:
grep [options] pattern [FILES]
If you do not specify a filename, he searches for standard input.
Let's start with the main options for the grep command. Here are just a few of the major options that we can get from the manual.

-C prints out the total number of matching rows, rather than printing out matching rows
-E Open Extended expression
-h suppresses the file name in which the matching content is found as the prefix of the output line
-I ignores case
-l lists file names with matching rows instead of output actual matching rows
-V Converts a matching type to select a row that does not match rather than a matching row
Here are some examples:
$ grep in Words.txt
When shall we three meet again. In thunder, lightning, or in rain?
I Come, graymalkin!
$ grep-c in Words.txt words2.txt
Words.txt:2
Words2.txt:14
$ grep-c-V in Words.txt words2.txt
Words.txt:9
Words2.txt:16
$

working principle:

In the first example, the option is not specified, and the grep command simply looks for the string in in the Words.txt file and prints out the matching rows. The filename is not printed here, because here we just use a file.
In the second example, print out the total number of matching rows in two different, in which case the file name will be printed.
In the last example we used the-V option to convert the lookup condition and print out the total number of rows that did not match in the two files.

Regular Expressions

As we can see, the basic usage of the grep command is relatively easy to master. Now we're going to take a look at the basic regular expression, which allows us to do some more complex matches. As we mentioned earlier, Regular expressions are used in Linux or in some open source languages. We can use it in VI or in writing Perl scripts.

During the use of regular expressions, some characters are processed in different ways. Some of the most common uses are as follows:

^ At the beginning of a line
$ at the end of a line
. Any one single character
[] The square brackets contain the range of letters, any one of which can be matched, such as the letter range of the A-E, or we can use ^ to do the opposite.
If we're going to use them as normal characters, we'll precede them with/. So if we're looking for a $ character, we're going to use/$ to find it.

The following are some of the more useful special matches that can be used in square brackets:

[: Alnum:] alpha-numeric characters
[: Alpha:] Letters
[: ASCII:] ASCII characters
[: Blank:] Space or Tab
[: Cntrl:] ASCII code control character
[:d Igit:] Number
[: Graph:] Non-control, non-space characters
[: Lower:] Small Letter
[:p rint:] printable characters
[:p UNCT:] punctuation character
[: space:] whitespace characters, including vertical tab
[: Upper:] Uppercase characters
[: xdigit:] hexadecimal digits

In addition, if you specify an extension match using the-e option, you may be following a regular expression with some other character that controls the matching type. If we just want to use them as normal characters, we also need to precede them with an escape character/.

? Optional match, but match up to once
* Must match 0 or more items
+ must match 1 or more items
{n} must match n times
{N,} must match n times or more times
{n,m} match range N to m times, including M times

This may seem a bit complicated, but if we step in, we'll find that it's actually not as complex as we first saw it. The simplest way to master regular expressions is to try some examples:

If we want to find the line ending with character E, we can use the following command:

$ grep e$ Words2.txt
Art thou not, fatal vision, sensible
I thee yet, in form as palpable
Nature seems dead, and wicked dreams abuse
$

As we can see, this command searches for a matching line ending with E.
Now let's say we're looking for a word that ends with the letter A. To achieve this, we use special matches in square brackets. In such cases, we want to use [[: Blank:]], which tests a space or a tab:

$ grep a[[:blank:]] Words2.txt
Is this a dagger which I before me
A dagger of the mind, a false creation,
Moves like a ghost. Thou Sure and Firm-set earth,
$

Now let's say we're looking for a three-letter word that begins with th. In this case, we need to use [[: Space:]] To determine the end of a word and use it. to match another letter:

$ grep th.[[:space:]] Words2.txt
The handle toward my hand? Come, let me clutch thee.
The curtain ' d sleep; Witchcraft celebrates
Thy very stones prate of my whereabout,
$

Finally, we're going to use the extended grep command to look up 10-character words in lowercase letters. Here we want to specify a range of characters to match A to Z, while specifying 10 repetitions of the character:

$ GREP-E [a-z]/{10/} words2.txt
Proceeding from the heat-oppressed brain?
And such an instrument I is to use.
The curtain ' d sleep; Witchcraft celebrates
Thy very stones prate of my whereabout,
$

We are here just to touch some of the more important parts of regular expressions. As with most of the other content in Linux, there are many documents to help us find more detail, but the best way to learn regular expressions is to experiment with these expressions.

Command execution:

When we write a script, we often need to use the results of the command execution results in the shell script. It says we need to execute a command and put the output of this command in a variable. We can then use the $ (command) we described in the example of the previous set command Syntax. This is also a relatively old format, and the most commonly used usage is the ' command ' format.

All new scripts should use $ (...) The format, this can be used to avoid some fairly complex conversion rules that are created by using the $, ',/in the inverted quote command. If you use inverted quotes in the ' ... ' structure, we need to use/to escape. These relatively vague characters confuse the program, Sometimes even some experienced programs have to experiment to make the quotes in the inverted quote command work correctly.

The result of the $ (command) command is simply the output of a simple command. Here we should note that this is not the return state of the command, but the output string. As the following example:

#!/bin/sh
echo the current directory is $PWD
echo the current users are $ (WHO)
Exit 0

Because the current directory is a shell environment variable, the first line does not need to use this command to execute the structure. However, the results of the WHO command, if you want him to be visible in this script, we will use this command structure.

If we want to put their results in a variable, we can assign them to a variable as usual:

Whoisthere=$ (WHO)
Echo $whoisthere

The ability to put the execution results of a command in a script variable is quite powerful, this makes it easy to use the current commands in the script and get their output. If you find that you are trying to convert a standard command on the standard output of the output of the parameter set merge them as a program parameter, You will find that the command Xargs will help you accomplish all this. You can view the man page to get deeper and more detailed content.
One problem that sometimes arises is that the command we want to call prints out a few whitespace characters before the text we want, or more than we want. In such cases, we can use the SET command we mentioned earlier.

Arithmetic extension

We have used the expr command, which allows simple arithmetic commands to be processed, but his execution is rather slow because a new shell needs to be invoked when processing the expr command.

A new and better replacement is $ ((...)) Extend. By wrapping the expression we want in parentheses so that we can make a $ ((...)) , we can do more efficient simple arithmetic.

As in the following example:

#!/bin/sh
X=0
While ["$x"-ne 10]; Todo
Echo $x
x=$ (($x + 1))
Done
Exit 0

parameter Extension

We have already seen the simplest form of parameter assignment and extension, where we write this:

Foo=fred
Echo $foo

The problem occurs when we want to add another character to the end of a variable. Let's say we're going to write a short script to handle the file named 1_tmp and 2_tmp, and we can try this with the following script:

#!/bin/sh
For I in 1 2
Todo
My_secret_process $i _tmp
Done

But in every loop, we get the following information:

My_secret_process:too few arguments

What's wrong with it?

The problem is that the shell will try to replace the variable $i_tmp with his variable value, but there is no such variable. The shell does not think this is an error, but instead uses a null value to replace it, so there is no argument passed to my_secret_process. To add $ The extended protection of I is part of the variable, we need to put I in a pair of curly braces:

#!/bin/sh
For I in 1 2
Todo
My_secret_process ${i}_tmp
Done

So later in the first loop, the value of I is replaced with ${i}, giving an actual filename. So we've replaced the value of one parameter with a string.

We can do a lot of substitutions in the shell. Often such a method provides an elegant solution to the problem of parameter handling.

Some of the following tables are commonly used:

${parm:-default} If a parameter is empty, set it to a default value.
${#parm} gives the length of the parameter.
${parm%word} starts at the end, removing the smallest part that matches word and returns the rest.
${parm%%word} starts at the end, removing the longest part that matches word and returns the rest.
${parm#word} Starts from the beginning, removes the smallest part that matches word and returns the rest.
${parm# #word} starts at the beginning, removes the longest part that matches word and returns the rest.

These replacements are quite useful for us to work with strings. The last four can be used to remove parts of a string, which is more useful for handling filenames and paths. As shown in the following examples:

#!/bin/sh
unset foo
Echo ${foo:-bar}
Foo=fud
Echo ${foo:-bar}
Foo=/usr/bin/x11/startx
Echo ${foo#*/}
Echo ${foo##*/}
Bar=/usr/local/etc/local/networks
Echo ${bar%local*}
Echo ${bar%%local*}
Exit 0

If we run this script we'll get the following output:

Bar
Fud
Usr/bin/x11/startx
StartX
/usr/local/etc
/usr

working principle:

The first sentence, ${foo:-bar}, specifies bar for the value of Foo, because no value is specified for Foo when the statement starts executing. The value of Foo remains unchanged until he encounters the unset statement.

Here are some things we need to be aware of:

${foo:=bar} will set the variable $foo. This string operator detects that Foo exists and is not a null value. If he is not empty, he will return his value, but if the reverse is the case, the value of Foo is set to bar and the result value of the substitution is returned.
${foo:?bar} prints out the Foo:bar and exits the command if Foo does not exist or if he is set to a null value.
Finally, ${foo:+bar}, if Foo exists and is not empty, it returns bar.
The {foo#*/} statement matches and only removes the contents of the left (here we have to remember that * matches 0 or more characters). {foo##*/} matches and removes as much content as possible, so he removes the rightmost/and all characters in front of him.
The {bar%local*} statement matches the characters that start from the right until the first occurrence of the local character, while {bar%%local*} matches as many characters from the right as possible until the first time that local is found.

Because Unix and Linux are strongly dependent on the concept of filtering, we often have to manually redirect the results of an operation. Let's say we're going to use the Cjpeg command to convert a GIF file to a JPEG file:

$ cjpeg image.gif > Image.jpg

Maybe sometimes we do this on a lot of files. How do we automatically redirect? We can easily do this:

#!/bin/sh
For image in *.gif
Todo
Cjpeg $image > ${image%%gif}jpg
Done

This script converts each GIF file in the current directory to a JPEG file.

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.