35.文件的查找-grep+find+which+whereis+locate
文件的查找
which
查看可执行文件的位置
[root@localhost Desktop]# which cd
/usr/bin/cd
whereis
查看可执行文件的位置及相关文件
[root@localhost Desktop]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz
locate
配合数据库缓存,快速查看文件位置。
locate命令和find -name 功能差不多,是它的另外一种写法,但是这个要比find搜索快的多,因为find命令查找的是具体目录文件,而locate它搜索的是一个数据库/var/lib/mlocate/mlocate.db,这个数据库中存有本地所有的文件信息;这个数据库是Linux自动创建并每天自动更新维护。相关的配置信息在/etc/updatedb.conf,查看定时任务信息在/etc/cron.daily/mlocate
举例:
[root@localhost Desktop]# touch changmengka.txt #新建文件
[root@localhost Desktop]# locate changmengka.txt #查找文件,但是查找不到
[root@localhost Desktop]# updatedb #手动更新数据库
[root@localhost Desktop]# locate changmengka.txt #再次查找,即可找到
/root/Desktop/changmengka.txt
grep
过滤匹配,它是一个文件搜索工具
作用:过滤。他能够使用正则表达式来搜索文本,并把结果打印出来。
语法:
命令 | grep [参数] 关键字
参数:
-v 取反
-i 忽略大小写
^# 以#开头
#$ 以#结尾
^$ 空行
-n 对过滤的内容加上行号
| 或者的意思
-v取反
[root@localhost Desktop]# cat /etc/passwd | grep -v wang
-i忽略大小写
[root@localhost Desktop]# cat /etc/passwd | grep -i wang
wangbin:x:1001:1001::/home/wangbin:/bin/bash
wang:x:1002:1003::/home/wang:/bin/bash
^关键字,以关键字开头
[root@localhost Desktop]# cat /etc/passwd | grep ^w
wangbin:x:1001:1001::/home/wangbin:/bin/bash
wang:x:1002:1003::/home/wang:/bin/bash
关键字$,以关键字结尾
[root@localhost Desktop]# cat /etc/passwd | grep bash$
root:x:0:0:root:/root:/bin/bash
ai:x:1000:1000:ai:/home/ai:/bin/bash
wangbin:x:1001:1001::/home/wangbin:/bin/bash
wang:x:1002:1003::/home/wang:/bin/bash
^$显示空行
[root@localhost Desktop]# cat /etc/passwd | grep ^$
-n对过滤的内容加上行号
[root@localhost Desktop]# cat /etc/passwd | grep wang -n
45:wangbin:x:1001:1001::/home/wangbin:/bin/bash
46:wang:x:1002:1003::/home/wang:/bin/bash
|或者的意思
综合使用
root@localhost Desktop]# cat /etc/passwd | grep ^wang -v -i -n
find
查找相关文件
语法:
find 路径 [参数] 关键字
参数:
-name 按照文件名查找文件。 “名称”
-perm 按照文件权限来查找文件。666 777 等
-user 按照文件属主来查找文件
-group 按照文件所属的组来查找文件
-mtime -n / +n 按照文件的更改时间来查找文件,
- n 表示文件更改时间距现在n天以内
+ n 表示文件更改时间距现在n天以前
-type 查找某一类型的文件
b - 块设备文件
d - 目录
c - 字符设备文件
p - 管道文件
l- 符号链接文件
f - 普通文件
-size n 查找符合指定的文件大小的文件
-exec对匹配的文件执行该参数所给出的其他linux命令,相应命令的形式为' 命令 {} \;,注意{ }和 \;之间的空格,{}代表查到的内容
-maxdepth n #查找目录第n层的文件和目录
find 路径 –name
在某路径下查找名字是b开头的文件(可以查看以什么开头b*,可以查看以什么结尾*.txt)
[root@localhost Desktop]# find /root/Desktop/ -name b*
/root/Desktop/b.txt
find 路径 –perm
在某路径下查看权限是755的文件(权限必须是八进制形式)
[root@localhost Desktop]# find /root/Desktop/ -perm 755
/root/Desktop/
/root/Desktop/c.sh
find 路径 –user
在某路径下查看属主是root的文件
[root@localhost Desktop]# find /root/Desktop/ -user root
/root/Desktop/
/root/Desktop/.test.swp
/root/Desktop/.a.txt.swp
find 路径 –group
在某路径下查看属组是root的文件
[root@localhost Desktop]# find /root/Desktop/ -group root
/root/Desktop/
/root/Desktop/.test.swp
/root/Desktop/.a.txt.swp
find 路径 –mtime –n/+n(atime、ctime)
在某路径下查找n天之前或n天之内的文件
mtime:文件最后一次修改的时间
atime:文件最后一次访问的时间
ctime:文件的最后一次变化的时间,也是修改时间
-n:n天以内
[root@localhost Desktop]# find /root/Desktop/ -mtime -1
/root/Desktop/
/root/Desktop/.a.txt.swp
/root/Desktop/a.txt
+n:n天以前
[root@localhost Desktop]# find /root/Desktop/ -mtime +1
/root/Desktop/.test.swp
find 路径 –type b/d/c/p/l/f
在某路径下查找块设备、目录、字符设备、管道文件、符号链接、普通文件(显示所有内容)
b:块设备文件
[root@localhost Desktop]# find / -type b
/dev/sr0
/dev/sda3
/dev/sda2
d:目录
[root@localhost Desktop]# find / -type d
c:字符设备文件
[root@localhost Desktop]# find / -type c
p:管道文件
[root@localhost Desktop]# find / -type p
l:符号链接文件
[root@localhost Desktop]# find / -type l
f:普通文件
[root@localhost Desktop]# find / -type f
find 路径 –size n
在某路径下查找符合指定大小的文件
[root@localhost Desktop]# find /root/Desktop/ -size 50M
/root/Desktop/b.txt
find 路径 –其他参数 –exec 命令 {} \;
对匹配的文件执行该参数所给出的其他linux命令,相应命令的形式为' 命令 {} \;,注意{ }和 \;之间的空格,{}代表查到的内容
举例:
[root@localhost Desktop]# find /root/Desktop/ -name *.log -exec ls -l {} \;
-rw-r--r-- 1 root root 476 Dec 5 20:18 /root/Desktop/test.log
[root@localhost Desktop]# find /root/Desktop/ -name *.log -exec mv {} ./new.log \;
[root@localhost Desktop]# ll
total 51220
-rw-r--r-- 1 root root 476 Dec 5 20:18 new.log
find 路径 -maxdepth n –其他参数
在某路径下查看该路径下第n层级的满足其他参数的文件
[root@localhost Desktop]# find /root/Desktop/ -maxdepth 2 -perm 755
/root/Desktop/
/root/Desktop/c.sh
其他符号
-a:(and)并且
[root@localhost Desktop]# find /root/Desktop/ -size +1k -a -size -50M
/root/Desktop/.test.swp
/root/Desktop/.a.txt.swp
/root/Desktop/ture.file
-o:(or)或者
[root@localhost Desktop]# find /root/Desktop/ -size +1k -o -size -50M
/root/Desktop/
/root/Desktop/.test.swp
/root/Desktop/.a.txt.swp
/root/Desktop/a.txt
/root/Desktop/b.txt
+:超过
[root@localhost Desktop]# find /root/Desktop/ -size +20k
/root/Desktop/b.txt
-:低于
[root@localhost Desktop]# find /root/Desktop/ -size -20k
/root/Desktop/
/root/Desktop/.test.swp
/root/Desktop/a.txt

浙公网安备 33010602011771号