Linux find grep Find command

Source: Internet
Author: User
Tags egrep

Original: fhqdddddd.blog.163.com/blog/static/186991542012417105729415/

Find

1. Role

The function of the Find command is to search the directory for a file, and its use rights are for all users.

2. Format

Find [Path][options][expression]

path Specifies the directory path from which the system starts to look down the file down the directory tree. It is a list of paths that are separated from each other by spaces, and if you do not write path, the current directory is assumed.

3. Main parameters

[Options] Parameters:

-depth: Use the Depth-level lookup process to prioritize file content in a specific level of the specified directory.

-maxdepth levels: Indicates that at most find the first level subdirectory of the start directory. Level is a non-negative number, and if level is 0, it is only found in the current directory.

-mindepth levels: Indicates that at least the level subdirectory of the start directory is found.

-mount: Not found in directories and files in other file systems (such as MSDOS, VFAT, etc.).

-version: Print version.

[Expression] is a matching expression and is an expression accepted by the Find command, and all operations of the Find command are for an expression. It has a lot of parameters, and here are just a few common parameters.

-name: Supports wildcard characters * and?.

-atime N: Searches for files that have been read in the last n days.

-ctime N: Searches for files that have been modified in the last n days.

-group Grpoupname: Search for all files with Grpoupname group.

-user User name: Searches for all files that belong to the primary user name (ID or name).

-size N: The file size of the search file is n blocks.

-print: Output search results, and print.

4. Application Tips

The Find command finds several ways to locate a file:

(1) Search by file name

For example, if we want to find a file with a filename of lilo.conf, you can use the following command:

Find/-name lilo.conf

The "/" after the Find command means that the entire hard disk is searched.

(2) Quickly find files

Finding files based on file names can have a real problem, which is to take a long time, especially when large Linux file systems and high-capacity hard disk files are placed in deep sub-directories. If we know that this file is stored in a directory, you can save a lot of time by looking down in that directory. such as the smb.conf file, from its file suffix ". conf" can be determined that this is a configuration file, then it should be in the/etc directory, at this time can use the following command:

Find/etc-name smb.conf

This way, you can shorten the time by using the Quick Find file method.

(3) Search method based on partial file name

Sometimes we know that only one file contains the 4 words of ABVD, then to find all the files in the system that contain these 4 characters, you can enter the following command:

Find/-name ' *abvd* '

After entering this command, the Linux system will look in the/directory for all files containing the ABVD 4 characters (where * is a wildcard character), such as Abvdrmyz and other eligible files can be displayed.

(4) Find a file using a hybrid lookup method

The find command can use a hybrid lookup method, for example, if we want to find a file that is larger than 500000 bytes in the/etc directory and modified within 24 hours, you can use-and (and) to link the two lookup parameters together into a mixed lookup.

Find/etc-size +500000c-and-mtime +1

The role of the Find command is to search for files in the directory based on file names

Find lists the full path of all files and folders for the current directory and its subdirectories.

find-name Help.java searches the current directory and its subdirectories for files with the file name Help.java .

find.-name Help.java searches the current directory and its subdirectories for files with the file name Help.java (IBID.).

Find/-name Help.java searches the entire hard drive for files with file name Help.java.

find-perm 755 to find files of the specified permissions in the current directory and its subdirectories

Find-type b looks for the block device file under the current directory and its subdirectories.

Find-type D Check the folder in the current directory and its subdirectories.

Find-type C finds the character device file under the current directory and its subdirectories.

Find-type P Locate the pipeline file under the current directory and its subdirectories.

Find-type L finds the symbolic link file under the current directory and its subdirectories.

Find-type F finds normal files under the current directory and its subdirectories.

find-type d-exec ls-l {} \; finds the folder under the current directory and its subdirectories, and displays the results in a ls-l manner.

find-type d-ok rm-rf {} \; Find the folder under the current directory and its subdirectories, and execute the RM-RF command in turn, but there will be a confirmation prompt before executing the command

Grep

2. Use

The grep command allows you to specify a file to search for specific content and to output the standard rows containing the content. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.

2. Format

grep [Options]

3. Main parameters

[Options] Main parameters:

-C: Outputs only the count of matching rows.

-I: Case insensitive (only for single-character)


-H: The file name is not displayed when querying multiple files.

-L: Only file names that contain matching characters are output when querying multiple files.

-N: Displays matching lines and line numbers.

-S: does not display error messages that do not exist or have no matching text.

-V: Displays all lines that do not contain matching text.

Pattern Regular Expression Main parameters:

: Ignores the original meaning of special characters in regular expressions.

^: matches the start line of the regular expression.

$: Matches the end line of the regular expression.

<: Starts from the line that matches the regular expression.

: End of line to match regular expression.

[]: A single character, such as [a], a meets the requirements.

[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.

。 : all the individual characters.

*: There are characters, the length can be 0.

Regular expressions are a very important concept in the Linux/unix system. A regular expression (also known as "regex" or "regexp") is a pattern (pattern) that can describe a class of strings. If a string can be described with a regular expression, we say that the character matches the regular expression (match). This and the DOS user can use the wildcard character "*" to represent any character similar. On Linux systems, regular expressions are often used to find patterns in text, and to perform "search-and-replace" operations and other functions on text.

4. Application examples

Querying the DNS service is one of the daily tasks, which means maintaining a large number of IP addresses that cover different networks. Sometimes there are more than 2000 IP addresses. If you want to see the NNN.NNN network address, but forget the rest of the second part, only know that there are two periods, such as nnn nn ... To extract all of the nnn.nnn IP addresses, use [0-9]{3}. [0-0{3}. The meaning is that any number appears 3 times, followed by a period, followed by any number 3 times, followed by a period.

$grep ' [0-9]{3}. [0-0{3} ' ipfile

To add, the grep family also includes Fgrep and Egrep. Fgrep is fix grep, which allows you to find strings instead of a pattern; Egrep is an extended grep that supports both basic and extended regular expressions, but does not support the application of the Q-mode range and some of the more canonical patterns corresponding to it

grep The purpose of the command is to search for files in the directory based on file contents

grep Clock * finds all files in the current directory that contain clock strings, and does not find subdirectories

grep-r Clock * finds files in all files in the current directory that contain clock strings, finds subdirectories

GREP-NR Clock * finds all files in the current directory that contain clock strings, finds subdirectories, and displays line numbers

in the following example we use the grep command. The find command first matches all files named "passwd*", such as passwd, Passwd.old, Passwd.bak, and then executes the grep command to see if there is a SAM user in these files.
# find/etc-name "passwd*"-exec grep "Sam" {} \;
Sam:x:501:501::/usr/sam:/bin/bash

Linux find grep Find 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.