Linux上查找

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 &) 
    为了用ls -l命令列出所匹配到的文件,可以把ls -l命令放在find命令的-exec选项中:find . -type f -exec ls -l {  } ; 
    在/logs目录中查找更改时间在5日以前的文件并删除它们:find /logs -type f -mtime +5 -exec rm {  } ;
  • -ok,和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。 
    find . -name "*.conf"  -mtime +5 -ok rm {  } ; 在当前目录中查找所有文件名以.LOG结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示

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

   在当前目录下,搜索文件名为a.txt的文件。

[keysystem@localhost ~]$ find . -name a.txt
./a.txt
./Desktop/redirect/a.txt
[keysystem@localhost ~]$

  在/home目录下,搜索文件名为a.txt的文件,不区分大小写。

[keysystem@localhost home]$ find /home -iname a.txt
/home/keysystem/a.txt
/home/keysystem/Desktop/redirect/a.txt
/home/keysystem/Desktop/redirect/A.txt
[keysystem@localhost home]$

  指定文件类型查找。

[keysystem@localhost ~]$ find . -type d -name hello
./hello
./Desktop/redirect/hello
[keysystem@localhost ~]$ find . -type f -name hello.txt
./Desktop/test/hello.txt
[keysystem@localhost ~]$ 

  指定文件权限查找。

[keysystem@localhost ~]$ find . -type f -perm 777
[keysystem@localhost ~]$ find . -type f -perm 755
./hello-2.10/configure
./hello-2.10/build-aux/depcomp
./hello-2.10/build-aux/missing
./hello-2.10/build-aux/do-release-commit-and-tag
./hello-2.10/build-aux/config.rpath
./hello-2.10/build-aux/mdate-sh
./hello-2.10/build-aux/test-driver

find和grep的使用权限是所有用户

find命令的作用是在目录中根据文件名搜索文件

find 列出当前目录及其子目录的所有文件和文件夹的完整路径。

find -name Help.java 在当前目录及其子目录中搜索文件名为Help.java的文件。

find . -name Help.java 在当前目录及其子目录中搜索文件名为Help.java的文件(同上)。

find / -name Help.java 在整个硬盘中搜索文件名为Help.java的文件。

find -perm 755 在当前目录及其子目录中查找指定权限的文件

find -type b 在当前目录及其子目录下查找块设备文件。

find -type d 在当前目录及其子目录下查文件夹。

find -type c 在当前目录及其子目录下查找字符设备文件。

find -type p 在当前目录及其子目录下查找管道文件。

find -type l 在当前目录及其子目录下查找符号链接文件。

find -type f 在当前目录及其子目录下查找普通文件。

find -type d -exec ls -l {} ; 查找当前目录及其子目录下的文件夹,并将查找结果以ls -l的方式展现。

find -type d -ok rm -rf {} ;查找当前目录及其子目录下的文件夹,并将查找结果依次执行rm -rf命令,但是在执行命令前会有确认提示。

grep命令的作用是在目录中根据文件内容搜索文件

grep Clock * 查找当前目录下的所有文件中包含Clock字符串的文件,不查找子目录

grep -r Clock * 查找当前目录下的所有文件中包含Clock字符串的文件,查找子目录

grep -nr Clock * 查找当前目录下的所有文件中包含Clock字符串的文件,查找子目录,并显示行号

find命令之(-atime,-ctime,-mtime)区别,参考:http://blog.csdn.net/li_ning_/article/details/51468980

happygrep

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