Use the find command in linux to search for common file Method Records

Source: Internet
Author: User
Find is a very powerful search command in linux. you can view the find Manual through manfind. it may take a lot of time to read the description of find on one screen. I have summarized the common usage of find.

Find is a very powerful search command in linux. you can use man find to view the find Manual. it may take a lot of time to explain the find statement on one screen. I have summarized the common usage of find.

1. command format

Find search path expression
  • Search path: this is easy to understand, that is, the specified search directory, such as/etc. You can specify multiple search paths and separate them with spaces, for example,/etc/home.
  • Search expression: the key to the find command is that there are many options for the expression. The following command tests are divided based on the expression options.

2. Command Test

A. search for objects by file name

This function is quite common, for example:

$ find /etc -name 'apache2'/etc/default/apache2/etc/php5/apache2/etc/apache2/etc/logrotate.d/apache2/etc/cron.daily/apache2/etc/init.d/apache2

This-name option is commonly used, followed by a file name to be searched. The preceding example is used to search for a file named apache2 in The etc directory. In addition, you should note that the exact search is here, and only the file name is apache2 will be searched. You can use a regular expression to perform fuzzy search, as shown in the following example:

$ find /etc -name 'apache*'

B. search by file Time

If you want to find the most recently modified file, you can use the time option. the time options include atime, mtime, and ctime. mtime is used as the description below.

  • Mtime n: find the file modified on the nth day before.
  • Mtime + n: find the file modified n days ago, excluding the nth day. it is represented by a mathematical interval (-∞,-n)
  • Mtime-n: find the file modified in n days, excluding the nth day. it is represented by a mathematical interval (-n, 0]

-∞ Indicates a long time ago, and 0 indicates today. It is obvious to use the number axis.

Test the time option of the find command in linux

Find the file that has been changed today

$ find /home mtime 0

Here 0 means 0th days, and today is the day. This method is useful for specifying the files that have been modified on a certain day. for example, if you want to find the files modified on January 28, December 30, you can change 0 to 2. As follows:

$ find /home mtime 2

Find the file modified within 2 days

$ find /home mtime -2

The range is expressed as (-2, 0]. Obviously, there are only two numbers-1 and 0, indicating yesterday and today, respectively.

Find the file modified two days ago

$ find /home mtime +2

C. search for files by user or user group

This type of option is relatively common. Commonly used:

  • -Uid n: search for objects whose user ID is n.
  • -Gid n: search for objects whose user group number is n.
  • -User name: search for a file whose user name is name
  • -Group name: Search for the file whose user group name is

Search for files whose username is root

$ find / -user 'root'

Summary

The find command has many options. for details about other options, refer to the man manual. Compared with locate, find will actually scan the disk, which is relatively slow. I suggest you use locate to fix it.

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.