find指令
find指令是按照路径,递归查找相应条件的文件,并将其显示在终端上
基本语法: find [搜索范围] [选项]
选项:
-name (按照文件名查找)
-user (按照拥有者查找)
-size +n/-n/n (按照文件大小查找;+n表示大于n的文件,-n表示小于n的文件,n表示查找大小等于n的文件)(文件大小有 K,M,G)
举例:
文件名查找:按名称查找/home目录下的hello.txt 文件-----------find /home -name hello.txt
[root@localhost ~]# find /home -name hello.txt
/home/hello.txt
拥有者查找:按拥有者查找/home目录下拥有者为 lyj 的文件-------------find /home -user lyj
[root@localhost home]# find /home -user lyj
/home/lyj
/home/lyj/.mozilla
/home/lyj/.mozilla/extensions
/home/lyj/.mozilla/plugins
/home/lyj/.bash_logout
/home/lyj/.bash_profile
/home/lyj/.bashrc
大小查找:查找/etc 下大于500k的文件----------------------------find /etc -size +500k
[root@localhost etc]# find /etc -size +500k
/etc/udev/hwdb.bin
/etc/ssh/moduli
/etc/services
/etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/policy/policy.31
/etc/selinux/targeted/active/policy.kern
/etc/selinux/targeted/active/policy.linked
/etc/brltty/zh-tw.ctb
locate指令
locate 是一个快速定位文件路径的指令,利用事先建立的目录和文件路径的数据库来查找路径,因此locate指令无需遍历整个目录
在执行locate命令之前,必须先执行建立相应数据库的命令-------updatedb
基本语法: locate 搜索文件
举例:
使用 locate 命令快速定位 hello.txt 文件所在目录------------------locate hello.txt
[root@localhost home]# updatedb
[root@localhost home]# locate hello.txt
/home/hello.txt
which指令
查看某个指令在哪个目录下
基本语法:which 指令
举例:
查看 ls 指令在哪个目录下---------------which ls
[root@localhost home]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
grep指令和 管道符号 |
grep 为过滤查找; “ | ” 表示将前一个指令的处理结果输出传递给后面的指令
grep 指令经常和 “ | ” ,“cat” 一起使用
基本语法:grep [选项] [关键字] 文件名
选项:
-c (统计符合字符串条件的行数,并输出)-----------grep -c xxx 文件名
-v (显示不包含搜索关键字的全部信息)---------------grep -v xxx 文件名
-n (输出时显示行号)---------------------------------------grep -n xxx 文件名
-i (忽略字母大小写)---------------------------------------grep -i xxx 文件名
举例:
将hello.txt 文件中查找 “ok ” 所在行,并显示行号------------写法一:cat hello.txt | grep -n "ok" hello.txt;写法二:grep -n "ok" /home/hello.txt
[root@localhost ~]# cat /home/hello.txt | grep -n "ok" /home/hello.txt
2:ok
6:ok
[root@localhost ~]# grep -n "ok" /home/hello.txt
2:ok
6:ok