Linux find command

Source: Internet
Author: User
Each operating system is composed of thousands of different types of files. There are system files, user files, shared files, and so on. Sometimes we often forget where a file is stored on the hard disk. Searching for a file in Microsoft's Windows operating system is quite simple. You only need to click "start"-"Search" on the desktop, and then you can find a file on the local hard disk in various ways, local Network, and even search for various files and documents on the Internet.

However, users who use Linux are not so lucky. Finding a file on Linux is really troublesome. After all, in Linux, we need to use a dedicated "Search" command to find files on the hard disk. The file expression format in Linux is very complex. Unlike windows, the file expression format in DOS is a unified aaaaaaa. the BBB format is so easy to find. In Windows, you only need to know the file name or suffix of the file to be searched. In Linux, the command for searching files is usually the "find" command. The "find" command can help us find the files we need in Linux routine transactions. For new Linux users, the "find" command is also a method to understand and learn the characteristics of Linux Files. Due to the wide variety of Linux releases and fast version upgrades, Linux books often state the location of a configuration file, which is often not available for new Linux users. For example, RedHat
Linux 7. O and RedHat in Linux 7.1, the hard disk location and file directory of some important configuration files have changed a lot. If you do not learn to use the "find" command, it is quite difficult to find a configuration file among thousands of Linux Files. I have suffered this kind of hardship before being proficient in the "find" command. The following describes in detail how to use and use the powerful "find" command.

Search by File Name:

This method is as easy to understand as searching for files in windows. If you put this file in a single folder, you can easily find it by using the common "ls" command, using the "find" command to search for it will not impress you. After all, the "find" command has more powerful functions than this. If you know the file name of a file, but do not know the folder to which the file is put, or even layers of embedded folders. For example, if you forget the directory in which the httpd. conf file is located, or even somewhere in the system, you can run the following command:

Find/-name httpd. conf

This command syntax looks easy to understand, that is, write-name directly after find, indicating that the system is required to search by file name, and finally write the target file name httpd. conf. Wait a moment and the system will display the search result list on the computer screen:
ETC/httpd/CONF/httpd. conf

This is the complete path of the httpd. conf file in Linux. Search successful.
If the system does not display the result after you enter the preceding SEARCH Command, do not assume that the system has not executed the find/-name httpd command. conf command, but it may be that the Apache server is not installed in your system. If you have installed the Apache web server, then use find/-name httpd. conf to find the configuration file.

No error search skills:

In Linux, the "find" command is a command that most system users can use. It is not the patent of the root system administrator. However, common users may encounter this problem when using the "find" command, that is, the system administrator root in Linux can set some file directories to the Access prohibited mode. In this way, normal users do not have the permission to use the "find" command to query these directories or files. When a common user uses the "find" command to query the directories of these files, the words "permissiondenied." (Access prohibited) appears. The system will not be able to query the files you want. To avoid such errors, we tried to find the file and Input
Find/-name access_log 2>/dev/null

This method transfers the search error prompt to a specific directory. After the system executes this command, the error information is directly transmitted to stderrstream 2. access_log 2 indicates that the system will send the error information to stderrstream 2, /dev/null is a special file that indicates null or error information. In this way, the queried error information will be transferred and will not be displayed.

In Linux, you may encounter such a problem when searching for files. If we look for a file on the entire hard disk, it will take a long time to find a file in this system, especially for large Linux systems and large-capacity hard disks, when the file is placed in a very deep directory. If we know that this file is stored in a large directory, it can save a lot of time as long as we find it in this directory. Use Find/etc-name httpd. conf to solve this problem. The preceding command is used to query the httpd. conf file in the etc directory. Here we will describe the meaning of the "/" function symbol. If "find/" is input, it indicates that the Linux system is required to search for files in the entire root directory, that is, find the file on the entire hard disk, and "find/etc" is only
Find Files in the etc directory. Because "find/etc" indicates that files are only searched in the etc directory, the search speed is much faster.

Method for searching based on some file names:
This method is the same as finding known file names in windows. However, in Linux, searching for files based on some file names is much more powerful than similar searching methods in windows. For example, if we know that a file package contains the three letters of SRM, We can find all the files in the system that contain the three letters. Enter:

Find/etc-name '* SRM *'

This command indicates that the Linux system will find all files containing the three letters SRM in the/etc directory, such as absrmyz and tibc. SRM and other qualified files can be displayed. If you know that the file is prefixed by the SRM letters, you can also omit the asterisks. The command is as follows:

Find/etc-name 'srm *'

Only files such as srmyz are found. files such as absrmyz and absrm do not meet the requirements and are not displayed. This greatly improves the efficiency and reliability of file searching.

Query methods based on file features:

If you only know the size of a file, modify the date and other features, you can also use the "find" command to find out, which is basically the same as the "Search" function in windows. In Microsoft's "Search" in windows, "search assistant" makes searching for files and folders, printers, users, and other computers on the network easier. It makes searching on the Internet easier. The search assistant also includes an index service that maintains the indexes of all files in the computer, making the search faster. When you use "search assistant", you can specify multiple search criteria. For example, you can search for files and folders by name, type, and size. You can even search for files containing specific text. If you are using
Active Directory. You can also search for a printer with a specific name or location.

For example, if we know that the size of a Linux File is 1,500 bytes, we use the following command to query find/-size 1500c. Character C indicates that the size of the file to be searched is measured in bytes. If we do not know the specific size of this file, we can perform fuzzy search in Linux. For example, if we enter the find/-size + 0000000c command, it indicates that we specify the system to find the file larger than 10000000 bytes in the root directory and display it. The "+" command requires the system to list only files larger than the specified size, and the "-" command requires the system to list files smaller than the specified size. The following list shows how to use different
Find "command after the system to make the search action, we can easily see that in Linux, the use of the" find "command is a lot of ways, the" find "command to find files as long as flexible application, you do not have to find poor performance in windows.

Find/-Amin-10 # search for files accessed in the last 10 minutes in the system
Find/-atime-2 # search for files accessed in the last 48 hours in the system
Find/-empty # search for files or folders that are empty in the system
Find/-group cat # Find the groupcat file in the system
Find/-mmin-5 # search for the files modified in the last 5 minutes in the system
Find/-mtime-1 # search for files modified in the last 24 hours in the system
Find/-nouser # search for files belonging to the void user in the system
Find/-user Fred # search for files belonging to the user Fred IN THE SYSTEM

The following list is some of the conditions for searching the features of files that can be specified by the find command. No search conditions are listed here. You can find all the search functions of the find command in related Linux books.
-Amin n
Find the files accessed in the last n minutes of the system
-Atime n
Find the last n * 24 hours of files in the system
-Cmin n
Searches for the files in the last n minutes that have been changed.
-Ctime n
Searches for the last n * 24 hours of files in the system that have been changed.
-Empty
Search for Blank files, empty file directories, or folders without subdirectories in the system.
-False
Find files that are always wrong in the system
-Fstype type
Search for files in the specified file system, for example, ext2.
-Gid n
Find the file whose ID is N in the system.
-Group gname
Find the files in the system that belong to the gnam file group and specify the group and ID.
Description of the control options of the find command:
The find command also provides some special options for users to control the search operation. The following table lists the basic control options and usage of the most commonly used find command.
Option
Purpose description
-Daystart
. Test the system's files within 24 hours from today. usage is similar to-Amin
-Depth
Using the depth-level search process, you can first find the file content in the specified directory at a layer.
-Follow
Follow the wildcard link query method. In addition, you can ignore the wildcard link query method.
-Help
Show command Summary
-Maxdepth levels
Search for directories at a certain level by the descending Method
-Mount
It is not found in the file system directory. The usage is similar to-xdev.
-Noleaf
Do not optimize searches in non-unux file systems, MS-DOS systems, CD-ROM file systems
-Version
Print version numbers
  

After the-follow option is used, the find command follows the wildcard Link Method for search. Unless you specify this option, the find command will generally ignore the wildcard Link Method for file search.

-The purpose of the maxdepth option is to restrict the "find" command to search for files in a directory in descending mode, or to search for directories that exceed a certain level. This slows down the search speed, the search takes too much time. For example, to find a file named Fred in the subdirectory of the current (.) Directory tip, run the following command:

Find.-maxdepth 2-name Fred

If the Fred file is in the./sub1/Fred directory, the command will directly locate the file and it is easy to find the file. If the file is in the./sub1/sub2/Fred directory, this command cannot be found. Because the maximum Query Directory level of the find command in the directory is 2, you can only find files under the 2-layer directory. The purpose of this operation is to allow the find command to locate files more accurately. If you already know the level of the file directory where a file is located, then, the added-maxdepth N can be quickly searched in the specified directory.

Search for files using hybrid search
The find command can use a hybrid search method. For example, we want to find a file larger than 100000000 bytes in the/tmp directory and modify it within 48 hours, we can use-and to link two search options into a hybrid search method.
Find/tmp-size + 0000000c-and-mtime + 2

Anyone who has studied computer languages knows that in computer languages, they use and or to indicate the relationship between "and" and "or. Search commands in Linux are also common.
In addition,
Find/-user Fred-or-user George

We can interpret it as finding files belonging to both Fred and George in the/tmp directory.
You can also use the "Non" link in the "find" command to find files. If you want to find all files that do not belong to panda in the/tmp directory, use a simple
Find/tmp! -User panda
Command to solve the problem. Very simple.

How to find and display files
Finding a file is our purpose. We 'd like to know the details and attributes of the file. If we look for the file, it is quite tedious to use the LS command to view the file information. Now we can use these two commands together.

Find/-name "httpd. conf"-ls

After finding the httpd. conf file, the system displays the httpd. conf file on the screen.
12063 34-RW-r -- 1 Root 33545 Dec 30/etc/httpd/CONF/httpd. conf

The following table lists common parameters and usage methods for searching and displaying file information.
Option
Purpose description
-Exec command;
Search for and execute commands
-Fprint File
Print the complete file name
-Fprint0 File
Print the complete file name, including Empty files
-Fprintf File Format
Print File Format
-OK command;
Run commands for the user and confirm the input according to user y.
-Printf format
Print File Format
-Ls
Print files of the same file format.

Conclusion: We have learned how to use the find command and listed many common find Command Options so far, if we are familiar with the use of the find command in Linux, it is not difficult to find files in Linux.

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.