简述linux文件搜索 linuxsir_org.htm
来自「linuxSir 网站的精华文章」· HTM 代码 · 共 393 行 · 第 1/2 页
HTM
393 行
<P></P>
<P><FONT id=3 size=4><B><BR>3、在一个文件或输出中查找;<BR></B></FONT></P>
<P>有时我们为了管理服务器,可能要查看一些日志文件或管理指令的输出,并抽取出来;这时我们要用到more和grep、egrep、|(管道),如果要输出到一个文件中,
还要用到 > 。</P>
<P><B>比如我们查看/var/log/message 文件,并查找5月8号的日志;我们应该用如下的命令组合;</B></P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# cat /var/log/messages |grep
'May 8' |more</CODE></DIV>
<P></P>
<P><B>注意:</B>因为May和8之间有两个空格,所以得用''号括起来。</P>
<P>如果我们想把查看到的结果输出到一个文件中,应该用 > 输出到文件;</P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# cat /var/log/messages |grep
'May 8' > ~/message0508.txt<BR>[root@localhost ~]# more
~/message0508.txt</CODE></DIV>
<P></P>
<P>上面的例子,是我们首先用 cat 来查看/var/log/message 的内容,然后抽取带有 May 8字样的行,然后输出到用户家目录下的
message0508.txt文件中,然后再用more来查看message0508.txt文件内容;</P>
<P><B>从一个输出结果中查找</B></P>
<P>我们也可以从一个输出的结果中查找所需要的内容,请看下面的例子;</P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ls -lh<BR>总计
24M<BR>-rwxr-xr-x 1 root root 545 04-25 11:21 adduml02.sh<BR>-rwxr-xr-x 1
root root 545 2004-01-18 adduml.sh<BR>-rw-rw-rw- 1 root root 0 04-25 14:26
dood<BR>drwxr-xr-t 2 root root 4.0K 04-24 21:59 googledir<BR>-rwxr-xr-x 1
root root 7 04-21 12:47 lsfile.sh<BR>-rw-r--r-- 1 root root 31K 05-08
13:47 message0508.txt<BR>drwxr-xr-x 2 root root 4.0K 04-21 12:46
mkuml-2004.07.17<BR>-rwxr-xr-x 1 root root 67K 04-22 14:13
mkuml-2004.07.17-ananas.tar.bz2<BR>drwxr-xr-x 2 root 502 4.0K 04-25 09:08
mydir<BR>-rw-r--r-- 1 root root 7.9M 04-27 20:35 myfile.img<BR>-rw-r--r--
1 root root 4.0M 04-27 20:37 myfileSpaa<BR>-rw-r--r-- 1 root root 3.9M
04-27 20:37 myfileSpab<BR>-rw-r--r-- 1 root root 7.9M 04-27 20:38
newmyfile.img<BR>drwxrw-rw- 2 root root 4.0K 04-25 14:22
sundir<BR>drwxr-xr-x 2 root root 4.0K 04-25 09:20 testdir<BR>-rwxr-xr-x 1
root root 613 03-26 18:41 upgrade.log<BR>-rw------- 1 root root 4.0K 04-29
20:12 vsftpd.conf<BR>-rw-r--r-- 1 root root 4.0K 04-29 20:23
vsftpd.config<BR>-rw-r--r-- 1 root root 100 04-27 16:14 xaa</CODE></DIV>
<P></P>
<P>在ls -lh
的输出结果中,查看带有04-27字样的行,我们看到带有04-27字样的行都出来了。04-27在这里表示文件创建或最后修改(访问)的时间。符合这一特征的都在里面。</P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ls -lh |grep '04-27'
|more<BR>-rw-r--r-- 1 root root 7.9M 04-27 20:35 myfile.img<BR>-rw-r--r--
1 root root 4.0M 04-27 20:37 myfileSpaa<BR>-rw-r--r-- 1 root root 3.9M
04-27 20:37 myfileSpab<BR>-rw-r--r-- 1 root root 7.9M 04-27 20:38
newmyfile.img<BR>-rw-r--r-- 1 root root 100 04-27 16:14 xaa</CODE></DIV>
<P></P>
<P><B>查找正在运行的程序:</B></P>
<P>这也是从一个输出中查找的例子,先用ps 来列出所有正在运行中的进程,然后通过grep
来提取。下面的例子中查找是否有gaim程序在运行;</P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ps -aux |grep
gaim<BR>Warning: bad syntax, perhaps a bogus '-'? See
/usr/share/doc/procps-3.2.6/FAQ<BR>beinan 2682 0.0 4.0 152644 30188 ? S
08:59 0:16 gaim<BR>root 5660 0.0 0.0 5160 720 pts/1 S+ 13:58 0:00 grep
gaim</CODE></DIV>
<P></P>
<P>从上面的例子,我们可以看到的确有gaim运行,进程号是2682 。如果想杀掉gaim怎么办?应该有kill 2682 或killall
gaim </P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# kill
2682<BR>或<BR>[root@localhost ~]# killall gaim</CODE></DIV>
<P></P>
<P>对于进程的查找,也可以用pgrep 来进行;比如我们查找gaim;</P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# pgrep
gaim<BR>2682</CODE></DIV>
<P></P>
<P>等价于;</P>
<P>
<DIV class=codeblock><CODE>[root@localhost ~]# ps -aux |grep
gaim<BR>Warning: bad syntax, perhaps a bogus '-'? See
/usr/share/doc/procps-3.2.6/FAQ<BR>beinan 2682 4.4 2.3 105000 17504 ? S
14:05 0:02 gaim<BR>root 5716 0.0 0.0 5156 712 pts/1 R+ 14:06 0:00 grep
gaim</CODE></DIV>
<P></P>
<P><FONT id=4 size=4><B><BR>4、关于本文;<BR></B></FONT></P>
<P>关于查找的命令和工具说起来比较复杂,本文也仅仅是一个入门性的文档。如果只是通过文件名来查找到他位置,我感觉还是用locate好一点。有时find太费时间
;</P>
<P>这篇文章本来仅仅是写文件和目录的搜索的,后来我想到:可能有的弟兄要学一点指定关健字在一个文件或输出中查找。所以又写了一点在一个文件或输出中查找所需要的内容。看上去本文有点拼凑的感觉,是不是太随意了?</P>
<P><FONT id=5 size=4><B><BR>5、参考文档;<BR></B></FONT></P>
<P>man 和help </P>
<P><FONT id=6 size=4><B><BR>6、相关文档;<BR></B></FONT></P>
<P><A href="http://www.linuxsir.org/main/?q=node/198"><B>《Linux
文件内容查看工具介绍》</B></A><BR><A
href="http://www.linuxsir.org/main/?q=node/193"><B>《关于Linux
文件系统中路径的理解》</B></A><BR><A
href="http://www.linuxsir.org/main/?q=node/192"><B>《Linux
文件和目录管理之列出、删除、复制、移动及改名》</B></A><BR><A
href="http://www.linuxsir.org/main/?q=node/191"><B>《Linux 文件类型
及文件的扩展名》</B></A><BR><A
href="http://www.linuxsir.org/main/?q=node/189"><B>《简述Linux
文件系统的目录结构》</B></A><BR><A
href="http://www.linuxsir.org/main/?q=node/91"><B>《Linux
用户(user)和用户组(group)管理概述》</B></A><BR><A
href="http://www.linuxsir.org/main/?q=node/196"><B>《Linux
文件和目录的属性》</B></A></P>
<P><BR><BR><BR></P></DIV>
<DIV class=links>By 北南南北 at 2006/05/08 - 14:39 | <A
href="http://www.linuxsir.org/main/?q=taxonomy/term/1">Linux</A> | <A
href="http://www.linuxsir.org/main/?q=taxonomy/term/25">基础知识</A> | <A
title=共享你有关本文的思想和意见。
href="http://www.linuxsir.org/main/?q=comment/reply/208#comment">参与评论</A>
| 3203 阅读</DIV></DIV><A id=comment></A>
<FORM action=?q=comment method=post>
<DIV><INPUT type=hidden value=208 name=edit[nid]> <A id=comment-454></A>
<DIV class=comment>
<H3 class=title><A class=active
href="http://www.linuxsir.org/main/?q=node/208#comment-454">您好</A></H3>
<DIV class=content>
<P>有一些语法不通的地方,希望能修正一下。比如:which 和where 相似,只是我们所设置的环境变量中设置好的路径中寻找</P></DIV>
<DIV class=links>By Anonymous at 周一, 2006/05/22 - 10:10 | <A
href="http://www.linuxsir.org/main/?q=comment/reply/208/454">回复</A></DIV></DIV></DIV></FORM><!-- end content -->
<DIV id=footer>
<CENTER><A href="http://www.linuxsir.org/"><IMG
src="简述Linux文件搜索 LinuxSir_Org.files/logo.jpg"></A> <BR><A
href="http://www.miibeian.gov.cn/"><FONT color=blue
size=3><B>闽ICP备06025536号</B></FONT></A><BR>
<SCRIPT language=JavaScript
src="简述Linux文件搜索 LinuxSir_Org.files/cyberpolice.htm"></SCRIPT>
<BR><A href="http://www.linuxsir.org/main/?q=node/78"><FONT color=blue
size=3><B>© 2002-2006 LinuxSir.Org</B></FONT></A><BR></CENTER></DIV></TD>
<TD id=sidebar-right>
<DIV class="block block-block" id=block-block-2>
<H2 class=title>基础知识</H2>
<DIV class=content>
<UL>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/23">安装配置</A>
<LI><A
href="http://www.linuxsir.org/main/?q=taxonomy/term/1/25/">基础入门</A>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/22/">硬件解决</A>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/21">软件管理</A>
<LI><A
href="http://www.linuxsir.org/main/?q=taxonomy/term/35/48/">重要资源</A>
</LI></UL></DIV></DIV>
<DIV class="block block-block" id=block-block-4>
<H2 class=title>软件应用</H2>
<DIV class=content>
<UL>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/30">网络工具</A>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/32">图形图像</A>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/31">音乐视频</A>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/49">字体中文</A>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/33">软件其它</A>
</LI></UL></DIV></DIV>
<DIV class="block block-block" id=block-block-5>
<H2 class=title>网络服务器</H2>
<DIV class=content>
<UL>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/27">文件服务器<A>
<LI><A
href="http://www.linuxsir.org/main/?q=taxonomy/term/28">Web服务器</A>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/29">邮件服务器</A>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/37">数据库应用</A>
<LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/50">服务器其它</A>
</LI></UL></DIV></DIV>
<DIV class="block block-comment" id=block-comment-0>
<H2 class=title>最新评论</H2>
<DIV class=content>
<DIV class=item-list>
<UL>
<LI><A
href="http://www.linuxsir.org/main/?q=node/222#comment-624">错了</A><BR>7
min 37 sec 前
<LI><A
href="http://www.linuxsir.org/main/?q=node/222#comment-623">q</A><BR>27
min 14 sec 前
<LI><A
href="http://www.linuxsir.org/main/?q=node/222#comment-622">有些道理</A><BR>48
min 43 sec 前
<LI><A
href="http://www.linuxsir.org/main/?q=node/219#comment-621">"内置的摄像头也能用这个吗?"</A><BR>13
hours 53 min 前
<LI><A
href="http://www.linuxsir.org/main/?q=node/80#comment-620">终于知道了...</A><BR>23
hours 8 min 前
<LI><A
href="http://www.linuxsir.org/main/?q=node/106#comment-619">okok</A><BR>2
days 14 hours 前
<LI><A
href="http://www.linuxsir.org/main/?q=node/93#comment-618">那要除去阴影呢?</A><BR>3
days 19 hours 前
<LI><A
href="http://www.linuxsir.org/main/?q=node/227#comment-617">不错的机会</A><BR>4
days 6 hours 前
<LI><A
href="http://www.linuxsir.org/main/?q=node/221#comment-616">配置可执行程序的路径</A><BR>4
days 12 hours 前
<LI><A
href="http://www.linuxsir.org/main/?q=node/222#comment-615">修改hostname出现的问题</A><BR>5
days 2 hours
前</LI></UL></DIV></DIV></DIV></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?