Linux file search commands

Source: Internet
Author: User

For commands related to Linux File Search, see table 2-7.

Table 2-7 commands for searching files in Linux

Command

Brief Chinese description

Program directory

More

Display a file or any output results by PAGE

/Bin

Less

A file is displayed on multiple pages and can be returned.

/Usr/bin

Whereis

Searching for file tools

/Usr/bin

Find

Searching for file tools

/Usr/bin

Locate

Searching for file tools

/Usr/bin

Grep

Search for a string content Tool

/Bin

Env

View Environment Settings

/Usr/bin

More [file name]

Display a file or any output results by PAGE

In fact, more is not used to search for files, but it is useful when looking for files.

Because more is mainly used to display the output result on the screen and stop the page once, for example, when we use the LS command to find a file starting with an X letter, when ls x * is issued, too many files are still listed. When a screen cannot be viewed, you can use the pipeline symbol and the more command:

Ls x * | more

It stops one screen and continues until you press the blank key. So more is like the position of the DOS dir command/P option. When more is the main character, it is used to display an article one page at a time. For example, if you want to view the xf86config file in/etc, you can run the following command:

More/etc/xf86config

In this way, we can continue to press the blank key to read the file slowly. However, because of the inherent design of more, if you have read this page and want to look back at the previous page, I am sorry, it is not feasible. You must start from scratch! Therefore, when using this command, you can simply discard it and use the VI, Pico, Joe, and other document editors to view text files!

Pico/etc/xf86config

At the end, few people used more. So more often works with ls to find files. It can be used more than a dozen times a day.

Therefore, it is believed that the commands related to searching for files that have lost the main stage are illegal, but they are reasonable and fit with the status quo.

Exercise:

Which of the following is more convenient to view a text file than Pico?

 

Less [File name]

A file is displayed on multiple pages and can be returned.

The less command is funny. When I name it, I deliberately fight with the more command. When you call it "more", I call it "less ", it's like you call black toothpaste, And I call white toothpaste. In fact, it has nothing to do with "more" and "less. It is mainly for improvement: More cannot look back!

The advantage of less is that you can go back at any time, and you can simply use the [pgup] key to flip up.

However, in our humble opinion, it is better to use the document editor to read the file. What's more, the less itself has up to 42 options, so why bother!

Therefore, we will not describe the options for your convenience. This command is currently only available in Linux, and is not available in other UNIX families.

Exercise:

(1) Which one is more convenient than reading a text file using Pico?

(2) Can the less command work with pipeline symbols like the more command, so that the LS listed file can be looked back?

Whereis file name

Searching for file tools

Whereis is a small and easy-to-use file search tool dedicated to finding executable programs, original programs, and user manuals.

For example, run the following command:

Whereis Bzip2

It will tell you that Bzip2 is put in/usr/bin. Generally, if you are sure that something is a program and whereis cannot be found, it indicates that the program has not been installed in the system. For example:

Whereis cjoe

It indicates that cjoe is not installed in the system, otherwise it should be found.

Exercise:

(1) Is there any xpaint program on your system? This program is not listed in the default Main Menu of KDE and gnome, but it does not necessarily mean it does not exist.

(2) Can I use the whereis command to find the xf86config setting file? Why?

 

Find [Directory to be searched] [representation]

Searching for file tools

Find is an advanced file search tool, unlike whereis ". But also because it is too advanced, complicated to many people are not skilled. We try to give a simple example.

The simplest format is as follows:

Find/-name my *-print

This means that you need to start from the bottom-layer main directory, find the file whose file name starts with my, and display it. The-print option is displayed. You can use it as a fixed project to be added.

However, it can also be time-based, for example:

Find/usr-atime 3-print

It will start from the/usr directory and find the files that have been retrieved from the memory in the last three days.

Find/usr-ctime 5-print

It will start from the/usr directory and find the files modified in the last five days.

Find/doc-user Jacky-name 'J * '-print

Start from the/doc directory and find the Jacky file whose name starts with J.

Find/DOC \ (-name 'ja * '-o--name 'ma *' \)-print

It will start from the/doc directory and find the file with the name starting with Ja or ma.

Find/doc-name '* Bak'-exec RM {}\;

It will start from the/doc directory, find the file whose name ends with Bak, and delete it. -The exec option indicates the execution, RM indicates the deletion command, {} indicates the file name, and "\;" indicates the end of the specified command.

Exercise:

Can I use the find command to find out where the xf86config file is located?

 

LocateFile Name

Searching for file tools

Locate is also a tool for searching for files, but it is not as complex as whereis but can only find program files and other files. It can be regarded as a "moderate way "!

The moderate approach is often the best choice for most people, as shown in 2-32.

Figure 2-32 comparison of 3 file search commands

Whereis cannot find the file. Find requires a large number of commands. It takes a long time to find the xf86config setting file, and it is easy to find it with locate!

Exercise:

(1) Use locate to find out if there is an inittab file?

(2) Use locate to find the. PCF file. If not, find the compressed. PCF. Z or. pcf.gz.

Grep [-Option] [String] [file name]

Search for a string content Tool

Sometimes, when we store a file, we randomly get a file name. Afterwards, we forget what the file name is, and cannot even remember the first letter at the beginning. If you still remember a special word in the file, you can use the grep command to find it.

For example, we want to find out which file mentioned "typographical" in the 200 files in a directory:

Grep Layout *. txt

The two files are found with this word. Then you can call it up to see which one is what you want. When searching, try to find special words. If you enter "then", "so", "no", "computer "...... This type of word is too common. It may have been found in hundreds of articles, and the meaning of using grep to find a file is lost.

Because the second article appears twice in this example, three rows are listed. In fact, only two files match. If you want it to appear only once if it meets the conditions, add the-L option, as shown in Figure 2-33.

Figure 2-33 Add the grep command after the-L option

Other common options include:

-N simultaneously lists the row number of the word in the article.

-C calculates the number of times this string appears.

-I does not care about Case sensitivity.

Exercise:

When we want to add a user to the system, we often worry about adding an existing user name. Can I avoid this problem if I use the grep command? How to avoid it?

 

Env

View Environment Settings

The env command can be used to set many environment variables, such as the terminal type, the browser used, and the home directory.

But now we focus on one of them: PATH environment variable.

Because the PATH environment variable is in charge of your command, it will go to the following directories to find your command program:

/Usr/bin

/Sbin

/Usr/sbin

/Usr/x11r6/bin

/Root/bin

When you use the Env command to list all the environment variables, you can see that there is a path setting, separate the preceding directories with semicolons, and then string them into a string. This is the "path" of the command ". The DOS system also has a path setting with the same name and similar meanings. From 2 to 34.

Figure 2-34 run the Env command

If you want to add/usr/bin/he to the path of the command to search for it and remove/root/bin, you can re-issue the command:

Env Path =/usr/bin;/sbin;/usr/x11r6/bin;/usr/bin/He

Of course, other env variables can also be changed. However, because of the wide scope, we only focus on changing the search path.

Exercise:

Use the Env command to change your default browser.

 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.