Brief Linux file search

Author: North-South-North
from: League of Legends Community
Abstract: This paper briefly describes how to search for files, one to view the file in which the location of the file name, the second is to find specific content in a file or output.

table of Contents

1, the search;
2, to find files by searching for the file name;

2.1 find ;
2.2 locate
2.3 whereis 和which

2.3.1 where
2.3.2 which

3, looking up in a file or output;
4, about the article;
5, reference document;
6, related documents;


+++++++++++++++++++++++++++++++++++++++
text
++++++++++ +++++++++++++++++++++++++++++


1, on the search;

In the Linux file system, search for concepts, there are two, one is to search for the file name, the other is the search for specific content in a file; these two searches, we have a brief introduction, but not necessarily very professional. Think of what to write where;


2, to find files by searching for the file name;

Some brothers often say where files are placed, how do I find the file? In fact, here that means that the file name to find the location of the file is located; search for file names, we used tools find, locate, where so on. Now we simply talk about these tools in the file system to view their position by looking at the file name of the method;


2.1 find ;

find  路径    -name  文件名

Example: such as search vsftpd.conf file in / etc;

[root@localhost ~]# find     /etc  -name vsftpd.conf



find more detailed information, refer to: "Linux file search command find, xargs detailed"

find is the most powerful search tool usage is also more flexible, rather than just looking for a file name this function.


2.2 locate

locate tool is used to search for files by file name, its use is relatively simple. locate the application, you must first establish by updatedb index database before you can apply; if you install new software or store a new file, it must first run the updatedb command to generate new index library.

usage:

locate 文件名

Our first run updatedb;

[root@localhost ~]# updatedb

For example: we are looking for 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 和which


2.3.1 where

whereis command is to find binary files, but also to find its help file;

For example, we do not know where to put fdisk tool, we can use whereis fdisk to find;

[root@localhost ~]# whereis fdisk
fdisk: /sbin/fdisk /usr/share/man/man8/fdisk.8.gz

If we look at where grub, you should use the following command, indicating that grub is located in / sbin directories, files his help 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 and where, but we set the environment variable is set to find a good path; for example;

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


3. Find a file or output;

Sometimes we have to manage the server, you may want to see some log file or command output management, and extracted; then we have to use more and grep, egrep, | (pipe), if you want to output to a file, but also with to>.

For example, we see the / var / log / message file, and look for the number of May 8 logs; we should be combined with the following command;

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

Note: Because there are two spaces between May and 8, the starting 'enclosed in quotes.

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

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

The above example is our first cat used to view the contents of / var / log / message, and then extract the line with the May 8 words, and then output to message0508.txt file in the user's home directory, and then use to view more message0508.txt file contents;

Find a output results

We can also find the required content from the results in an output, see the following example;

[root@localhost ~]# ls -lh
总计 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 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 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 root  100 04-27 16:14 xaa

In the output of the ls -lh in line with the view of 04-27 words, we see a row with a 04-27 word out. 04-27 here represents the time the file was created or last modified (access) of. In line with this feature are inside.

[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 root  100 04-27 16:14 xaa

Find programs that are running:

This is also an example of the output from the find, first with ps to list all processes that are running, and then extracted by grep. The following example to find out if there is 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 indeed a gaim run, the process number is 2682. If you want to kill gaim how to do? There should kill 2682 or killall gaim

[root@localhost ~]# kill 2682

[root@localhost ~]# killall gaim

For the discovery process, you can also use pgrep to; for example, we find 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, on paper;

Find commands and tools on speaking more complicated, this is only an introductory document. If only to find his location by file name, I still feel a little better locate. Sometimes find too time-consuming;

This article was originally written only search files and directories, but then I thought: Some brothers might want to learn a little bit to find the specified key words in a file or output. So wrote a little look at a file or output in the required content. This article looks a little patchwork of feeling, it is not too casual?


5, reference documentation;

man and help

Reproduced in: https: //www.cnblogs.com/licheng/archive/2008/02/18/1072015.html

Guess you like

Origin blog.csdn.net/weixin_34050427/article/details/92631116