Brief introduction to Linux file Search __linux

Source: Internet
Author: User

Directory
1, about search;
2, search the file name to find files;

2.1 Find;
2.2 Locate
2.3 Whereis and which

2.3.1 where
2.3.2 which

3, in a file or output to find;
4, about this article;
5, reference documents;
6, related documents;


+++++++++++++++++++++++++++++++++++++++
Body
+++++++++++++++++++++++++++++++++++++++


1, about search;

In the Linux file system, there are two kinds of search concepts, one is search file name, the other is to search for the specified content in a file, both of these search, we are briefly introduced, but not necessarily very professional. Think of where to write;


2, search the file name to find files;

Some brothers often say where the files are, how I look for files. In fact, the meaning of this is to find the location of the file by file name, search file name, we commonly used tools have found, locate, where and so on. Now let's simply say that these tools in the file system, by looking up the file name of the method to view its location;


2.1 Find;

Find path-name filename


Examples: For example, search for vsftpd.conf files in/etc;

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



Find more detailed instructions please refer to: "Linux file Lookup command Find,xargs details"

Find is the most powerful search tool and is more flexible, rather than just looking for a filename.


2.2 Locate

Locate is a tool used to search for files through file names, and it is simpler to use. Locate application, first of all, through the updatedb to establish an index database, and then to apply; If you have new software installed or new files are stored, run the updatedb command to generate the latest index library.

Usage:

Locate file name


We have to run UpdateDB first;

[Root@localhost ~]# UpdateDB

For example: We want to find vsftpd.conf files are located in which location;

[Root@localhost ~]# Locate vsftpd.conf
/etc/vsftpd/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 that looks for commands, and also finds its help files;

For example, we do not know where the Fdisk tool is placed, we can use Whereis fdisk to find;

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


If we are looking for grub where, we should use the following command, stating that Grub is located in the/sbin directory, and that his help file is grub.8.gz, also known as 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 it is, only the set of the environment variables we set in the path of the search;

[Root@localhost ~]# which fdisk
/sbin/fdisk



3, in a file or output to find;

Sometimes in order to manage the server, we may want to view some log files or the output of management instructions, and extract it; At this time we need to use more and grep, egrep, | (pipe), if you want to output to a file, but also to use the >.

For example, we look at the/var/log/message file, and look for the May 8 log, we should use the following combination of commands;

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


Note: Because there are two spaces between May and 8, you have to enclose the ' number.

If we want to output the results of the search to a file, we should use the > output to the file;

[Root@localhost ~]# cat/var/log/messages |grep ' May 8 ' > ~/message0508.txt
[Root@localhost ~]# more ~/message0508.txt

The above example is that we first use cat to view the contents of the/var/log/message, then extract the line with May 8 and then output it to the user's home directory. Message0508.txt file, and then use more to view the contents of the Message0508.txt file;

Find from one output result

We can also find the desired content from an output result, please see the example below;

[Root@localhost ~]# LS-LH
Total 24M
-rwxr-xr-x 1 root root 545 04-25 11:21 adduml02.sh
-rwxr-xr-x 1 root root 545 2004-01-18 adduml.sh
-rw-rw-rw-1 root root 0 04-25 14:26 Dood
Drwxr-xr-t 2 root root 4.0K 04-24 21:59 googledir
-rwxr-xr-x 1 root 7 04-21 12:47 lsfile.sh
-rw-r--r--1 root root 31K 05-08 13:47 message0508.txt
Drwxr-xr-x 2 root root 4.0K 04-21 12:46 mkuml-2004.07.17
-rwxr-xr-x 1 root root 67K 04-22 14:13 mkuml-2004.07.17-ananas.tar.bz2
Drwxr-xr-x 2 root 502 4.0K 04-25 09:08 Mydir
-rw-r--r--1 root root 7.9M 04-27 20:35 myfile.img
-rw-r--r--1 root root 4.0M 04-27 20:37 Myfilespaa
-rw-r--r--1 root root 3.9M 04-27 20:37 myfilespab
-rw-r--r--1 root root 7.9M 04-27 20:38 newmyfile.img
Drwxrw-rw-2 root root 4.0K 04-25 14:22 sundir
Drwxr-xr-x 2 root root 4.0K 04-25 09:20 TestDir
-rwxr-xr-x 1 root root 613 03-26 18:41 upgrade.log
-RW-------1 root 4.0K 04-29 20:12 vsftpd.conf
-rw-r--r--1 root root 4.0K 04-29 20:23 vsftpd.config
-rw-r--r--1 root 04-27 16:14 XAA


In the LS-LH output, see the line with 04-27 words, we see the line with 04-27 words out. 04-27 represents the time when the file was created or last modified (accessed). The one that fits this trait is in it.

[Root@localhost ~]# ls-lh |grep ' 04-27 ' |more
-rw-r--r--1 root root 7.9M 04-27 20:35 myfile.img
-rw-r--r--1 root root 4.0M 04-27 20:37 Myfilespaa
-rw-r--r--1 root root 3.9M 04-27 20:37 myfilespab
-rw-r--r--1 root root 7.9M 04-27 20:38 newmyfile.img
-rw-r--r--1 root 04-27 16:14 XAA

To find a running program:

This is also an example of looking up from an output, using PS to list all the running processes and then extracting them through grep. The following example finds whether a Gaim program is running;

[Root@localhost ~]# Ps-aux |grep Gaim
Warning:bad syntax, perhaps a bogus '-'? See/usr/share/doc/procps-3.2.6/faq
Beinan 2682 0.0 4.0 152644 30188? S 08:59 0:16 Gaim
Root 5660 0.0 0.0 5160 720 pts/1 s+ 13:58 0:00 grep gaim


From the above example, we can see that there is a gaim run, the process number is 2682. What if you want to kill Gaim. There should be a kill 2682 or Killall Gaim.

[Root@localhost ~]# Kill 2682
Or
[Root@localhost ~]# Killall Gaim

The search for the process can also be done with pgrep, for example, we look for Gaim;

[Root@localhost ~]# pgrep Gaim
2682


equivalent to;

[Root@localhost ~]# Ps-aux |grep Gaim
Warning:bad syntax, perhaps a bogus '-'? See/usr/share/doc/procps-3.2.6/faq
Beinan 2682 4.4 2.3 105000 17504? S 14:05 0:02 Gaim
Root 5716 0.0 0.0 5156 712 pts/1 r+ 14:06 0:00 grep gaim



4, about this article;

The commands and tools for finding are complex to say, and this article is just an introductory document. If it's just a filename to find his location, I feel better to use locate. Sometimes find too time-consuming;

This article was originally just a search for files and directories, and then I thought: maybe some of the brethren have to learn a little bit. Specifies that the key word is found in a file or output. So it's written a little bit. Find what you need in a file or output. It seems that this article is a little piece of patchwork feeling, is not too casual.


5, reference documents;

Mans and Help


6, related documents;

Introduction to the Linux file content viewing tool
Understanding of paths in Linux file systems
List, delete, copy, move, and rename Linux file and directory management
"Linux file type and file extension"
A brief description of the directory structure of the Linux file system
Linux User (user) and group Management overview
"Linux Files and Directory Properties"

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.