文件搜索命令
文件搜索命令find
尽量减少搜索操作(把文件整理好,能记住就能减少),会占用很多资源。
尽量不要在服务器使用高峰期查找,查找的范围也要尽量精确,搜索条件越精准越好。
- 命令名称:find
- 命令所在路径:/bin/find
- 执行权限:所有用户
- 语法:find [搜索范围] [匹配条件]
- 功能描述:文件搜索。
根据文件名搜索
例:在目录/etc中查找文件init
-iname 不区分大小写

查看详细信息
find /etc -name inittab -exec ls -l {} \;
在/etc下查找inittan文件并显示其详细信息
- -exec/ok 命令 :会对你有一个询问确认的过程(每一个查找得到的文件都会询问)
- {} ; 对搜索结果执行操作。
- “\”是转义符

模糊查找
注意:在Linux中不存在模糊查找,是精准搜索。
但是——可以使用通配符“*”,新版本的要加双引号
find /etc -name "*init*"

以关键字开头/结尾的文件
以关键字开头
find /etc -name "*init"

以关键字结尾
同上,知识将“*”放在关键字前面。 
更精准的查找
比如你记得文件名是init后面加三个字母。
可以使用“?”,一个问号匹配单个字符。
find /etc -name init???

不区分大小写的查找
find [文件名] [-iname]
find /etc -iname init

根据大小搜索
根据固定大小搜索
find / -size +204800
在根目录下查找大于100MB的文件。
- +n 大于 -n 小于 n等于
- 注意:单位是数据块,数据块在Linux中是512字节,也就是0.5K

根据大小范围搜索
find /etc -size +163840 -a -size -204800
在/etc下查找大于80MB小于100MB的文件
- -a :两个条件同时满足(and)
- -o:两个条件满足一个即可(or)

根据所有者/所属组搜索
find /home -user q0
在根目录下查找所有者为q0的文件。 
所属组——根据-group查找即可。
根据时间属性查找
find /etc -cmin -5
在/etc下查找五分钟内被修改过属性的文件和目录。
5分钟之内没有结果,那就1000吧。 
- -amin 访问时间access
- -cmin 修改时间 change
- -mmin 文件内容 modify

文件类型type
- -a :两个条件同时满足(and)
- -o:两个条件满足一个即可(or)
示例:找含有init的文件而非目录
-type 根据文件类型查找
f 文件 d 目录 l 软连接文件
find /etc -name "init*" -a -type f

inum 根据i节点查找
ls -i : 查看文件的i节点(有些文件名过于复杂,比如有空格的情况就可以根据i节点删除)
- 每个文件都有i节点
 创建一个文件并删除  以上的查找条件都可以通过-a或者-o连接起来。
其他文件搜索命令
快速查找命令locate
类似于windows中everything这种查找工具实现快速查找的命令。
- 命令名称:locate
- 命令所在路径:/usr/bin/locate
- 执行权限:所有用户
- 语法:locate [文件名]
- 功能描述:在文件资料库中查找文件
- 范例:$locate inittab
用locate搜索locate (用魔法打败魔法) 
-
其中有一个/var/lib/mlocate/mlocate.db就是系统文件资料库,所有系统文件都会被定期更新收录到里面。  -
所以locate不像find在确定的分区以及目录下查找,而是直接查找资料库,所以速度很快 -
但是这样新建的文件用locate就不会被找到,但是find可以找到
- locate都打错了,丢人!
- 因为locate不能自动实时更新
- 但是可以手动呀!不是有手就行?
 -
updatedb更新。  -
但是放在tmp文件这种临时文件目录下也是不能找到的,因为临时文件并不收录在文件资料库中。(我这里的是temp不是官方的tmp哈) -
update严格区分大小写
精确搜索命令which
- 命令名称:which
- 命令所在路径:/usr/bin/which
- 执行权限:所有用户
- 语法:which 命令
- 功能描述:搜索命令所在目录及别名信息
- 范例:$which ls
示例
- ls放在bin下面是所有用户都能用的
 ls=后面的那一坨是它的别名(别名也太tm长了) 
- useradd放在sbin目录下,是只有管理员才能用的。

whereis查找帮助文档路径
- 命令名称:whereis
- 命令所在路径:/usr/bin/whereis
- 执行权限:所有用户
- 语法:whereis [命令名称]
- 功能描述:搜索命令所在目录及帮助文档路径
- 范例:$whereis ls
示例

- man值manual(手册),不是男人的意思(想男人想疯了)
文件搜索命令:grep
这个命令很重要哦!!!
- 命令名称:grep
- 命令所在路径:bin/grep
- 执行权限:所有用户
- 语法:grep -iv [指定子串] [文件]
- 功能描述:在文件中搜寻子串匹配的行并输出
- 范例:# grep mysql /root/install.log
示例
grep mysql /root/install.log
- mysql就是我想查找的指定字符串,
- grep是指找文件中的内容的位置而不是文件本身。
我没有装mysql,那就找个我有的吧,,
- 用more查看一下文件内容
 - 就找defualt吧:
 by the way:
在linux里面“#“开头的都是注释。(如果不要的话这就可以用上面说的-v排除啦!)
但是慎用,可能会删除不必要的内容。
|