How to quickly find files in Linux, how to find the path of a command file, and search according to the content of the file?

Introduction: Review of the last issue

    The previous chapter described several ways to use the find command to query files. If the server has been used for a long time, or a lot of services are deployed, in this case, using find to search for files will be very slow and consume a lot of money. resource. So in response to this problem, today we will learn another command that can quickly find a file, and what the principle of this command is, and also teach you how to find the file path where we input the command in linux.

1.locate command: locate file name

    The locate command is faster to search for files. Why? First, let's talk about the principle of this locate to find files.

    The reason why this locate command finds fast is that it does not search in our search range like find, nor does it operate on our hard disk, but in the database it stores. What is that database? Just like a file library that records operation logs, when we create a file, it will be saved to our database, so when we search for a file, we will only retrieve the database, thereby improving efficiency .

    We only use locate to search tomcat (Figure 1), and the speed is almost a second search. This locate database is regularly stored, if a file we just created can be searched? Let's try it. For example (Figure 2), a newly created folder today, and then use locate to find, the first query results are not available. The newly created file has not been saved to the database, so we can query it by updating the database. The command to update the database is updatedb .

    Here we also need to note that locate's database is not for all directories . Well, for example, we create a folder in the temporary directory /tmp, and try to see if we can find it (Figure 3). We create a folder in the /tmp directory and search after updating the database, but the result is not searched arrive. So everyone remember that locate to the database is not for all files in the directory.

    For file names with upper and lower case, you can use the -i option to ignore case search , which will not be demonstrated here.

    

2.which command: which needs to find the command

   Do you know that the commands we enter are generally stored in that directory? do not know? It's okay, I'll tell you hahahaha

      /bin, /usr/bin: The commands stored in this directory are commands that can be used by ordinary users.

      /sbin, /usr/sbin: Commands stored in this directory that can only be used by the root user.

    So how do we know where this command is stored? At this time, you can use the which command to find it. As shown in Figure 1, use which to find the file location of the rm and useradd commands. We can clearly see that rm can be used by all users, and there is also a line "rm=rm -i", which means that rm is given an alias, which we will introduce later. Like our useradd command is only used for the root user, because this command is stored in the /usr/sbin directory.

    Of course, the directories stored by these commands can also be searched using find, but the efficiency will be slower.

    

3.whereis command: whereis  needs to find the command

    What is this command for? This is also the location of the search command, but it is different from which. whereis can not only find out the file where a command is located, but also query the file location of the help documentation for the command. ( In fact, in the linux system, the corresponding help files will be provided for everyone to view, and this content will be introduced in the next chapter ).

   

4.grep command: grep search content file name

   The commands we have learned above are all searching for files, so this grep searches for the contents of files. For example, when I view a configuration file, there may be hundreds or even thousands of lines in the configuration file. Every time I view it, I need to turn pages or search for keywords. If you use the grep command, you can quickly see what you need to browse.

    Let's first browse the /etc/inittab file with more, and use grep to find the line containing the set keyword in each line. The result shows that there are 2 lines, which is the effect of the grep command.

    The grep command has two other knowledge points that everyone needs to master:

             First : how to remove redundant lines? What does that mean? For example, we view a configuration file, which contains a lot of comment information (generally # represents comments in the configuration file), but we don't want to see the comment information. Many of the comment information in the configuration file are in English. For me, see It's the same thing if you don't watch it, haha, don't watch it at all. We can use the -v option to filter

             Note : If we don't want to see the comment information, but some comments are written at the back, so we only need to remove the # at the beginning of the line, the syntax is as follows:

             "grep -v ^# /etc/inittab", ^: This means to remove only the lines starting with #.

             Second : If there is a problem with case and you can't find the line you need, you can use the -i option to ignore case to search.

                 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324427335&siteId=291194637