Every operating system is composed of thousands of different types of files. There are files that come with the system itself, the user's own files, shared files, etc. Sometimes we often forget where a certain file is placed on the hard drive. It is quite simple to find a file in Microsoft's WINDOWS operating system. Just click "Start" - "Search" on the desktop and you can search in various ways on the local hard disk, local area network, or even on the INTERNET. Various files and documents.
But users using Linux are not so lucky. Finding a file on Linux is indeed a troublesome thing. After all, in Linux we need to use a dedicated "find" command to find files on the hard disk. The file expression format under Linux is very complicated. It is not as easy to find as the unified AAAAAAA.BBB format under WINDOWS and DOS. In WINDOWS, it is very easy to find the file name or suffix of the file you want to find. The command to find files in Linux is usually the "find" command. The "find" command can help us find the files we need conveniently in daily tasks of using and managing Linux. For Linux novices, the "find" command is also a way to understand and learn the characteristics of Linux files. Because there are many Linux distributions and versions are upgraded quickly, Linux books often indicate the location of a certain configuration file, but Linux novices often cannot find it even if they search for it. For example, in REDHAT Linux 7.O and REDHAT Linux 7.1, the hard disk locations and file directories where some important configuration files are located have changed a lot. If you don’t learn to use the “find” command, then thousands of Linux files It is quite difficult to find one of the configuration files in . The author suffered from this before I was proficient in the "find" command. Okay, now I will introduce to you in detail all the usage methods and uses of the powerful "find" command.
Search method by file name:
This method is as easy to understand as searching for files under WINDOWS. If you put this file in a single folder, you can easily find it using the common "ls" command. Then using the "find" command to find it will not impress you. After all, "find" "The power of commands doesn't stop there. If you know the file name of a certain file, but don't know which folder the file is placed in, or even nested folders. For example, if you forget which directory the httpd.conf file is in the system, or even don’t know where it is somewhere in the system, you can use the following command:
find / -name httpd.conf
This The command syntax seems easy to understand. Just write -name directly after find, indicating that the system is required to search according to the file name, and finally write the target file name httpd.conf. After a while, the system will display a list of search results on the computer screen:
etc/httpd/conf/httpd.conf
This is the full path of the httpd.conf file in the Linux system. The search was successful.
If the system does not display the results after entering the above search command, then do not think that the system has not executed the find/ -name httpd.conf command, but it may be that the Apache server is not installed in your system. In this case, as long as you install the Apache Web server , and then use find / -name httpd.conf to find this configuration file.
Error-free search techniques:
In Linux systems, the "find" command is a command that can be used by most system users and is not exclusive to ROOT system administrators. However, ordinary users may also encounter such a problem when using the "find" command, that is, the system administrator ROOT in the Linux system can set certain file directories into access-denied mode. In this way, ordinary users do not have permission to use the "find" command to query these directories or files. When ordinary users use the "find" command to query these file directories, the words "Permissiondenied." (access prohibited) often appear. The system will not be able to query the file you want. In order to avoid such errors, we use the method of transferring error prompts to try to find files. Enter
find / -name access_log 2>/dev/null
This method is to transfer the search error prompts to a specific directory. After the system executes this command, the error information encountered will be directly sent to stderrstream 2. Access_log 2 indicates that the system will send the error information to stderrstream 2. /dev/null is a special file indicating that it is empty or wrong. information, so that the queried error information will be transferred and will not be displayed again.
Looking for files in a Linux system will also encounter such a practical problem. If we search for a certain file in the entire hard disk, it will take a long time in this system, especially in large Linux systems and hard disks with large capacity, when the files are placed in deeply nested directories. If we know that this file is stored in a large directory, we can save a lot of time just by searching down in this directory. Use find /etc -name httpd.conf to solve this problem. The above command means to query the httpd.conf file in the etc directory. Here is another explanation of the meaning of the "/" function symbol. If you enter "find/", it means that the Linux system is required to search for files in the entire ROOT directory, that is, to search for files on the entire hard disk, while "find/etc" means only Find files in etc directory. Because "find/etc" means only searching for files in the etc directory, the search speed is correspondingly much faster.
Search method based on part of the file name:
This method is the same as the method of searching for known file names in WINDOWS. However, the method of finding files based on part of the file name in Linux is much more powerful than the similar method in WINDOWS. For example, we know that a file contains the three letters srm, then it is possible to find all the files in the system that contain these three letters. Enter:
find /etc -name '*srm*'
This The command indicates that the Linux system will search for all files containing the three letters srm in the entire /etc directory. For example, absrmyz, tibc.srm and other files that meet the conditions can be displayed. If you also know that this file starts with the three letters srm, then we can also omit the first asterisk, the command is as follows:
find/etc -name 'srm*'
This is only like srmyz The files are found only when files like absrmyz or absrm do not meet the requirements and are not displayed. In this way, the efficiency and reliability of finding files are greatly enhanced.
Query method based on file characteristics:
If you only know the size of a file, modification date and other characteristics, you can also use the "find" command to find it, which is basically the same as the "search" function in the WINDOWS system. In Microsoft Search, the Search Assistant in Windows makes it easier to search for files and folders, printers, users, and other computers on the network. It even makes searching on the Internet easier. Search Assistant also includes an indexing service that maintains an index of all the files on your computer, making searches faster. When using the Search Assistant, users can specify multiple search criteria. For example, users can search files and folders by name, type and size. Users can even search for files containing specific text. If you are using Active Directory, you can also search for printers with a specific name or location.
For example, we know that the size of a Linux file is 1,500 bytes, then we can use the following command to query find / -size 1500c. The character c indicates that the size of the file to be found is in bytes. If we don't even know the specific size of this file, we can also use fuzzy search in Linux to solve the problem. For example, if we enter the command find/ -size +10000000c, it means that we specify that the system will find files larger than 10000000 bytes in the root directory and display them. The "+" in the command indicates that the system is required to list only files larger than the specified size, while using "-" indicates that the system is required to list files smaller than the specified size. The following list is the search action that the system will perform after using different "find" commands in Linux. From it, we can easily see that there are many ways to use the "find" command in Linux. As long as the "find" command is used flexibly to find files, There is no need to have poor search ability in WINDOWS.
find / -amin -10 # Find files accessed in the last 10 minutes in the system
find / -atime -2 # Find files accessed in the last 48 hours in the system
find / -empty # Find files that are empty in the system File or folder
find / -group cat #Find files belonging to groupcat in the system
find / -mmin -5 #Find files modified in the last 5 minutes in the system
find / -mtime -1 #Find files in the system Files modified in the last 24 hours
find/-nouser #Find files belonging to the invalid user in the system
find/-user fred #Find files belonging to the user FRED in the system
The following list is for find The command can specify some conditions for searching the characteristics of the file. Not all search conditions are listed here. You can learn about the search functions of all find commands by referring to relevant books on Linux.
-amin n
Find files accessed in the last N minutes in the system
-atime n
Find files accessed in the last n*24 hours in the system
-cmin n
Find files whose status has been changed in the last N minutes in the system
-ctime n
Find files whose status has been changed in the last n*24 hours in the system
-empty
Find blank files in the system, or blank file directories, or folders without subdirectories in the directory
-false
Find files that are always wrong in the system
-fstype type
Find files that exist in the specified file system in the system, For example: ext2 .
-gid n
Find files in the system whose file number group ID is n
-group gname
Find 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 users with some unique options to control the search operation. The following table summarizes the most basic and commonly used control options of the find command and their usage.
Options
Usage description
-daystart
. Test the system’s files within 24 hours starting from today. The usage is similar to -amin
-depth
Use the depth level search process method to prioritize the search for file contents in a specified directory at a certain level
-follow
Follow the wildcard link method to search; In addition, you can also ignore the wildcard link method to query
-help
Display command summary
-maxdepth levels
Search in a certain level of directories in descending order
-mount
Do not search in the file system directory, Usage is similar to -xdev.
-noleaf
Disable optimal search in non-UNUX file systems, MS-DOS systems, CD-ROM file systems
-version
Print version number
After using the -follow option, the find command is Follow the wildcard link method to search. Unless you specify this option, the find command will generally ignore the wildcard link method when searching for files.
The function of the -maxdepth option is to restrict the find command from searching for files exceeding a certain level or searching too many directories when searching for files in a descending manner in a directory. This will cause the search speed to slow down and the search to take too much time. For example, if we want to find a file named fred in the subdirectory of the current (.) directory, we can use the following command
find . -maxdepth 2 -name fred
If the fred file is in the ./sub1/fred directory , then this command will directly locate the file, and the search can easily succeed. If this file is in the ./sub1/sub2/fred directory, then this command cannot find it. Because the find command has been given a maximum query directory level of 2 in the directory before, it can only find files in the 2-level directory. The purpose of this is to allow the find command to locate files more accurately. If you already know the approximate file directory level where a certain file is located, then adding -maxdepth n will quickly find it successfully in the specified directory.
Use mixed search method to find files
The find command can use mixed search method. For example, if we want to find a file in the /tmp directory that is larger than 100000000 bytes and modified within 48 hours, we can use -and to combine the two Search options are linked together into a hybrid search method.
find /tmp -size +10000000c -and -mtime +2
Friends who have studied computer language all know that in computer language, and and or are used to express the relationship of "and" and "or" respectively. It is also common in the search command of Linux system.
There is another example,
find / -user fred -or -user george
We can interpret it as finding files belonging to the two users fred or george in the /tmp directory.
You can also use the "not" relationship to find files in the find command. If we 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.
Methods to find and display files
Finding a file is our purpose. We would also like to know the detailed information and attributes of the found file. If we find the file now, it is quite easy to use the LS command to view the file information. Cumbersome, now we can also use these two commands together.
find / -name "httpd.conf" -ls
After the system finds the httpd.conf file, it immediately displays the httpd.conf file information on the screen.
12063 34 -rw-r--r-- 1 root root 33545 Dec 30 15:36 /etc/httpd/conf/httpd.conf
The following table is some commonly used parameters and uses for finding files and displaying file information. Method
Option
Usage description
-exec command;
Find and execute the command
-fprint file
Print the complete file name of the file
-fprint0 file
Print the complete file name of the file including empty files
-fprintf file format
Print the file format
-ok command;
Give the user a command to perform the operation, and execute it according to the user's Y confirmation input
-printf format
Print the file format
-ls
Print files in the same file format.
Summary: So far we have learned a lot about how to use the find command, and also listed many commonly used find command options. If we can master the use of the find command in Linux, then find in Linux Documentation is not a difficult thing either.