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

Linux基础--文件查找

时间:2016-03-10 18:53:52      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:linux   find   

1. which

which - shows the full path of (shell) commands.

[root@localhost ~]# which top
/usr/bin/top
[root@localhost shell]# which grep
alias grep=‘grep --color=auto‘
/usr/bin/grep
[root@localhost ~]# which --skip-alias grep
/usr/bin/grep


2. locate

locate - find files by name

[root@localhost ~]# locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/root/.pyenv/versions/3.5.1/lib/python3.5/test/keycert.passwd.pem
/root/.pyenv/versions/3.5.1/lib/python3.5/test/ssl_key.passwd.pem
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/kdepasswd
/usr/bin/lppasswd
/usr/bin/passwd
/usr/bin/smbpasswd
/usr/bin/vncpasswd
...


locate文件查找是基于数据库的,系统会每天固定时间去更新locate数据库,那么当天创建的文件可能使用locate命令就没有办法找到了。此时需要手动更新locate数据库,可以使用系统自带的每日更新locate数据库脚本

[root@localhost ~]# /etc/cron.daily/mlocate

或者

[root@localhost ~]# updatedb


3. find

find查找文件是实时查找,没有基于数据库,速度可能会慢一点,但是比locate会更常用。因为locate会遍历整个文件系统,然后建立数据库索引,这会消耗大量的资源,影响服务器上服务的正常运行。


基本用法可用通过此[链接]找到,介绍得很详细了,这里只是总结一下查找方式和一些应该注意的地方。

  • 根据文件名查找

可以使用glob匹配规则,可以使用正则表达式。

[root@localhost ~]# find /etc/ -name *.conf
[root@localhost ~]# find /etc/ -regextype posix-basic -regex "/etc/sudo.*\.conf"


regextype为正则表达式类型,默认为emacs类型,还有posix-awk, posix-basic, posix-egrep,posix-extended这四种。

Changes the regular expression syntax understood by -regex  and  -iregex
tests  which  occur  later  on  the command line.  Currently-implemented
types are emacs (this is the default),  posix-awk,  posix-basic,  posix-
egrep and posix-extended.

当你给出不存在的regextype时,find命令会给出如下提示信息,可用的类型有这些。不太清楚是man手册没有更新,还是这些可用的类型中其中一些只是标准类型的缩写。不过常用的类型一般也就是posix-basic和posix-extended吧。

valid types are ‘findutils-default’, ‘awk’, ‘egrep’, ‘ed’, ‘emacs’, ‘gnu-awk’, ‘grep’, ‘posix-awk’, ‘posix-basic’, ‘posix-egrep’, ‘posix-extended’, ‘posix-minimal-basic’, ‘sed’.


值得注意的是man手册中这样一段描述,意思是在find中使用正则表达式需要匹配整个路径而不是仅仅匹配文件,如果仅匹配文件名你是找不到的。

File  name  matches  regular expression pattern.  This is a match on the
whole  path,  not  a  search.   For  example,  to  match  a  file  named
‘./fubar3‘, you can use the regular expression ‘.*bar.‘ or ‘.*b.*3‘, but
not ‘f.*r3‘.  The regular expressions understood by find are by  default
Emacs  Regular  Expressions, but this can be changed with the -regextype
option.

感受一下

[root@localhost ~]# find /etc/ -regextype posix-basic -regex "sudo.*\.conf"
[root@localhost ~]# find /etc/ -regextype posix-basic -regex "/etc/sudo.*\.conf"
/etc/sudo-ldap.conf
/etc/sudo.conf
  • 根据文件类型查找

  • 根据文件时间戳查找

  • 根据文件大小查找

  • 根据文件权限查找

mode精确匹配则满足条件

[root@localhost ~]# find /etc/ -user root -perm 400 -ls


将mode转换成二进制,比如mode=111那么转换成二进制为001 001 001,而被查找的文件的权限位也可以被转换成一个二进制数,两者在位上为1的部分必须完全匹配,则满足条件,位上为0的不用管。即被查找文件需要完全匹配mode给出的基本权限即可。

[root@localhost ~]# find /etc/ -user root -perm -111 -ls


将mode转换成二进制,比如mode=111那么转换成二进制为001 001 001,而被查找的文件的权限位也可以被转换成一个二进制数,两者在任意一位为1位上匹配,则满足条件,位上为0的不用管。即被查找文件z只需要匹配mode给出的基本权限的任意一个权限位即可。

[root@localhost ~]# find /etc/ -user root /perm -111 -ls
  • 多条件联合查找时使用与或非

注意:优先级:非>与>或;尽量使用括号将条件之间的逻辑关系描述清楚,增强可读性。

  • 对于查找到的文件进行处理

虽然find自带两个选项-ok和-exec来做复杂处理动作,但是它是将所有匹配的结果一次性交给后面的处理动作,当结果集过大时很容易出现问题。

[root@localhost ~]# find /etc -user root -o -name "*.conf" -size +1k | wc -l
2016
[root@localhost ~]# find /etc -user root -o -name "*.conf" -size +1k -exec ls -dl {} \; | wc -l
1

使用xargs命令结合管道来使用就可以解决。

[root@localhost ~]# find /etc -user root -o -name "*.conf" -size +1k | xargs ls -dl | wc -l
2016


本文出自 “knfprex3a29” 博客,请务必保留此出处http://knfprex3a29.blog.51cto.com/9761463/1749686

Linux基础--文件查找

标签:linux   find   

原文地址:http://knfprex3a29.blog.51cto.com/9761463/1749686

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