目录
2. 指定所有者(用户、组、其他人)至少有一个拥有此权限即可
1. 查找当前目录下,所属用户为root的文件,并把匹配到的每一个文件通过 chown 修改所有者为 tom。
2. 查找家目录下面,以 .txt 结尾的文件,并把匹配到的每一个文件执行删除操作,-ok 与 -exec 不同之处在于会提示需要用户是否要执行此操作
3. 查找当前目录下,以 .txt 结尾的文件,并把匹配到的每一个文件通过 cat 命令显示屏幕上,然后执行 > /all.txt 把结果写入到 all.txt 文件中
5. 找出当前目录下所有.txt文件并以“File:文件名”的形式打印出来
7. 查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk
3. 忽略错误信息,使用 2 > /dev/null 重定向到垃圾桶
-user
-group
通过所有者来查找
1. 指定所属的用户
find . -user syslog
2. 指定所属的组
find . -group www
-perm
Searching files by permissions
• Search for files which are executable but not readable.
$ find /sbin /usr/sbin -executable \! -readable -print #
搜索
/sbin /usr/sbin
目录下 能执行但是不能读的权限文件
• Search for files which have read and write permission for their owner, and group, but which other users
can read but not write to.
$
find . -perm 664
# 搜索权限是644的文件
Files which meet these criteria but have other permissions bits set (for example if someone can execute
the file) will not be matched.
• Search for files which have read and write permission for their owner and group, and which other users
can read, without regard to the presence of any extra permission bits (for example the executable bit).
$ find . -perm -664
# 搜索权限是644的文件,不考虑是否存在任何额外的权限位
This will match a file which has mode 0777, for example.
• Search for files which are writable by somebody (their owner, or their group, or anybody else).
$ find . -perm /222
• Search for files which are writable by either their owner or their group.
$ find . -perm /220
$ find . -perm /u+w,g+w
$ find . -perm /u=w,g=w
All three of these commands do the same thing, but the first one uses the octal representation of the
file mode, and the other two use the symbolic form. The files don't have to be writable by both the
owner and group to be matched; either will do.
• Search for files which are writable by both their owner and their group.
$ find . -perm -220
$ find . -perm -g+w,u+w
Both these commands do the same thing.
• A more elaborate search on permissions.
$ find . -perm -444 -perm /222 \! -perm /111
$ find . -perm -a+r -perm /a+w \! -perm /a+x
These two commands both search for files that are readable for everybody (-perm -444 or -perm -a+r),
have at least one write bit set (-perm /222 or -perm /a+w) but are not executable for anybody (! -perm
/111 or ! -perm /a+x respectively).
通过权限来查找
1. 指定精确的权限查找
find / -perm 644
2. 指定所有者(用户、组、其他人)至少有一个拥有此权限即可
find / -perm /644
3. 指定文件最低权限查找,即大于等于
find / -perm -644
4. 查找文件不是指定的权限(取反)
find / ! -perm 644
5. 查找所有只读的文件
find . -perm /u=r
6. 查找所有可执行文件
find . -perm /a=x
-mmin n 查找按照分钟时间进行搜索
File's data was last modified less than, more than or exactly n minutes ago.
-mtime n 按照多少天进行搜索
File's data was last modified less than, more than or exactly n*24 hours ago. See the comments for
-atime to understand how rounding affects the interpretation of file modification times.
-prune需要和-path结合使用,并且放在其他条件前面
通过文件的时间来查找
文件的时间分类:
访问时间 (
-atime/
天,
-amin/
分钟):用户最近一次访问时间。修改时间 (
-mtime/
天,
-mmin/
分钟):文件最后一次修改时间。变化时间 (
-ctime/
天,
-cmin/
分钟):文件数据元(例如权限等)最后一次修改时间。
1. 搜索最近七天内被访问过的所有文件
find . -type f -atime -7
2. 搜索恰好在七天前被访问过的所有文件
find . -type f -atime 7
3. 搜索超过七天内被访问过的所有文件
find . -type f -atime +7
4. 搜索访问时间超过10分钟的所有文件
find . -type f -amin +10
5. 找出比file.log修改时间更长的所有文件
find . -type f -newer file.log
6. 以今天开始计算时间查找修改时间大于等于30天的文件
find . -mtime +30 -daystart
基于目录深度搜索
1.
向下最大深度限制为
3
find . -maxdepth 3 -type f
2.
搜索深度距离当前目录至少
2
个子目录的所有文件
find . -mindepth 2 -type f
删除匹配的文件
find . -type f -name "*.txt" -delete
使用 -exec 选项来执行其他命令
语法固定格式:
-exec [other command] {} \;
语法解释:
•
{}
表示匹配到的每一个文件,此处花括号作为占位符
•
\; ;
表示
-exec
选项的结束标记,
\
则用来转义
;
实例:
1. 查找当前目录下,所属用户为root的文件,并把匹配到的每一个文件通过 chown 修改所有者为 tom。
find . -type f -user root -exec chown tom {} \;
2. 查找家目录下面,以 .txt 结尾的文件,并把匹配到的每一个文件执行删除操作,-ok 与 -exec 不同之处在于会提示需要用户是否要执行此操作
find ~ -name "*.txt" -ok rm {} \;
3. 查找当前目录下,以 .txt 结尾的文件,并把匹配到的每一个文件通过 cat 命令显示屏幕上,然后执行 > /all.txt 把结果写入到 all.txt 文件中
find . -type f -name "*.txt" -exec cat {} \;> /all.txt
4. 将30天前的.log文件移动到old目录中
find . -type f -mtime +30 -name "*.log" -exec mv {} old \;
5. 找出当前目录下所有.txt文件并以“File:文件名”的形式打印出来
find . -type f -name "*.txt" -exec printf "
File: %s\n
" {} \;
6.
-exec
后面接
sh
脚本文件,可执行多个命令
-exec ./text.sh {} \;
7. 查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk
find . -path "./sk" -prune -o -name "*.txt" -print
8. 查找指定目录,忽略多个目录,用 -o 连接
find . \( -path ./sk -o -path ./st \) -prune -o -name "*.txt" -print
其他实例:
1. 删除空文件
find . -type f -empty -delete
2. 删除空目录
find . -type d -empty -delete
3. 忽略错误信息,使用 2 > /dev/null 重定向到垃圾桶
find / -name "abc.txt" 2 > /dev/null
4. 在知道部分路径名的情况下,使用 -ipath 来搜索
5. 查找大文件进行删除
find / -type f -size +100M -exec rm -f {} \;
6. 配合 grep 使用
find . -type f -name "*.txt" -exec grep 'Geek' {} \;
关键点说明
1. find
不指定路径,则表示在当前目录查找
2.
文件名最好加上双引号
3. -o
是或的意思,默认不加则为且所有特殊符号都必须要用
\
反斜杠转义
4.
所有特殊符号都必须要用
\
反斜杠转义
5.
.
是当前目录,
~
是家目录,
/
是根目录
记住:
在
shell
中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如
mv
或
rm
命令时,可以使用
-exec
选项的安全模式。它将在对每个匹配到的文件进行操作之前提示
你。
在下面的例子中,
find
命令在当前目录中查找所有文件名以
.LOG
结尾、更改时间在
5
日以上的文件,并删除它们,只不过在删除之前先给出提示。
$ find . -name "*.conf" -mtime +5 -ok rm { } \;
< rm ... ./conf/httpd.conf > ? n
按
y
键删除文件,按
n
键不删除。
任何形式的命令都可以在
-exec
选项中使用。
在下面的例子中我们使用
grep
命令。
find
命令首先匹配所有文件名为
“ passwd*”
的文件,例如
passwd
、
passwd.old
、
passwd.bak
,然后执行
grep
命令看看在这些文件中是否存在一个
sam
用
户。
# find /etc -name "passwd*" -exec grep "sam" { } \;
sam:x:501:501::/usr/sam:/bin/bash