Linux Find File Lookup command with grep file Content Lookup command

Source: Internet
Author: User

When using Linux, file lookups are often required. The commands you find are primarily find and grep. Two commands are of a district.

  The Difference : (1) The Find command is based on the attributes of the file , such as file name, file size, owner, owning group, whether it is empty, access time, modification time, etc.

(2) grep is a search based on the contents of the file , and each line of the file is matched in a given pattern (patter).

  A. Find command

basic format:Find path expression

    1. Search by file name

(1) Find/-name httpd.conf #在根目录下查找文件httpd. conf, which indicates that the entire hard drive is found
(2) Find/etc-name httpd.conf #在/etc directory file httpd.conf
(3) find/etc-name ' *srm* ' #使用通配符 * (0 or any number). Find files in the/etc directory that contain the string ' SRM ' in the file name
(4) Find. -name ' srm* ' #表示当前目录下查找文件名开头是字符串 ' SRM ' files

    2. Search by File feature

(1) Find/-amin-10 # Find files accessed in the last 10 minutes of the system (access time)
(2) Find/-atime-2 # Find files accessed in the last 48 hours of the system
(3) Find/-empty # finds files or folders that are empty in the system
(4) Find/-group Cat # finds files that belong to group Cat in the system
(5) Find/-mmin-5 # finds files that have been modified in the last 5 minutes of the system (modify time)
(6) Find/-mtime-1 #查找在系统中最后24小时里修改过的文件
(7) Find/-user Fred #查找在系统中属于fred这个用户的文件
(8) Find/-size +10000c #查找出大于10000000字节的文件 (c: Bytes, W: double word, K:KB,M:MB,G:GB)
(9) Find/-size-1000k #查找出小于1000KB的文件

3. Find a file using a hybrid lookup method

Parameters are:! ,-and (-a),-or (-O).

    (1) find/tmp-size +10000c-and-mtime +2 #在/tmp directory to find files larger than 10000 bytes and modified in the last 2 minutes
(2) Find/-user fred-or-user George #在/directory lookup user is Fred or George's file file
(3) Find/tmp! -user Panda #在/tmp directory to find all files that are not part of panda users

Second, grep command

basic format:Find expression

1. Main parameters

    [Options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive
-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.

    2. Example

(1) grep ' test ' d* #显示所有以d开头的文件中包含 Test line
(2) grep ' Test ' AA bb cc #显示在aa, the line containing test in the BB,CC file
(3) grep ' [a-z]\{5\} ' AA #显示所有包含每行字符串至少有5个连续小写字符的字符串的行
(4) grep magic/usr/src #显示files (without subdirectories) in the/USR/SRC directory contain magic lines
(5) Grep-r magic/usr/src #显示files (including subdirectories) in the/USR/SRC directory containing magic lines

(6) Grep-w pattern files: matches only the entire word , not a part of the string (such as matching ' magic ', not ' magical '),

Linux Find File Lookup command with grep file Content Lookup 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.