How Linux searches for files

Source: Internet
Author: User
Tags file size mixed

Looking up a file on Linux is a bit of a hassle. After all, in Linux we need to use the dedicated "find" command to find the files on the hard disk.

Linux file expression format is very complex, unlike windows,dos under the uniform aaaaaaa.bbb format, so easy to find, in Windows, as long as you know to find the file name or suffix is very easy to find. The command for finding files in Linux is usually the "find" command, which helps us find the files we need in the day-to-day business of using and managing Linux. For Linux novices, the "find" command is also a way to learn and learn about the features of Linux files. Because of the variety of Linux distribution, version upgrade quickly, in Linux books often specify the location of a configuration file, often Linux novice netizens still can't find. For example Redhat Linux 7.O and Redhat Linux 7.1 Some of the important configuration files in the hard disk location and file directory has changed a lot, if you do not learn to use the "find" command, So it's pretty hard to find one of the profiles in thousands of Linux files, and I've had it before without mastering the "find" command. OK, here is a detailed description of the powerful "find" command of the full use of the method and use.

Find by file name:

This approach is as easy to understand as finding a file under Windows. If you put this file in a single folder, as long as the use of the common "ls" command can easily find out, then use the "find" command to look up it can not give you a deep impression, after all, "find" the powerful function of the command more than this. If you know the file name of a file, you don't know which folder to put it in, even in layers of nested folders. For example, if you forget httpd.conf this file is in which directory of the system, or even somewhere in the system, you can use the following command:

#find/-name httpd.conf

This command syntax looks very easy to understand, is directly in find behind the-name, indicating that the system to search by file name, and finally write httpd.conf this target filename can be. Wait a minute. The system displays a list of search results on the computer screen:

Etc/httpd/conf/httpd.conf

This is the full path of the httpd.conf file on the Linux system. Find success.

If the system does not display results when you enter the above lookup command, do not assume that the system does not perform the find/-name httpd.conf command, and that the Apache server may not be installed on your system, so long as you install the Apache Web server and then use the Find /-name httpd.conf will be able to find this configuration file.

No Error Lookup tips:

The "find" command in a Linux system is a command that most system users can use, and is not a patent for the root system administrator. However, it is also possible for ordinary users to use the "find" command, which is that system administrator root in the Linux system can set certain file directories to a disabled access mode. So that ordinary users do not have the right to use the "find" command to query these directories or files. When ordinary users use the "find" command to query these file directories, there are often "permissiondenied." (No access) words. The system will not be able to query the file you want. To avoid such errors, we try to find the file using the transfer error hint, and enter

Find/-name Access_log 2>/dev/null

This approach is to move the lookup error hint to a specific directory. When the system executes this command, the error message is delivered directly to Stderrstream 2, Access_log 2 means that the system will send the error message to Stderrstream 2,/dev/null is a special file that indicates empty or wrong information, The error messages that are queried will be shifted and will not be displayed again.

Finding files on a Linux system can also be a real problem. If we're looking for a file on the entire hard drive, it's going to take a long time, especially for large Linux systems and larger-capacity hard disks, where the files are embedded in a deeply nested directory. If we know that this file is stored in a large directory, it will save a lot of time if you look down in the directory. Using Find/etc-name httpd.conf can solve this problem. The above command means to query httpd.conf this file in the ETC directory. Here again explain "/" the meaning of this function symbol, if the input "find/" means that the Linux system in the entire root directory to find files, that is, the entire hard disk to find files, and "Find/etc" is only in the ETC directory to find files. Because "find/etc" means finding files only in the ETC directory, the lookup speed is much faster.

Find methods based on partial file names:

This method is the same as finding a known filename method in Windows. However, the way to find files based on partial filenames in Linux is much more powerful than similar lookups in Windows. For example, if we know that a file contains 3 letters of SRM, it is possible to find all files containing these 3 letters in the system, and enter:

Find/etc-name ' *srm* '

This command indicates that the Linux system will look for all the 3-letter files containing SRM, such as Absrmyz, TIBC.SRM, and so on, in the entire directory of/ETC to display the eligible files. If you also know that this file starts with the 3-letter SRM, we can also omit the front asterisk, which commands the following:

Find/etc-name ' srm* '

This is only a file such as Srmyz is found, such as Absrmyz or ABSRM files are not met the requirements, not be displayed, so the efficiency and reliability of the search file is greatly enhanced.

According to the characteristics of the file Query method:

If you only know the size of a file, the modification date and other features can also be found using the "find" command, which is essentially the same as the "search" feature in Windows systems. Search Companion in Microsoft's search for Windows makes it easier to search for files and folders, printers, users, and other computers in the network. It even makes it easier to search the Internet. The Search Companion also includes an Indexing service that maintains the index of all files on the computer, making the search faster. When you use Search Companion, users can specify multiple search criteria.

For example, users can search for files and folders by name, type, and size. Users can even search for files that contain specific text. If the user is using Active Directory, you can also search for a printer with a specific name or location.

For example, we know a Linux file size of 1,500 bytes, then we use the following command to query Find/-size 1500c, the character C indicates that the size of the file to look for is in bytes. If we do not know the exact size of this file, then in Linux can also be a fuzzy lookup method to solve.

For example, if we enter find/-size +10000000c This command, we specify that the system finds files larger than 10000000 bytes in the root directory and displays them. The "+" in the command indicates that the system is required to list only files that are larger than the specified size, and using "-" means that the system is required to list files that are less than the specified size. The following list is the search action that the system will make when Linux uses different "find" commands, from which we can easily see that there are many ways to use the "find" command in Linux, and finding commands to locate files as long as they are flexible enough to not have the ability to find them in Windows.

Find/-amin-10 # finds files accessed in the last 10 minutes of the system

Find/-atime-2 # finds files accessed in the last 48 hours of the system

Find/-empty # Look for files or folders that are empty in the system

Find/-group Cat # finds files that belong to Groupcat in the system

Find/-mmin-5 # finds files that have been modified in the last 5 minutes of the system

Find/-mtime-1 #查找在系统中最后24小时里修改过的文件

Find/-nouser #查找在系统中属于作废用户的文件

Find/-user Fred #查找在系统中属于FRED这个用户的文件

The following list is a partial condition for locating the characteristics of a file that can be specified by the Find command. Here is not a list of all the search conditions, reference to the Linux related books can know all the find command of the lookup function.

-amin N

Find the last n minutes of files accessed in the system

-atime N

Find the last n*24 hours accessed in the system

-cmin N

Find files in the system where the last n minutes have been changed

-ctime N

Find files in the system that have been changed for the last n*24 hours

-empty

Find a blank file in the system, or a blank file directory, or a folder with no subdirectories in the directory

-false

Find files that are always wrong in your system

-fstype type

Locate files in the system that are present on the specified file system, such as ext2.

-gid N

Find files with file Number group ID N in System

-group Gname

Locate files in the system that belong to the GNAM filegroup and specify the groups and IDs

A description of the control options for the Find command:

The Find command also provides users with some specific options to control the lookup operation. The following table is the most basic and commonly used control option and its usage for the Find command that we have summarized.

Options

Use description

-daystart

The test system is 24 hours from today, with a similar-amin

-depth

Use a depth-level lookup process to find the contents of a file in a specified directory at a level

-follow

Follow the linked connection method to find; In addition, you can also ignore the linked access method query

-help

Show Command Summary

-maxdepth levels

Find in descending way in a directory of a hierarchy

-mount

is not found in the file system directory and is similar to-xdev.

-noleaf

Disable optimized lookups in non-unux file systems, MS-DOS systems, CD-ROM file systems

-version

Print version numbers

After using the-follow option, the Find command follows the wildcard linked connection method, unless you specify this option, otherwise the Find command will ignore the linked connection method for file lookup.

The function of the-maxdepth option is to restrict the Find command to search for files in descending order in the directory.

More than one level or searching for too many directories, which results in slower lookups and too much time to find.

For example, we will be in the current (.) Directory Tip subdirectory to find a file called Fred, we can use the

Under command

Find. -maxdepth 2-name Fred

If this Fred file is in the./sub1/fred directory, then the command will locate the file directly, and the search is easy to succeed. If this file is in the./sub1/sub2/fred directory, then the command cannot be traced. Because the find command has been given the largest query directory level of 2 in the directory, you can only look for files in the 2-tier directory. The purpose of this is to make the find command more accurate positioning of the file, if you already know a file probably in the file directory progression, then add-maxdepth n will soon be able to search in the specified directory success.

Find a file by using a mixed lookup method

The find command can use a mixed lookup method, for example, we want to find more than 100000000 words in the/tmp directory

section and a file that was modified within 48 hours, we can use-and to pick up two search necklaces.

Combine to form a hybrid lookup.

Find/tmp-size +10000000c-and-mtime +2

Friends who have studied computer languages know that in computer languages, and, or, or, respectively, "and" and "or"

of the relationship. As common as in the search command for Linux systems.

There are examples of this,

Find/-user Fred-or-user George

We can explain that we look for files in the/tmp directory that belong to two users, Fred or George.

You can also use a "non" relationship in the Find command to look up files if we want to find all of them in the/tmp directory

Files that are not part of the panda, using a simple

Find/tmp! -user Panda

The order will be resolved. Very simple.

Ways to find and display files

Finding a file is our purpose and we would like to know the details and attributes of the file found, if

We take the now find file, in using the LS command to view file information is quite cumbersome, now we can also

Use the two commands together.

Find/-name "httpd.conf"-ls

The system displays httpd.conf file information on the screen immediately after the httpd.conf file is found.

12063 34-rw-r--r--1 root 33545 Dec 15:36/etc/httpd/conf/httpd.conf

The following table is a list of commonly used to find files and display file information parameters and how to use

Options

Use description

-exec command;

Find and Execute commands

-fprint file

Print file full filename

-fprint0 file

Print the full file name including the empty file

-fprintf file format

Print file format

-OK command;

Perform actions on user commands, confirm input execution According to user's Y

-printf format

Print file format

-ls

Prints files of the same file format.

Summary: So far we have learned this more about the use of Find commands, but also lists a lot of common find command options, if we can master the use of the Find command in Linux, then looking for files in Linux is not a difficult thing.

Note : More wonderful tutorials Please pay attention to the triple computer tutorial section, triple Computer group: 189034526 welcome you to join

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.