Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Find Command in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "use of Find commands in Linux", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the use of Find commands in Linux" bar!

Each operating system is made up of thousands of different kinds of files. Among them, there are files that come with the system itself, users' own files, and shared files, and so on. We sometimes forget where a file is on our hard drive. It is quite easy to find a file in Microsoft's WINDOWS operating system. Just click "start"-"search" on the desktop and you can find all kinds of files and documents on your local hard disk, local area network, and even INTERNET in various ways.

However, users who use Linux are not so lucky, and it is really troublesome to find a file on Linux. After all, in Linux, we need to use a special "find" command to find files on the hard drive. The file expression format under Linux is very complex, unlike the unified AAAAAAA.BBB format under WINDOWS,DOS, which is so easy to find. In WINDOWS, it is very easy to find as long as you know the file name or suffix of the file you are looking for. The command to find files in Linux is usually a command, and the Linux Find command can help us to find out the files we need in the daily affairs of using and managing Linux. For novices to Linux, the Linux Find command is also a way to understand and learn the characteristics of Linux files. Because there are many Linux distributions and version upgrades are very fast, Linux books often specify the location of a configuration file, and often novice Linux users follow clues to find it or can't find it. For example, REDHAT Linux 7.o and REDHAT Linux 7.1where some important configuration files are located in the hard disk location and file directory have been greatly changed, if you do not learn to use the Linux Find command, then in thousands of Linux files to find one of the configuration files is very difficult, the author has not mastered the Linux Find command before the suffering. OK, here is a detailed introduction to all the uses and uses of the powerful Linux Find command.

Look up through the file name:

This method is as easy to understand as looking up 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 Linux Find command to find it will not impress you, after all, the Linux Find command is more powerful than that. If you know the file name of a file, but do not know which folder the file is placed in, or even layers of nesting folders. For example, if you forget which directory the httpd.conf file is in, or even somewhere on the system, you can use the following command:

Find /-name httpd.conf

The syntax of the Linux Find command seems easy to understand, just write-name after Linux Find, indicating that the system is required to look up the file name by file name, and * write the target file name httpd.conf. In a moment, 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 file httpd.conf 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 does not execute the find/-name httpd.conf command, but may not have an Apache server installed in your system, as long as you install the Apache Web server, and then use find/-name httpd.conf to find this configuration file.

Error-free finding techniques:

Linux Find command is a command that can be used by most system users in Linux system, and it is not the patent of ROOT system administrator. However, the average user may encounter this problem when using the Linux Find command, that is, the system administrator ROOT in the Linux system can set some file directories to disable access mode. In this way, the average user does not have permission to query these directories or files with the Linux Find command. When the average user uses the Linux Find command to query these file directories, the words "Permissiondenied." (forbidden) often appear. The system will not be able to query the file you want. To avoid such errors, we can use the method of transferring the error prompt to try to find the file and type

Find /-name access_log 2 > / dev/null

This method is to transfer the search error prompt to a specific directory. After the system executes this command, the error information will be sent directly to stderrstream 2. Access_log 2 indicates that the system will send the error message to stderrstream 2. / dev/null is a special file that indicates empty or error information, so the error message will be transferred and will not be displayed again.

Finding files in the Linux system will also encounter such a practical problem. If we look for a file on the entire hard drive, it will take a long time to find a file in this system, especially on large Linux systems and larger hard drives, where 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 by looking down in this directory. You can solve this problem by using find / etc-name httpd.conf. The above command is to query the httpd.conf file in the etc directory. Here again explain the meaning of the "/" function symbol, if you enter "find/" means that the Linux system is required to look for files in the entire ROOT directory, that is, to look for files on the entire hard disk, while "find/etc" is only looking for files in the etc directory. Because "find/etc" means to look for files only in the etc directory, the search speed is much faster.

Linux Find command: find the method based on some file names:

This method is the same as looking up a known file name in WINDOWS. However, the method of finding files based on partial filenames in Linux is much more powerful than similar methods in WINDOWS. For example, if we know that a file contains the letters srm, it is possible to find all the files in the system that contain these three letters. Enter:

Find / etc-name'* srm*'

This command indicates that the Linux system will search the entire / etc directory for all files containing the three letters srm, such as absrmyz, tibc.srm, and other eligible files can be displayed. If you also know that the file starts with the letters srm, we can also omit the first asterisk, as follows:

Find/etc-name 'srm*'

Only files like srmyz can be found, and files like absrmyz or absrm do not meet the requirements and are not displayed, so the efficiency and reliability of finding files are greatly enhanced.

Linux Find command: query method according to the characteristics of the file:

If you only know the size of a file, the modification date and other features can also be found using the Linux Find command, which is basically the same as the "search" function in the WINDOWS system. The "search assistant" in WINDOWS in Microsoft's search makes it easier to search for files and folders, printers, users, and other computers on the network. It even makes it easier to search on Internet. Search Assistant also includes an indexing service that maintains indexes of all files on your computer, making searches faster. When using search Assistant, 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 you are using Active Directory, you can also search for printers with a specific name or location.

For example, if we know that a Linux file size is 1500 bytes, we can use the following command to query find /-size 1500c, and the character c indicates that the size of the file we are looking for is in bytes units. If we don't even know the exact size of this file, we can also use fuzzy lookup in Linux to solve the problem. For example, if we enter the command find/-size + 10000000c, it indicates that our designated system looks up files greater than 10000000 bytes in the root directory and displays them. The "+" in the command requires the system to list only files larger than the specified size, while the use of "-" means that the system is required to list files that are smaller than the specified size. The following list is the search action to be made by the system after Linux uses a different "find" command, from which 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, there is no need to find poor ability in WINDOWS.

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

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

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

Find /-groupcat # finds files that belong to groupcat in the system

Find /-mmin-5 # find files that have been modified in the system within 5 minutes

Find /-mtime-1 # find files that have been modified in the system within 24 hours

Find /-nouser # finds files that belong to invalid users in the system

Find /-user fred # finds files that belong to the user FRED in the system

The following list is some of the criteria for finding the characteristics of a file that can be specified by the Linux Find command. Not all the search criteria are listed here. Refer to the relevant books on Linux to know the search functions of all Linux Find commands.

-amin n

Find the files accessed in * * N minutes in the system

-atime n

Find files in the system accessed by * 24 hours a day

-cmin n

Find the files in the system that have been changed in * * N minutes.

-ctime n

Find the files in the system that have been changed in 24 hours.

-empty

Find blank files in the system, or blank file directories, or folders that do not have subdirectories

-false

Find files in the system that are always wrong

-fstype type

Find files in the system that exist on the specified file system, for example: ext2.

-gid n

Find files in the system where the file number group ID is n

-group gname

Find files in the system that belong to the gnam filegroup and specify the group and ID

The control options for the Linux Find command are described:

The Linux Find command also provides users with some unique options to control the lookup operation. The following table summarizes the control options and usage of the most basic and commonly used Linux Find commands.

Option

Usage description

-daystart

. Test system files within 24 hours from today, usage similar to-amin

-depth

Use the depth-level search process to give priority to finding the contents of a file in a specified directory at a certain level

-follow

Follow the wildcard link search; in addition, you can also ignore the wildcard link query

-help

Show command summary

-maxdepth levels

Look up in a directory at a certain level according to the decreasing method

-mount

Do not look in the file system directory, the usage is similar to-xdev.

-noleaf

Disable * * search in non-UNUX file systems, MS-DOS systems, and CD-ROM file systems

-version

Print version number

After using the-follow option, the Linux Find command follows the wildcard link method to find the file. Unless you specify this option, the Linux Find command will generally ignore the wildcard link method for file search.

The function of the-maxdepth option is to restrict the Linux Find command from searching for text when it looks for files in a directory in a descending manner

Items exceed a certain level or search too many directories, which results in slower search speed and too much time to find. For example, we want to be in the current (.) To find a file called fred in the directory skill subdirectory, we can use the following command

Find. -maxdepth 2-name fred

If the fred file is in the. / sub1/fred directory, then the command will locate the file directly, and the search will be easy to succeed. If the file is in the. / sub1/sub2/fred directory, then this command cannot be found. Because the query directory level of the Linux Find command in the directory is 2, you can only find files in the 2-tier directory. The purpose of this is to make the Linux Find command locate the file more accurately. If you already know the approximate file directory level of a file, add-maxdepth n and you can quickly find it in the specified directory.

Success.

Use hybrid lookup to find files

The Linux Find command can use a hybrid lookup 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 link the two lookup options together to form a hybrid lookup method.

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

Friends who have studied computer language know that in computer language, and and or are used to express the relationship between "and" and "or" respectively. It is also common in the search command of the Linux system.

There are other examples like this.

Find /-user fred-or-user george

We can interpret this as looking for files belonging to fred or george in the / tmp directory.

You can also use the "not" relationship in the Linux Find command to find files. If we want to find all files that do not belong to panda in the / tmp directory, use a simple

Find / tmp!-user panda

The order can be solved. It's simple.

How to find and display files

Find a file is our purpose, we would like to know the details and attributes of the file found, if we take to find the file now, using the LS command to view the file information is quite tedious, now we can also use the two commands together.

Find /-name "httpd.conf"-ls

As soon as the system finds the httpd.conf file, it 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 shows some commonly used parameters and methods for finding and displaying file information.

Option

Usage description

-exec command

Find and execute commands

-fprint file

Print file full file name

-fprint0 file

Print file full file name including empty file

-fprintf file format

Print file format

-ok command

Execute the operation to the user command, according to the user's Y confirmation input

-printf format

Print file format

-ls

Print a file in the same file format.

Summary: so far we have learned a lot about the use of Linux Find commands, but also listed a lot of commonly used Linux Find command options, if we can master the use of Linux Find commands in Linux, then it is not difficult to find files in Linux.

Thank you for your reading, these are the contents of "the use of Find commands in Linux". After the study of this article, I believe you have a deeper understanding of the use of Find commands in Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report