Find File Lookup command and grep file Content Lookup command in Linux

Source: Internet
Author: User
Tags file size

When using Linux, file lookups are often required. The commands that are found mainly have find and grep. Two commands are with district.

Difference: (1) Find command is based on the properties of the file lookup, such as file name, file size, owner, group, whether it is empty, access time, modify time, and so on.

(2) grep searches according to the contents of the file, and matches each row of the file according to the given pattern (patter).

A. Find command

Basic format: Find path expression

1. Find by file name

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

2. Find by File feature

(1) Find/-amin-10 # Finding files accessed in the last 10 minutes of the system (access time)
(2) Find/-atime-2 # Finding files accessed in the last 48 hours of the system
(3) Find/-empty # Search for files or folders that are empty in the system
(4) Find/-group Cat # Look for files that belong to group Cat in the system
(5) Find/-mmin-5 # Search for files 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 mixed 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 look for the user is Fred or George's file file
(3) Find/tmp! -user Panda #在/tmp directory to find all files that do not belong to panda users
Second, grep command

Basic format: Find expression

1. Main parameters

[Options] Main parameters:
-C: Only the count of matching rows is output.
-I: case-insensitive
-H: Do not display file names when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching rows and line numbers.
-S: Do not display error messages that do not exist or do not match text.
-V: Displays all rows that do not contain matching text.

Pattern Regular Expression main parameter:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of a regular expression.
\<: Starts with the line that matches the regular expression.
\>: To the end of the line that matches the regular expression.
[]: A single character, such as [a], that is, a meets the requirements.
[–]: range, such as [A-z], that is, a, B, c all the time until Z meets the requirements.
.: all the individual characters.
*: There are characters, the length can be 0.

2. Examples

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

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

Related Article

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.