How to find files in linux-Linux general technology-Linux technology and application information. The following is a detailed description. -Name: required. Indicates that the system is required to search by file name.
General Format: find/(dirname)-name filename
Specific file name search method:
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
No error search method:
In Linux, the system administrator ROOT 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.
Fuzzy file name search method:
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 *'
This is only available for files like srmyz.
With or without searching for files:
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
Use and or to indicate the relationship between "and" and "or. 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 not belonging to panda in the/tmp directory, you can do it in a simple way.
Find/tmp! -User panda
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 usage description
-Exec command: Find and execute the command
-Fprint file: print the complete file name.
-Fprint0 file: print the complete file name, including an empty file.
-Fprintf file format: format of the printed file
-OK command: Execute the command for the user, and run the command according to user's Y.
-Printf format: print the file format
-Ls: displays files of the same file format
Query methods based on file features:
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 system in the last N minutes.
-Atime n: Find the last n * 24 hours of files in the system.
-Cmin n: Find the file whose status is changed in the last N minutes in the system.
-Ctime n: searches for files in the last n * 24 hours of the system that have been changed.
-Empty: searches for Blank files, empty file directories, or folders without subdirectories in the system.
-False: searches for files that are always wrong in the system.
-Fstype type: searches 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.
Example:
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
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.
Description of the control options of the Find command:
The Find command also provides some special options for users to control the search operation.
Option usage description
-The daystart test system starts to test files within 24 hours from today. The usage is similar to-amin.
-Depth uses the depth-level search process to prioritize searching for file content in a specified directory at a Layer
-Follow follows the wildcard link query method. In addition, you can ignore the wildcard link query method.
-Help: Display command Summary
-Maxdepth levels searches for directories at a certain level according to the descending method.
-Mount is not found in the file system directory. The usage is similar to-xdev.
-Noleaf prohibits optimal search in non-UNUX file systems, MS-DOS systems, and 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.
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.