Linux Series tutorial (vi)--linux file Search command

Source: Internet
Author: User

Previous Blog We explained the Linux Link command and Rights Management commands, through the LN-S link name to create a soft link, no-s means to create a hard link, there are three changes to the permissions of the command, chmod command can change the file or directory permissions, chown command can change the file or directory owner, The CHGRP command can change the group to which the file or directory belongs. However, it is important to note that the following two commands can only be done by the root user, while the chmod command can be completed by the root user, and the owner of the file or directory is changed. Then this blog we will continue to introduce the Linux command--file Search command.

1. The most powerful search command: Find

First of all, the Find command is the most powerful command we use for file search in a Linux system. But what we want to say is to use the Find command as little as possible to perform the search task, even if you want to search we should try to narrow the scope, and do not use the server during peak times for file search, because the search is also a very important system resources. This requires us in the Linux file collation, as far as possible to standardize, what files in what directory should have a better convention.

Find this command to be completely clear, I am afraid it will take a long time, and a lot of usage we can hardly use, so here I do not introduce each of the use of this command, I will detail several of the most common usage, we just need to remember that these are completely enough for our daily use.

①, command name: Find

② and English original meaning:

③, command path:/bin/find

④, execute permissions: All Users

⑤, Function Description: Carry out a variety of fancy file search

⑥, Syntax: find "search scope" "Match criteria"

  Note: Linux search is significantly different from Windows, and Linux strictly differentiates file capitalization.

I. Search by file or directory name

Find "Search directory" "-name or-iname" "Search character": difference between-name and-iname a case-insensitive, one case insensitive

①, Find/etc-name init (exact search, name must be init to search)

②, Find/etc-iname init (exact search, name must be init or can be searched with uppercase letters)

③, Find/etc-name *init (fuzzy Search, file or directory name ending with init)

④, Find/etc-name Init??? (Fuzzy Search,?) Represents a single character, i.e. search to init___)

  

Second, according to file size search

For example: Find files larger than 100M in the root directory

Find/-size +204800

Here +n means greater than,-n means less than, n means equals

1 Data Block = = 512 byte ==0.5kb, which is 1KB equals 2 data block

100MB = = 102400kb==204800 Data block

  

Iii. Search by owner and owning group

①, in the home directory to query the file belongs to the group root

Find/home-group Root

②, in the home directory to query the owner-root file

Find/home-user Root

  

Iv. Search by Time attribute

Find "Path" "Options" "Time"

There are three types of options:-amin access time

-cmin file property is changed

-mmin file contents are modified

Time: +n,-n,n for more than n minutes, less than n minutes and n minutes respectively

Example: Find files and directories with properties modified within 5 minutes in/etc directory

Find/etc-cmin-5

V. Search by file type or I-node

-type based on file type: F for file, D for directory, L for soft link

Example: Finding the file type in the/home directory is the Find/home-type L of the directory

-inum based on I-node lookup

Example: Finding a file or directory with the I node 400342 in the/tmp directory Find/tmp-inum 400342

  

Six, combination conditions search

Here are two parameters:

①,-a means that two conditions are met (and)

②,-o means two conditions to satisfy any one (or)

Example: Find files larger than 80MB and less than 100MB in/etc directory

Find/etc-size +163840-a-size-204800

2. Locate the file in the File Repository command: Locate

①, command name: Locate

② and English original meaning:

③, command path:/usr/bin/locate

④, execute permissions: All Users

⑤, Function Description: Find files in the file repository

⑥, Syntax: Locate "file name"

-I is case insensitive

  Note: There is a difference between this and the Find command, and find is a full search, while locate is searching in the file repository. So the locate command executes much faster than the Find command. But there's a problem here, and the file repository needs to be constantly updated. If our newly created file does not update the file repository, the use of locate is not found.

  UpdateDB manually update the repository, but for new files in the/tmp directory, the file repository is not updated because the/tmp directory is not part of the file repository.

  

3, Search command directory and alias Information: which

①, command name: which

② and English original meaning:

③, command path:/usr/bin/which

④, execute permissions: All Users

⑤, Function Description: Search the directory of the command and alias information

⑥, Syntax: which "command"

Example: Querying the directory where the LS command resides and the alias information

  

4. The directory where the search command is located and the Help document path: Whereis

①, command name: Whereis

② and English original meaning:

③, command path:/usr/bin/whereis

④, execute permissions: All Users

⑤, Function Description: The directory where the search command resides and the Help document path

⑥, Syntax: Whereis "command"

Example: Querying the directory where the LS command resides and the Help document path

  

5. Search the file for string matching lines and output: grep

①, command name: Whereis

② and English original meaning:

③, command path:/bin/grep

④, execute permissions: All Users

⑤, Function Description: Search the file for string matching lines and output

⑥, Syntax: Grep-iv "Specify string" "File"

-I is case insensitive

-V excludes the specified string

Example: Find the line in the/root/install.log file that contains the MySQL string and output

grep mysql/root/install.log

  

6. Summary

This blog we introduce several file search commands, where find is the most powerful file or directory Search command, and another search command locate the difference is that the Find command is a full search, the newly created file can also be searched, and locate is in the file database search, compared to find command search faster, but the newly created file if not included in the file repository, using the Locate command is not searchable, and then/tmp directory is not included in the file database, This means that the locate command is used to search for files that are not in the/tmp directory.

Then we describe the directory where the search command is located and the alias information which, the directory where the search command is located, and the Help document path Whereis, the last command grep and the previous search file or directory command is different, grep is to search for a matching string in the file, is in the file content search, This command is used more later, you need to remember the usage.

Linux Series tutorial (vi)--linux file Search command

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.