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

Introduction of Linux File query Command

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "Linux file query command introduction". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

Introduction to 0x01 query command

Introduction to 0x02 find command

Introduction to 0x01 query command

Before I introduce you, let's first look at a special variable-- PATH.

The path where the command is located is stored under the PATH. When we execute the command, we will first look up the path from front to back in the PATH variable, and then stop directly after finding one, so we need to put the path that we want to take effect first to the front, and use a colon to separate each path.

Which command

Through the which command, we can find the location of a command, which is queried through the PATH path

Whereis command

The whereis command can also search the path like which, but it also searches for some other content, which is also queried through the PATH path.

If you only want to search for binary commands, you need to add the-b parameter

Locate command

Locate can also query the path and help of the command, but it does not traverse from the PATH path. It is queried from mlocate.db, a database in Linux. The mlocate.db database is located in / var/lib/mlocate/mlocate.db. Because it shows too much, I use the-n parameter to specify the number of rows displayed.

Then let's give another example to make a query.

We create a balabala.txt file in the root directory

Then use the following command to query

Locate balabala

Why can't you find this file when it exists?

This is because locate searches in the mlocate.db database, but the file we just created is not in this database, so we need to use updatedb to update the database

After the update, you can query it normally. After querying, you can know that updatedb will automatically execute once a day.

But we don't usually use this order.

Introduction to 0x02 find command

Compared with the above commands, find is the slowest, because find is found from the hard drive, but the find command is also one of our most commonly used commands, and it is the quickest to find the files we want.

There is a rough formula for the use of the find command

Location to find by find [constraint] File name to be found

For example, look in the root directory for all the files whose names contain balabala

If you want to execute some commands on the query after it is completed, add the-exec parameter to it.

Find /-type f-name "* balabala*"-exec ls-l {}\

The-exec parameter is followed by the command command that needs to be executed, ending with a semicolon, but considering that the semicolon in each system may have different meanings, a backslash is added for escape.

The curly braces represent the content queried by the find command, and spaces are required on both sides of the curly braces

There is an equivalent way to write this command, which is to use pipes and xargs

Xargs has two functions, one is to group the content with the-n parameter, and the other is to get the output of the previous command and pass it as input to the following command, which sounds the same as the pipe character, but part of the command cannot be executed without the cooperation of xargs and pipe character.

Just take a look at the following example.

If there is no cooperation between the pipe character and the xargs, the following commands will be executed directly in the current directory, that is, the data from the pipe character will be ignored by ls, but will perform the original function as it wishes.

By the way, introduce the grouping function of xargs.

Let me give you another example to understand the above.

Let's look for all the files in the current directory that end in .txt, and then output their contents.

We can also look up the file according to the modification time and add the-mtime parameter.

To facilitate find's search by time, we use the following command to generate a batch of files

Let's first take a look at the date command.

If you want to change the time, we can use date-s to change the time, but you are absolutely not recommended to change it, which may lead to problems with some functions.

Then we generate a batch of files and create one file every day from October 15th to October 25th.

Note that the following time is based on the standard that today is October 20.

For n in 'seq 15 20`; do date-s "2019 Universe 10 Universe"; touch test_$n.txt;done

Let's move on to the time-by-time query of the find command

Query files created three days ago

Find. -type f-name "* .txt"-mtime + 3

Query the files created on the third day

Find. -type f-name "* .txt"-mtime 3

Query the files for the last three days

Find. -type f-name "* .txt"-mtime-3

These are the commonly used search commands, and if you need anything else, just check the help file.

This is the end of the introduction of Linux File query Command. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report