grep,egrep,fgrep:文本查找
文件查找:
locate:
非实时,模糊匹配,查找是根据全系统文件数据库进行的
# updatedb,手动生成文件数据库
速度快
find:
实时
精确
支持众多查找标准
遍历指定目录中的所有文件完成查找,速度慢
find 查找路径 查找标准 查找到以后的处理运作
查找目录:默认为当前目录
查找标准:默认为指定路径下的所有文件
处理运作:默认为显示
匹配标准:
-name 'FILENAME':对文件名做精确匹配
find /etc -name 'passwd'
文件名通配:
*:任意长度的任意字符
?
[]
-iname 'FILENAME':文件名匹配不区分大小写
-regex PATTERN:基于正则表达式进行文件名匹配
-user USERNAME:根据属主查找
-group GROUPNAME:根据属组查找
-uid UID:根据UID查找
-gid GID:根据GID查找
-nouser:查找没有属主的文件
-nogroup:查找没有属组的文件
-type
f:普通文件
d
c
b
l
p
a
-size [+|-]
#k
#M
#G
find /etc -size 10k 查找9k到10k的文件
组合条件:
-a
-o
-not
find /tmp -nouser -a -type d
find /tmp -nouser -o -type d 没有属主或是目录的
find /tmp -not -type d
/tmp目录,不是目录,并且还不能套接字类型的文件
find /tmp -not -type d -a -not -type -l
/tmp/test目录下,属主不是user1,也不是user2的文件;
find /tmp/test -not -user user1 -a -not -user user2
find /tmp/test -not \( -user user1 -o -user user2 \)
-mtime
-ctime
-atime
[+|-]# +5:5天之前访问过,-5:5天之内访问过
-mmin
-cmin
-amin
[+|-]#
find ./ -amin -5 5分钟之内访问过的
find ./ -amin 5 5分钟之前访问过
-perm MODE:精确匹配
/MODE: 任意一位匹配即满足条件
-MODE: 文件权限能完全包含此MODE时才符合条件
-644
644: rw-r--r--
755: rwxr-xr-x
750: rwxr-x---
find ./ -perm -001
find ./ -perm -022
运作:
-print: 显示
-ls:类似ls -l的形式显示每一个文件的详细
-ok COMMAND {} \; 每一次操作都需要用户确认
-exec COMMAND {} \;
find ./ -perm -006 -exec chmod o-w {} \;
find ./ -type d -ok chmod +x {} \;
find ./ -perm -020 -exec mv {} {}.new \;
find ./ -name "*.sh" -a -prem -111 -exec chmod o-x {} \;
1、查找/var目录下属主为root并且属组为mail的所有文件;
find /var -user root -group mail
2、查找/usr目录下不属于root,bin,或student的文件;
find /usr -not -user root -a -not -user bin -a -not -user student
find /usr -not \( -user root -o -user bin -o -user student \)
3、查找/etc目录下最近一周内内容修改过且不属于root及student用户的文件;
find /etc -mtime -7 -not \ ( -user root -o -user student \)
find /etc -mtime -7 -not -user root -a -not -user student
4、查找当前系统上没有属主或属组且最近1天内曾被访问过的文件,并将其属主属组均修改为root;
find / \( -nouser -o -nogroup \) -a -atime -1 -exec chown root:root {} \;
5、查找/etc目录下大于1M的文件,并将其文件名写入/tmp/etc.largefiles文件中;
find /etc -size +1M >> /tmp/etc.largefiles
6、查找/etc目录下所有用户都没有写权限的文件,显示出其详细信息;
find /etc -not -perm /222 -ls
文件查找:
locate:
非实时,模糊匹配,查找是根据全系统文件数据库进行的
# updatedb,手动生成文件数据库
速度快
find:
实时
精确
支持众多查找标准
遍历指定目录中的所有文件完成查找,速度慢
find 查找路径 查找标准 查找到以后的处理运作
查找目录:默认为当前目录
查找标准:默认为指定路径下的所有文件
处理运作:默认为显示
匹配标准:
-name 'FILENAME':对文件名做精确匹配
find /etc -name 'passwd'
文件名通配:
*:任意长度的任意字符
?
[]
-iname 'FILENAME':文件名匹配不区分大小写
-regex PATTERN:基于正则表达式进行文件名匹配
-user USERNAME:根据属主查找
-group GROUPNAME:根据属组查找
-uid UID:根据UID查找
-gid GID:根据GID查找
-nouser:查找没有属主的文件
-nogroup:查找没有属组的文件
-type
f:普通文件
d
c
b
l
p
a
-size [+|-]
#k
#M
#G
find /etc -size 10k 查找9k到10k的文件
组合条件:
-a
-o
-not
find /tmp -nouser -a -type d
find /tmp -nouser -o -type d 没有属主或是目录的
find /tmp -not -type d
/tmp目录,不是目录,并且还不能套接字类型的文件
find /tmp -not -type d -a -not -type -l
/tmp/test目录下,属主不是user1,也不是user2的文件;
find /tmp/test -not -user user1 -a -not -user user2
find /tmp/test -not \( -user user1 -o -user user2 \)
-mtime
-ctime
-atime
[+|-]# +5:5天之前访问过,-5:5天之内访问过
-mmin
-cmin
-amin
[+|-]#
find ./ -amin -5 5分钟之内访问过的
find ./ -amin 5 5分钟之前访问过
-perm MODE:精确匹配
/MODE: 任意一位匹配即满足条件
-MODE: 文件权限能完全包含此MODE时才符合条件
-644
644: rw-r--r--
755: rwxr-xr-x
750: rwxr-x---
find ./ -perm -001
find ./ -perm -022
运作:
-print: 显示
-ls:类似ls -l的形式显示每一个文件的详细
-ok COMMAND {} \; 每一次操作都需要用户确认
-exec COMMAND {} \;
find ./ -perm -006 -exec chmod o-w {} \;
find ./ -type d -ok chmod +x {} \;
find ./ -perm -020 -exec mv {} {}.new \;
find ./ -name "*.sh" -a -prem -111 -exec chmod o-x {} \;
1、查找/var目录下属主为root并且属组为mail的所有文件;
find /var -user root -group mail
2、查找/usr目录下不属于root,bin,或student的文件;
find /usr -not -user root -a -not -user bin -a -not -user student
find /usr -not \( -user root -o -user bin -o -user student \)
3、查找/etc目录下最近一周内内容修改过且不属于root及student用户的文件;
find /etc -mtime -7 -not \ ( -user root -o -user student \)
find /etc -mtime -7 -not -user root -a -not -user student
4、查找当前系统上没有属主或属组且最近1天内曾被访问过的文件,并将其属主属组均修改为root;
find / \( -nouser -o -nogroup \) -a -atime -1 -exec chown root:root {} \;
5、查找/etc目录下大于1M的文件,并将其文件名写入/tmp/etc.largefiles文件中;
find /etc -size +1M >> /tmp/etc.largefiles
6、查找/etc目录下所有用户都没有写权限的文件,显示出其详细信息;
find /etc -not -perm /222 -ls