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 mlocate to find Files in Linux system

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about how to use mlocate to find files in the Linux system. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Everything in a Linux system is a file, and there may be hundreds of thousands of files in a system. So how to find files quickly? If you need to find a specific configuration file, you can use mlocate to find the file, let's take a look at the specific use of mlocate.

If you have used some Unix-like machines, you must have used the find command. Without a doubt, it is very complex and powerful. The following is an example of searching only for symbolic links in a directory and ignoring files:

# find. -lname "*" you can do almost endless things with find commands, which is undeniable. The find command is nice and concise when it's easy to use, but it can also be complex. This is not necessarily because of the find command itself, but because it combines with xargs, you can pass a variety of options to adjust your output and delete the files you find.

Location, location, frustrating however, simplicity is usually the best choice, especially when a grumpy boss puts his arm around your shoulder and gossips about the importance of time. You're still vaguely guessing the path to a file you've never seen before, while your boss is sure it's somewhere in the crowded / var partition.

Take a closer look at mlocate. You may have noticed one of its close relatives: slocate, which securely (note that the prefix s stands for security) records relevant file permissions to prevent unprivileged users from seeing privileged files. In addition, there is an older, primitive locate command from which they originated.

Mlocate differs from other members of its family, including at least slocate, in that mlocate does not need to continuously rescan all file systems when scanning file systems. Instead, it merges the files it finds (note that the m stands for merging) with the existing file list, making it more efficient and lightweight with the help of system caching.

In this series of articles, we will take a closer look at mlocate (also known as locate because of its popularity) and examine how to quickly and easily adjust it to the way you want it to be.

Small and compact unless you often reuse complex commands, like me, you will eventually forget them and need to look for them when you use them. The advantage of the locate command is that you can quickly query the entire file system without worrying about your top-level directory, root directory, and path, and simply use the locate command.

You may have found that find commands are very disobedient in the past, making you often scratch your ears and scratch your cheeks. You know, that's what happens when you lose a semicolon or a special character that is not properly escaped. Now let's get out of this complex find command, relax, and take a look at this clever little command.

You may need to first check if it is on your system by running the following command:

For the Red Hat family:

# yum install mlocate for the Debian family:

There should not be any differences between # apt-get install mlocate distributions, but there are almost certainly subtle differences between versions. Watch out.

Next, we will introduce a key component of the locate command, called updatedb. As you may have guessed, this is the command to update the database of the locate command. The name is very intuitive.

This database is a list of files for the locate command I mentioned earlier. The list is saved in a relatively simple and efficient database. Updatedb runs periodically through cron tasks, usually at quiet times of the day. In listing 1 below, we can see the interior of the file / etc/cron.daily/mlocate.cron (the path to the file and its contents may vary from release to release).

#! / bin/sh nodevs=$ (renice + 19-p $$> / dev/null 2 > & 1 ionice-c2-n7-p $> / dev/null 2 > & 1 / usr/bin/updatedb-f "$nodevs" listing 1: how to trigger the "updatedb" command every day.

As you can see, mlocate.cron scripts use excellent nice commands to minimize impact on system performance. I haven't made it clear that this command runs at the set time every day (but if I remember correctly, the original locate command has something to do with your computer slowing down at midnight). This is because, on some "cron" versions, delay is now introduced to the overnight start time.

This may be due to the so-called "hippopotamus Thundering Herd of Hippos" problem. Imagine many computers (or hungry animals) waking up at the same time from a single or limited source demanding resources (or food). This can happen when all hippopotamus use NTP to set their watches (OK, this fable is too much, but put up with it). Imagine that every five minutes (like a "cron task"), they all ask for food or something.

If you don't believe me, take a look at the configuration file-the cron version called anacron in listing 2, which is the contents of the file / etc/anacrontab.

# / etc/anacrontab: configuration file for anacron # See anacron (8) and anacrontab (5) for details. SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 # period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts / etc/cron.daily 7 25 cron.weekly nice run-parts / etc/ Cron.weekly @ monthly 45 cron.monthly nice run-parts / etc/cron.monthly listing 2: how delays are brought in when running the "cron" task.

You can see the RANDOM_DELAY and "delay in minutes" columns from listing 2. If you are not familiar with this aspect of cron, you can find more here:

# man anacrontab otherwise, you can postpone it yourself if you like. There is a great web page (now more than a decade old) that discusses this issue in a very reasonable way. This site discusses how to use sleep to introduce a randomness, as shown in listing 3.

#! / bin/sh # Grab a random value between 0-240. Value=$RANDOM while [$value-gt 240]; do value=$RANDOM done # Sleep for that time. Sleep $value # Syncronize. / usr/bin/rsync-aqzC-- delete--delete-after masterhost::master / some/dir/ listing 3: introduce a randomly delayed shell script before triggering an event to avoid hippopotamus shock.

When referring to these (possibly surprising) delays, I mean the / etc/crontab or root user's own crontab file. If you want to change the time it takes to run the locate command, especially when disk access slows down, it's not too tricky. There may be a more elegant way to implement it, but you can also move the file / etc/cron.daily/mlocate.cron somewhere else (I use the / usr/local/etc directory), use the root user to add a record to the root user's crontab, and paste the following:

# crontab-e 33 3 * / usr/local/etc/mlocate.cron uses anacron instead of / var/log/cron and its old, rotational version, and you can quickly tell it when the last cron.daily task was triggered:

# ls-hal / var/spool/anacron what is the Linux system Linux is a free to use and freely spread UNIX-like operating system, is a POSIX-based multi-user, multi-tasking, multi-threading and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

The above is the editor for you to share the Linux system how to use mlocate to find files, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report