码迷,mamicode.com
首页 > 系统相关 > 详细

Linux上查找

时间:2017-12-04 23:36:39      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:pts   localhost   span   find命令   backend   定位   打印   key   level   

locate

  用法:locate filename

  locate是Linux系统中的一个查找(定位)文件命令,和find命令等找寻文件的工作原理类似,但locate是通过生成一个文件和文件夹的索引数据库,当用户在执行locate命令查找文件时,它会直接在索引数据库里查找,若该数据库太久没更新或不存在,在查找文件时就提示:

“locate: can not open `/var/lib/mlocate/mlocate.db‘: No such file or directory”。

此时执行“updatedb”

更新下数据库即可。

[root@localhost keysystem]# updatedb
[root@localhost keysystem]# 

  用法示例:

[keysystem@localhost ~]$ locate a.txt 
/home/keysystem/a.txt
/usr/share/doc/sane-backends-1.0.21/matsushita/matsushita.txt
/usr/share/doc/vim-common-7.2.411/README_extra.txt
/usr/share/gnupg/help.ca.txt
/usr/share/gnupg/help.da.txt
/usr/share/gnupg/help.ja.txt
/usr/share/perl5/unicore/UnicodeData.txt
/usr/share/vim/vim72/doc/ft_ada.txt.gz
/usr/share/vim/vim72/doc/os_amiga.txt.gz
/usr/share/vim/vim72/doc/uganda.txt.gz
[keysystem@localhost ~]$ 

find

  find命令是一个无处不在命令,是Linux中最有用的命令之一。find命令用于:在一个目录(及子目录)中搜索文件,你可以指定一些匹配条件,如按文件名、文件类型、用户甚至是时间戳查找文件。下面就通过实例来体验下find命令的强大。

  用法:find

man文档中给出的find命令的一般形式为:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

其实[-H] [-L] [-P] [-D debugopts] [-Olevel]这几个选项并不常用(至少在我的日常工作中,没有用到过),上面的find命令的常用形式可以简化为:

find [path...] [expression]

  • path:find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录
  • expression:expression可以分为——“-options [-print -exec -ok ...]”
  • -options,指定find命令的常用选项,下节详细介绍
  • -print,find命令将匹配的文件输出到标准输出
  • -exec,find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为‘command‘ {  } \;,注意{   }和\;之间的空格 

find ./ -size 0 -exec rm {} \; 删除文件大小为零的文件 (还可以以这样做:rm -i `find ./ -size 0`  或 find ./ -size 0 | xargs rm -f &) 

find命令示例:

##查找当前目录下类型是文件的
[keysystem@localhost redirect]$ find . -type f
./out.put
./file
./file1
./file2
./b.txt
./files.txt
./a.txt
[keysystem@localhost redirect]$ 
##查找当前目录下类型是目录的
[keysystem@localhost redirect]$ find . -type d . ./hello ./world
##查找当前目录下类型是文件的  并用ls -l查看个文件   {}代表前面查找到的文件名
[keysystem@localhost redirect]$ find . -type f -exec ls -l ‘{}‘ ;
-rw-rw-r--. 1 keysystem keysystem 50 Dec  3 21:36 ./out.put
-rw-rw-r--. 1 keysystem keysystem 12 Dec  3 21:32 ./file
-rw-rw-r--. 1 keysystem keysystem 6 Dec  3 21:31 ./file1
-rw-rw-r--. 1 keysystem keysystem 6 Dec  3 21:31 ./file2
-rw-rw-r--. 1 keysystem keysystem 31 Dec  3 21:49 ./files.txt
##-printfind命令将匹配的文件输出到标准输出
[keysystem@localhost redirect]$ find . -type f -exec ls -l {} ; -print -rw-rw-r--. 1 keysystem keysystem 50 Dec 3 21:36 ./out.put ./out.put -rw-rw-r--. 1 keysystem keysystem 12 Dec 3 21:32 ./file ./file -rw-rw-r--. 1 keysystem keysystem 6 Dec 3 21:31 ./file1 ./file1 -rw-rw-r--. 1 keysystem keysystem 6 Dec 3 21:31 ./file2 ./file2 -rw-rw-r--. 1 keysystem keysystem 31 Dec 3 21:49 ./files.txt ./files.txt
##查找文件中包含hello的
[keysystem@localhost redirect]$ find . -type f -exec grep hello {} ; -print
hello
./file
hello
./file1
hello
./a.txt
##查找文件中包含Hello的 并打印出行号   -n 表示行号
[keysystem@localhost redirect]$ find . -type f -exec grep -n Hello {} ; -print
1:Hello
./b.txt
##查找文件中包含Hello的 并打印出行号   -n 表示行号 -i表示忽略大小写
[keysystem@localhost redirect]$ find . -type f -exec grep -ni Hello {} ; -print
1:hello
./file
1:hello
./file1
1:Hello
./b.txt
1:hello
./a.txt

 

 

happygrep

 

Linux上查找

标签:pts   localhost   span   find命令   backend   定位   打印   key   level   

原文地址:http://www.cnblogs.com/alsodzy/p/7979228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!