Brief Introduction to Linux File Search

Source: Internet
Author: User
Article Title: Brief Introduction to Linux file search. Linux is a technology channel of the IT lab in China. Including desktop applications, Linux system management, kernel research, embedded systems, open source, and other basic categories 1. About search;

In a Linux file system, there are two types of search concepts: one is the search file name, the other is the search for the specified content in a file; the two types of search, we will give a brief introduction, but it is not necessarily extremely professional. Where to write;


2. Search for files by file name;

Some people often say where all files are stored. How can I find them? In fact, the object name is used to find the location of the object. The commonly used tools for searching the object name include find, locate, and where. Now let's briefly talk about these tools in the file system, you can find the file name to view its location;


2.1 find;

Find path-name file name

For example, search for the vsftpd. conf file in/etc;

[Root @ localhost ~] # Find/etc-name vsftpd. conf


For more details about find, see find and xargs commands in Linux.

Find is the most powerful search tool, and its usage is flexible, rather than simply finding a file name.


2.2 locate

Locate is a tool used to search for files by file name. It is also easy to use. For a locate application, you must first create an index database through updatedb before the application can be applied. If you have installed new software or stored new files, you must first run the updatedb command, to generate the latest index library.

Usage:

Locate file name

Run updatedb first;

[Root @ localhost ~] # Updatedb

For example, we need to find the location where the vsftpd. conf file is located;

[Root @ localhost ~] # Locate vsftpd. conf
/Etc/vsftpd. conf
/Home/beinan/vsftpd. conf
/Root/. vsftpd. conf. swp
/Root/vsftpd. conf
/Root/vsftpd. config
/Usr/share/doc/vsftpd-2.0.4/EXAMPLE/INTERNET_SITE/vsftpd. conf
/Usr/share/doc/vsftpd-2.0.4/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd. conf
/Usr/share/doc/vsftpd-2.0.4/EXAMPLE/VIRTUAL_USERS/vsftpd. conf
/Usr/share/logwatch/default. conf/logfiles/vsftpd. conf
/Usr/share/logwatch/default. conf/services/vsftpd. conf
/Usr/share/man/man5/vsftpd.conf.5.gz


2.3 whereis and which


2.3.1 where

Whereis is a binary file used to search for commands. It will also find its help file;

For example, if we don't know where the fdisk tool is, we can use whereis fdisk to find it;

[Root @ localhost ~] # Whereis fdisk
Fdisk:/sbin/fdisk/usr/share/man/man8/fdisk.8.gz

If we look for where grub is, we should use the following command to explain that grub is located in the/sbindirectory, and its helper file is grub.8.gz, that is, man grub;

[Root @ localhost ~] # Whereis grub
Grub:/sbin/grub/etc/grub. conf/usr/share/grub/usr/share/man/man8/grub.8.gz


2.3.2 which

Which is similar to where, but it is found in the set path of the environment variable. For example;

[Root @ localhost ~] # Which fdisk
/Sbin/fdisk


3. Search in a file or output;

Sometimes we may want to view some log files or management command output to manage servers and extract them. In this case, we need to use more and grep, egrep, | (pipeline ), if you want to output data to a file, you also need to use>.

For example, we can view the/var/log/message file and search for the logs generated in April May 8. We should use the following command combination;

[Root @ localhost ~] # Cat/var/log/messages | grep 'may 8' | more

Note: because there are two spaces between May and 8, you must enclose them with the ''sign.

If we want to output the result to a file, we should use> to output it to the file;

[Root @ localhost ~] # Cat/var/log/messages | grep 'may 8'> ~ /Message0508.txt
[Root @ localhost ~] # More ~ /Message0508.txt

In the above example, we first use cat to view/var/log/message content, and then extract rows with the words "May 8, output to the message0508.txt file in the user's home directory, and then use Moreto parse the message0508.txt file;

Search from an output result

We can also find the desired content from an output result. Please refer to the following example;

[Root @ localhost ~] # Ls-lh
Total 24 M
-Rwxr-xr-x 1 root 545 04-25 11:21 adduml02.sh
-Rwxr-xr-x 1 root 545 adduml. sh
-Rw-1 root 0 04-25 14:26 dood
Drwxr-xr-t 2 root 4.0 K 04-24 21:59 googledir
-Rwxr-xr-x 1 root 7 04-21 12:47 lsfile. sh
-Rw-r -- 1 root 31 K 05-08 13:47 message0508.txt
Drwxr-xr-x 2 root 4.0 K 04-21 mkuml-2004.07.17
-Rwxr-xr-x 1 root 67 K 04-22 mkuml-2004.07.17-ananas.tar.bz2
Drwxr-xr-x 2 root 502 4.0 K 04-25 mydir
-Rw-r -- 1 root 7.9 M 04-27 20:35 myfile. img
-Rw-r -- 1 root 4.0 M 04-27 20:37 myfileSpaa
-Rw-r -- 1 root 3.9 M 04-27 20:37 myfileSpab
-Rw-r -- 1 root 7.9 M 04-27 20:38 newmyfile. img
Drwxrw-rw-2 root 4.0 K 04-25 14:22 sundir
Drwxr-xr-x 2 root 4.0 K 04-25 09:20 testdir
-Rwxr-xr-x 1 root 613 03-26 upgrade. log
-Rw ------- 1 root 4.0 K 04-29 20:12 vsftpd. conf
-Rw-r -- 1 root 4.0 K 04-29 20:23 vsftpd. config
-Rw-r -- 1 root 100 04-27 16:14 xaa

In the ls-lh output result, check the rows with the 04-27 characters. We can see all the rows with the 04-27 characters. 04-27 indicates the time when the file was created or last modified (accessed. All that match this feature are in it.

[Root @ localhost ~] # Ls-lh | grep '04-27 '| more
-Rw-r -- 1 root 7.9 M 04-27 20:35 myfile. img
-Rw-r -- 1 root 4.0 M 04-27 20:37 myfileSpaa
-Rw-r -- 1 root 3.9 M 04-27 20:37 myfileSpab
-Rw-r -- 1 root 7.9 M 04-27 20:38 newmyfile. img
-Rw-r -- 1 root 100 04-27 16:14 xaa

Find the running program:

This is also an example of searching from an output. First, ps is used to list all running processes and then grep is used to extract them. In the following example, check whether the gaim program is running;

[Root @ localhost ~] # Ps-aux | grep gaim
Warning: bad syntax, perhaps a bogu '-'? See/usr/share/doc/procps-3.2.6/FAQ
Beinan 2682 0.0 4.0 152644 30188? S gaim
Root 5660 0.0 0.0 5160 720 pts/1 S + grep gaim

From the above example, we can see that there is indeed gaim running, and the process number is 2682. What if I want to kill gaim? There should be kill 2682 or killall gaim

[Root @ localhost ~] # Killed 2682
Or
[Root @ localhost ~] # Killall gaim

You can also use pgrep to search for processes. For example, you can search for gaim;

[Root @ localhost ~] # Pgrep gaim
2682

Equivalent;

[Root @ localhost ~] # Ps-aux | grep gaim
Warning: bad syntax, perhaps a bogu '-'? See/usr/share/doc/procps-3.2.6/FAQ
Beinan 2682 4.4 2.3 105000 17504? S gaim
Root 5716 0.0 0.0 5156 712 pts/1 R + grep gaim


4. About this article;

Search commands and tools are complicated to say. This article is just an entry-level document. If I find his location only by using the file name, I feel it is better to use locate. Sometimes finding takes too much time;

This article was originally intended only for searching files and directories. Later I thought: Some may want to learn how to specify the word "Guan" in a file or output. So I wrote another point to find the desired content in a file or output. It seems like this article is a bit patchwork, isn't it too casual?


5. references;

Man and help


6. related documents;

Introduction to Linux File Content viewing tools
Understanding about paths in Linux File Systems
Linux file and directory management: listing, deleting, copying, moving, and renaming
Linux File Types and file extensions
Brief Introduction to the directory structure of the Linux File System
Linux user and user group management overview
Linux file and directory attributes

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.