4.3 文件搜索命令
4.3.1 文件搜索命令 find
find
文件搜索
命令名称:find
命令所在路径:/bin/find
执行权限:所有用户
功能:文件搜索
语法:find [搜索范围] [匹配条件]
建议:
-
不要在服务器使用高峰期的时候使用
find
进行查找 -
查找的过程中,搜索的范围越小越好,尽量不要在根目录进行查找
-
搜索的条件越精准越好,找到的速度越快,消耗的资源越少
根据文件名查找
*
匹配任意字符;?
匹配单个字符
[]# find /etc -name init % 在/etc下精准搜索
[]# find /etc -name *init* % 搜索文件名内包含init的文件
[]# find /etc -name init* % 搜索以init开头的文件
[]# find /etc -name init??? % 以init开头,后面是三个字幕的文件
[]# find /etc -iname init??? % -iname不区分大小写地搜索
根据文件大小查找
数据块是Linux里存储文件的最小单位
1数据块=512字节=0.5K,100MB=102400KB=204800数据块
[]# find / -size +204800 % 在根目录中查找大于100MB的文件
根据所有者/所属组查找
[]# find /home -user shenchao % 根据所有者查找
[]# find /home -group meinv % 根据所属组查找
根据时间属性查找
-amin
访问时间 access
-cmin
文件属性 change(ls -ld
能查看到的即文件属性)
-mmin
文件内容 modify(打开文档删除几行等)
[]# find /etc -mmin -30 % 30min以内被修改过的文件
连接选项
-a
=and,两个条件同时满足
-o
=or,两个条件满足任意一个即可
[]# find /etc +163840 -a -size -204800 % 在/etc下查找大于80MB且小于100MB的文件
-type
根据文件类型查找:f=文件,d=目录,l=软连接
[]# find /etc -name init* -a -type f % /etc目录下以init开头的文件
[]# find /etc -name init* -a -type d % /etc目录下以init开头的目录
-exec 命令 {} \;
对搜索结果执行操作
-ok 命令 {} \;
对搜索结果执行操作(会进行确认)
[]# find /etc -name inittab -exec ls -l {} \; % 找到文件后直接查看其详细信息
[]# find /etc -name xiaodianying -exec rm {} \; % 找到文件后直接删除
[]# find /etc -name init* -a -type f -exec ls -l {} \; % 查找以init开头的文件并直接查看其信息
[]# find /home -user shenchao -ok rm {} \; % 找到shenchao的文件后挨个确认是否要删除
-inum
根据i节点查找
[]# ls -i % 显示所有文件i节点
[]# find . -inum 31531 -exec rm {} \; % 在当前目录下查找i节点为31531的文件并删除
4.3.2 其它文件搜索命令
locate
在文件资料库中查找文件
命令名称:locate
命令所在路径:/usr/bin/locate
执行权限:所有用户
功能:在文件资料库中查找文件
语法:locate 文件名
[]# locate inittab % locate可以达到秒搜的效果
系统里所有的文件都会被定期更新收录到一个文件资料库中
find是在某一个分区或硬盘上查找,而locate只需要在这个资料库中查找,所以速度非常快
[]# locate locate % 搜索一下locate,其中会得到一条信息:/var/lib/mlocate/mlocate.db,为文件资料库
如果一个新建的文件,没有被收录到资料库中,实际上它是找不到的,需要手动更新资料库
[]# touch /root/yangmi % 新建文件yangmi
[]# locate yangmi % 资料库没有更新,locate无法找到yangmi
[]# find /root -name yangmi % find是实时查找,可以找到 /root/yangmi
[]# updatedb % 手动更新资料库
[]# locate yangmi % 资料库更新后,locate可以找到 /root/yangmi
有一些目录,如临时存放文件的目录/tmp并不在资料库的收录范围之内,所以 locate
无法找到该目录下的文件
[]# touch /tmp/zhangbozhi
[]# updatedb
[]# locate zhangbozhi % 更新资料库后,locate仍无法找到
locate
和 find
一样,都区分大小写
[]# touch /root/Teacher.Cang
[]# locate teacher.cang % 由于大小写原因无法搜索到
[]# locate -i teacher.cang % 使用-i后不区分大小写
which
命令所在目录及别名
命令名称:which
命令所在路径:/usr/bin/which
执行权限:所有用户
功能:搜索命令所在目录及别名信息
语法:which 命令
[]# which cp
aluas cp='cp -i'
/bin/cp
[]# which useradd
/usr/sbin/useradd
whereis
命令所在目录及帮助文档路径
命令名称:whereis
命令所在路径:/usr/bin/whereis
执行权限:所有用户
功能:搜索命令所在目录及帮助文档路径(大多数Linux命令的帮助文档都存放在/user/share下)
语法:whereis [命令名称]
[]# whereis useradd
useradd: /usr/sbin/useradd /user/share/man/man8/useradd.8.gz
grep
文件中字串搜索
命令名称:grep
命令所在路径:/bin/grep
执行权限:所有用户
功能:在文件中搜索字串匹配的行并输出
-i
不区分大小写
-v
排除指定字串
语法:grep -iv [指定字串] [文件]
grep multiuser /etc/inittab % 区分大小写
# 3 - Full multiuser mode
grep -i multiuser /etc/inittab % -i不区分大小写
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# grep -v ^# /etc/inittab % 去掉文件中以#号开头的行(注释),其中^表示以…开头
(LAMP = Linux Apache Mysql PHP)