1.which
功能描述:
查找Linux命令文件并显示所在的位置–搜索范围由PATH环境变量指定
语法:
which 命令或程序名
范例:
[root@localhost ~]# which mkdir
/usr/bin/mkdir
[root@localhost ~]# which ssh
/usr/bin/ssh
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# which cd
/usr/bin/cd
[root@localhost ~]#
echo $PATH的含义:
echo是用于在控制台显示信息的命令;
$在这里是取得某个变量的值 PATH就是变量名;
合起来就是在控制台显示PATH环境变量的值;
2.whereis
功能描述:
该指令会在特定目录中查找符合条件的文件。这些文件应属于原始代码、二进制文件,或者是帮助文件。
该指令只能用于查找二进制文件、源码文件和man手册页,一般文件的定位需使用locate命令。
语法:
whereis 命令
选项:
-b:只查找二进制文件
-m:只查找说明文件
范例:
#使用指令“whereis”查看指令“bash”的位置,输入如下命令:
[root@localhost ~]# whereis bash
bash: /usr/bin/bash /usr/share/man/man1/bash.1.gz /usr/share/info/bash.info.gz
注意:以上输出信息从左至右分别为查询的程序名、bash路径、bash的man手册页路径。
[root@localhost ~]# whereis -b bash
bash: /usr/bin/bash
[root@localhost ~]# whereis -m bash
bash: /usr/share/man/man1/bash.1.gz /usr/share/info/bash.info.gz
3.find
功能描述:
用于查找文件或目录
语法:
find [path] -option
选项:
常用查找条件:
选项 | 功能作用 |
---|---|
-name | 按文件名称查找 |
-size | 按文件大小查找 |
-user | 按文件属主查找(不常用) |
-type | 按文件类型查找(f:文件;d:目录;l:符号链接,软链接) |
高级查找条件:
选项 | 功能作用 |
---|---|
-perm | 按权限进行查找 |
-ctime(-cmin) | 按文件创建时间(以天为单位)查找 |
-atime(-amin) | 按访问时间查找 |
-mtime(-mmin) | 按修改时间查找 |
-maxdepth | 限制find的递归层级 |
-exec | 查找后再执行操作 |
范例:
#按照名字查找
[root@localhost ~]# find /etc/ -name "resol*.conf" #查找/etc/下名字为resol开头以.conf结尾的文件
#检索空目录
[root@localhost ~]# find / -type d -empty #检索用户主目录下所有的空目录
#反义匹配
[root@localhost ~]# find /usr/ -type f ! -name "*.txt" #检索/usr/下所有文件名不以.txt为后缀的文件
#按照时间相关
-mtime 2:该文件2天前被修改过
-mtime -2:该文件2天以内被修改过
-mtime +2:该文件距离上次修改已经超过2天时间
[root@localhost ~]# find /usr -type f -mtime +5 -mtime -10 #检索/usr下5到10天之前修改过的文件
[root@localhost ~]# find /usr -type f -mtime 2 -amin 5 #检索/usr下两天前被修改过且5分钟前又读取过的文件
#按照文件大小查找
表示文件大小的单位由以下字符组成(使用+或-符号表示大于或小于当前条件):
c:字节
k:KB
M:MB
G:GB
[root@localhost ~]# find / -size +1G #检索文件大小高于1GB的文件
/proc/kcore
find: ‘/proc/2092/task/2092/fd/6’: No such file or directory
find: ‘/proc/2092/task/2092/fdinfo/6’: No such file or directory
find: ‘/proc/2092/fd/5’: No such file or directory
find: ‘/proc/2092/fdinfo/5’: No such file or directory
#按照权限查找
[root@localhost ~]# find /usr/ -perm 644 #搜索/usr/目录下权限为644(即rwxr-xr-x)的文件
#限制递归层级
find命令默认时以递归的方式检索项目的,这有时候会导致的结果数量非常巨大。可以使用-maxdepth 限制find命令递归的层数。
[root@localhost ~]# find / -maxdepth 3 #搜索时向下递归的层数最大为3
#逻辑组合
find命令支持“and”和“or”两种逻辑运算,对应的命令选项分别时-a和-o,通过这两个选项可以对搜索条件进行更复杂的组合。
[root@localhost ~]# find /usr -type f -o -type d
#执行自定义命令
-exec 命令 {} \;
[root@localhost ~]# find / -type f -exec grep -l root {} \;
[root@localhost ~]# find /etc/ -name "*" | xargs grep "root" #查找/etc下name所有过滤出内容包含root的文件内容