Linux学习笔记-文件搜索命令

搜索会占用大量资源,不建议使用搜索。

服务器规划要做好,各种数据软件存储位置都规划好,方便查找,而不用搜索

1.7.1.find

find [搜索范围] [匹配条件]

 

文件搜索

1.7.1.1.文件名精确搜索

-name 根据文件名搜索,精确搜索,只有文件名相等才能搜索出来

 

[root@localhost cn]# find /etc -name init

/etc/sysconfig/init

/etc/selinux/targeted/active/modules/100/init

[root@localhost cn]#

 

 

1.7.1.2.星号通配符

以上精确搜索,很多名字不易记全,因此需要模糊搜索

 

通配符:星号* 匹配任意多个任意字符

查找所有以init开头的文件

[root@localhost cn]# find /etc -name init*

/etc/sysconfig/network-scripts/init.ipv6-global

/etc/sysconfig/init

/etc/init.d

/etc/rc.d/init.d

/etc/inittab

/etc/selinux/targeted/active/modules/100/init

/etc/selinux/targeted/contexts/initrc_context

[root@localhost cn]#

 

 

 

1.7.1.3.问号通配符

 

通配符?,匹配单个任意字符

 

以init开头,并且后面有3个字符

[root@localhost cn]# find /etc -name init???

/etc/inittab

[root@localhost cn]#

 

 

1.7.1.4.不区分大小写

 

 

-iname

 

[root@localhost cn]# find /etc -iname init???

/etc/inittab

[root@localhost cn]#

 

 

1.7.1.5.文件大小查找

 

-size 根据文件大小查找

size的单位是块

100MB是多少?

1个数据块=512字节=0.5KB

100MB=102400KB=204800块

 

1KB=2块

 

 

+n大于  -n小于  =n等于

 

查找大于100MB的文件

[root@localhost cn]# find / -size +204800

/proc/kcore

find: ‘/proc/1503/task/1503/fd/6’: No such file or directory

find: ‘/proc/1503/task/1503/fdinfo/6’: No such file or directory

find: ‘/proc/1503/fd/6’: No such file or directory

find: ‘/proc/1503/fdinfo/6’: No such file or directory

/usr/lib/locale/locale-archive

 

 

 

1.7.1.6.根据所有者查找

 

-user 根据所有者查找

 

[root@localhost cn]# find ./ -user root

./

./yum.log

./test

./test/log.log

./test1

./test2.log

./test3

[root@localhost cn]#

 

 

1.7.1.7.时间属性查找

 

 

-amin 访问时间access

-cmin 文件属性change

-mmin 文件内容modify

 

 

find /etc –cmin 5

5分钟内被修改过属性的文件和目录

 

1.7.1.8.复合条件and or

 

-a 表示and 两个条件都满足

 

大于50MB,并且小于100MB的文件

find /etc –size +102400 –a –size 204800

 

 

-o 表示 or 两个条件满足任意一个即可

 

 

1.7.1.9.文件类型

 

-type 根据文件类型查找

 

f 文件 d 目录  l 软链接文件

 

[root@localhost cn]# find ./ -type d

./

./test

./test1

./test3

[root@localhost cn]# find ./ -type f

./yum.log

./test/log.log

./test2.log

[root@localhost cn]#

 

 

1.7.1.10.对搜索结果执行命令再处理

 

-exec 命令 {} \;

{}表示前面查找的结果

\转义

;表示结束

 

好像查找结果不正确?

[root@localhost cn]# find /tmp/cn -iname 'test*' -exec ls -l {} \;

total 0

-rw-r--r--. 1 root root 0 Apr 24 20:44 log.log

total 0

-rw-r--r--. 1 root root 0 Apr 24 21:10 /tmp/cn/test2.log

total 0

[root@localhost cn]#

 

 

-ok 命令 {} \;

这个会确认提示

 

好像查找结果不正确?

 

[root@localhost cn]# find /tmp/cn -iname 'test*' -ok ls -l {} \;

< ls ... /tmp/cn/test > ? y

total 0

-rw-r--r--. 1 root root 0 Apr 24 20:44 log.log

< ls ... /tmp/cn/test1 > ? y

total 0

< ls ... /tmp/cn/test2.log > ? y

-rw-r--r--. 1 root root 0 Apr 24 21:10 /tmp/cn/test2.log

< ls ... /tmp/cn/test3 > ? y

total 0

[root@localhost cn]#

 


 

多文件查找时,参数需要加单引号:

[root@localhost cn]# find /tmp/cn -iname test*

find: paths must precede expression: test1

Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

[root@localhost cn]# find /tmp/cn -iname 'test*'

/tmp/cn/test

/tmp/cn/test1

/tmp/cn/test2.log

/tmp/cn/test3

[root@localhost cn]#

 

 

1.7.1.11.i节点查找

-inum 根据i节点查找

 

[root@localhost cn]# ls -i

    5598 test  16777673 test1       201 test2.log  33575346 test3      5597 yum.log

[root@localhost cn]# ls -il

total 4

    5598 drwxrwxrwx. 2 root root 21 Apr 24 20:44 test

16777673 drwxr-xr-x. 2 root root  6 Apr 24 21:10 test1

     201 -rw-r--r--. 1 root root  0 Apr 24 21:10 test2.log

33575346 drwxr-xr--. 2 root root  6 Apr 24 21:17 test3

    5597 -rw-rwx--x. 1 root root 27 Apr 23 21:58 yum.log

[root@localhost cn]# find . -inum 201

./test2.log

[root@localhost cn]# find . -inum 201 -exec ls -l {} \;

-rw-r--r--. 1 root root 0 Apr 24 21:10 ./test2.log

[root@localhost cn]#

 

 

 1.8.其他文件搜索命令

 

1.8.1.locate

 

安装mlocate

locate命令在安装mlocate中,所以需要安装mlocate

 

[root@localhost ~]# locate init

-bash: locate: command not found

[root@localhost ~]# yum list mlocate

Loaded plugins: fastestmirror

Loading mirror speeds from cached hostfile

 * base: mirrors.cn99.com

 * extras: mirrors.aliyun.com

 * updates: mirrors.aliyun.com

Available Packages

mlocate.x86_64                                   0.26-6.el7                                    base

[root@localhost ~]# yun install mlocate

-bash: yun: command not found

[root@localhost ~]# yum install mlocate

Loaded plugins: fastestmirror

base                                                                        | 3.6 kB  00:00:00

extras                                                                      | 3.4 kB  00:00:00

updates                                                                     | 3.4 kB  00:00:00

省略……………………………………………………….

Complete!

[root@localhost ~]# locate init

locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory

 

安装完成后,命令还是搜索不到内容,是因为资料库需要更新:

[root@localhost ~]# updatedb

[root@localhost ~]# locate init

/boot/initramfs-0-rescue-ee8e847af1004327a6a44a0a5a957248.img

/boot/initramfs-3.10.0-693.el7.x86_64.img

/boot/initrd-plymouth.img

/dev/initctl

 

 

 

locate搜索速度很快,他是在文件资料库中查找文件,所以速度很快。

语法:locate 文件名

 

locate自己维护的资料库:/var/lib/mlocate/mlocate.db

系统中所有的文件都会定期更新到这个文件资料库中。

 

存在的问题,新建的文件还未收录到资料库中的时间,是搜索不出来的。

 

更新文件资料库的命令:

updatedb

 

-i 不区分大小

locate –i init

 

[root@localhost opt]# locate china

/opt/china.log

[root@localhost opt]# locate -i china

/opt/China.log

/opt/china.log

 

[root@localhost opt]#

 

1.8.2.which

命令所在路径:/usr/bin/which

 

搜索命令所在目录及别名信息

 

[root@localhost opt]# which ls

alias ls='ls --color=auto'

        /usr/bin/ls

[root@localhost opt]# which locate

/usr/bin/locate

[root@localhost opt]#

 

查找命令别名

rm是rm –i的别名

当我们删除文件的时候一般会有提示,是因为-i起作用。

  -i  prompt before every removal

 

[root@localhost opt]# which rm

alias rm='rm -i'

        /usr/bin/rm

 

 

1.8.3.whereis

查找命令所在的目录及帮助文档路径,类似which

 

 

红色部分为文档路径

[root@localhost opt]# whereis rm

rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz

 

 

1.8.4.grep

在文件内容中搜索字串匹配的行并输出

-i 不区分大小写

-v 排除指定字符串

 

语法:grep –iv [指定字串] [文件]

 

 

[root@localhost opt]# grep to /etc/inittab

# multi-user.target: analogous to runlevel 3

# graphical.target: analogous to runlevel 5

 

不区分大小写

[root@localhost opt]# grep -i to /etc/inittab

# multi-user.target: analogous to runlevel 3

# graphical.target: analogous to runlevel 5

# To view current default target, run:

# To set a default target, run:

[root@localhost opt]#

 

 

查找文件中的配置信息,去掉注释,以#开头的行都是注释,都去掉

[root@localhost etc]# grep -v ^# yum.conf

[main]

cachedir=/var/cache/yum/$basearch/$releasever

keepcache=0

debuglevel=2

logfile=/var/log/yum.log

exactarch=1

obsoletes=1

gpgcheck=1

plugins=1

installonly_limit=5

bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum

distroverpkg=centos-release

 

 

 

[root@localhost etc]#

 

 

猜你喜欢

转载自wlcacc.iteye.com/blog/2421170