文章目录
which
which [-a] command
- 这个命令是根据PATH这个环境变量所包含的路径去查询“执行文件”的文件名。
参数
- -a:将所有由PATH目录中可以找到的命令均列出,而不只第一个被找到的命令名称
示例
[root@localhost ~]# which ifconfig
/usr/sbin/ifconfig
whereis
whereis [-bmsu] 文件或目录名
- 只会查找当前目录,不会查找子目录
参数
- -b:只找二进制格式的文件
- -m:只找在说明文件manual路径下的文件
- -s:只找source源文件【?】
- -u:查找不在上述三个选项当中的其他特殊文件【?】
- -B:限制whereis命令搜索二进制文件的目录,多个目录使用空格隔开【?】
- -M:限制whereis命令搜索说明文件的目录,多个目录使用空格隔开【?】
- -S:限制whereis命令搜索源文件的目录,多个目录使用空格隔开【?】
- -f:终止目录名,并指示文件名的开始,当使用-BMS时必须使用
示例
[root@localhost sbin]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@localhost sbin]# whereis -b ifconfig
ifconfig: /usr/sbin/ifconfig
[root@localhost sbin]# whereis -b -B /usr/sbin/ -f ifconfig
ifconfig: /usr/sbin/ifconfig
[root@localhost sbin]# whereis -B /usr/sbin/ -f ifconfig
ifconfig: /usr/share/man/man8/ifconfig.8.gz /usr/sbin/ifconfig
# 不会查找子目录
[root@localhost ~]# whereis -b -B /usr/ -f ifconfig
ifconfig:
locate
locate [-ir] keyword
- Linux系统会将系统内的所有文件都记录在一个数据库文件中,当使用whereis、locate时,都会以此数据库文件的内容为准,因此,有时可能会找到已经删除的文件,有时找不到刚刚创建的文件。
- 安装locate:yum -y install mlocate
- locate寻找的数据是由已创建的数据库/var/lib/mlocate/mlocate.db文件中的数据所查到的
- 新建文件或删除数据需要去更新数据库,否则查询结果会不正确,系统会定时更新,也可手动更新,执行updatedb命令即可
- updatedb:根据/etc/updatedb.conf的设置去查找系统硬盘内的文件名,并更新数据库文件
- locate:根据数据库文件中的数据,找出用户输入的关键字文件名【模糊匹配文件路径】
参数
- -i:忽略大小写的差异
- -r:后面可接正则表达式的显示方式
示例
[root@localhost sbin]# locate ifconfig
/usr/sbin/ifconfig
/usr/share/man/de/man8/ifconfig.8.gz
/usr/share/man/fr/man8/ifconfig.8.gz
/usr/share/man/man8/ifconfig.8.gz
/usr/share/man/pt/man8/ifconfig.8.gz
[root@localhost ~]# locate test.txt
/root/test.txt
/usr/share/doc/pcre-devel-8.32/pcretest.txt
/usr/share/doc/pcre-devel-8.32/perltest.txt
[root@localhost ~]# locate devel
/usr/share/doc/zlib-devel-1.2.7
/usr/share/doc/libstdc++-devel-4.8.5/ChangeLog-1998.bz2
....
find
find [PATH] [option] [action]
- 查找磁盘文件,性能较低,功能强大
与时间有关的参数:
-atime:文件访问时间
-ctime:文件状态变更时间
-mtime:文件内容修改时间
- -mtime n:n为数字,意义为在n天之前的“一天之内”被更改过的文件
- -mtime +n:列出在n天之前(不含n天本身)内容被更改过的文件名
- -mtime -n:列出在n天之内(含n天本身)内容被更改过的文件名
- -newer file:file为一个存在的文件,列出比file还要新的文件名
示例
# 列出当前目录下过去24小时内有改动的文件,0代表当前时间,从现在开始到24小时之前
[root@localhost ~]# find ./ -mtime 0
.
./.viminfo
./test.txt
# 查找当前目录下,比.viminfo文件日期更新的文件
[root@localhost ~]# find ./ -newer ./.viminfo
./
./test.txt
与用户或用户组名有关的参数
-uid n:n为数字,UID,用户ID记录在/etc/passwd中
-gid n:n为数字,GID,用户组名ID记录在/etc/group中
-user name:name为用户账号名称
-group name:name为用户组名
nouser:文件所有者不存在于/etc/passwd中的文件
nogroup:文件所有用户组不存在于/etc/group中的文件
示例
# 查找当前目录下所有者是root用户的文件
[root@localhost ~]# find ./ -user root
# 查找当前目录下不属于任何人的文件,通常是以源码编译软件时生成的文件
[root@localhost ~]# find ./ -nouser
与文件权限及名称有关的参数
-name filename:查找文件名为filename的文件
-size [±]SIZE:查找比SIZE还要大(+)或小(-)的文件,c代表byte,k代表1024bytes
-type TYPE:查找文件类型为TYPE的,类型主要有正常文件(f)、设备文件(b,c)、目录(d)、连接文件(l)、socket文件(s)、FIFO(p)
-perm mode:查找文件权限为mode的文件
-perm -mode:查找文件权限必须全部包括mode的文件
-perm +mode:查找文件权限包含任一mode权限的文件【已废弃】
-perm /mode:查找文件权限包含任一mode权限的文件
示例
[root@localhost ~]# find ./ -name test.txt
./test.txt
# 支持通配符 *
[root@localhost test]# find ./ -name *test*
./test.txt
# test目录权限
drwxr-xr-x. 2 root root 37 Jan 25 23:16 test
# test目录下文件权限
[root@localhost test]# ll
total 16568
-rwx-wx-wx. 1 root root 0 Jan 25 23:16 test.txt
-rw-r--r--. 1 root root 16963822 Jan 25 23:16 web.jar
# 查找当前目录下权限为0644(-rw-r--r--)的文件
[root@localhost ~]# find ./ -perm 0644
./web.jar
# 查找当前目录下权限包含全部0644(-rw-r--r--)的文件
[root@localhost test]# find ./ -perm -0644
./
./web.jar
# 修改文件权限
[root@localhost test]# ll
total 16568
--w-------. 1 root root 0 Jan 25 23:16 test.txt
-rw-r--r--. 1 root root 16963822 Jan 25 23:16 web.jar
# 查找当前目录下权限包含任意一个0644权限(-rw-r--r--)的文件
[root@localhost test]# find ./ -perm /0644
./
./test.txt
./web.jar
其他可进行的操作
-exec command:command为其他目录,这里可接其他目录来处理查找到的结果
-print:将结果打印到屏幕上,这是默认操作
示例
# 将find ./ -perm /0644的查询结果放入到{}中进行处理
# 从-exec到\;是find的额外命令
# -exec后接命令,但不支持别名,所以只能用ls -l,而不能用ll
# ;在bash环境中有特殊意义,因此利用反斜杠来转义
[root@localhost test]# find ./ -perm /0644 -exec ls -l {} \;
total 16568
--w-------. 1 root root 0 Jan 25 23:16 test.txt
-rw-r--r--. 1 root root 16963822 Jan 25 23:16 web.jar
--w-------. 1 root root 0 Jan 25 23:16 ./test.txt
-rw-r--r--. 1 root root 16963822 Jan 25 23:16 ./web.jar
参考资料
《鸟哥的Linux私房菜》基础学习篇(第三版)