Linux学习笔记8(Find命令进阶学习)

目录

通过所有者来查找

1. 指定所属的用户

2. 指定所属的组

通过权限来查找

1. 指定精确的权限查找

2. 指定所有者(用户、组、其他人)至少有一个拥有此权限即可

3. 指定文件最低权限查找,即大于等于

4. 查找文件不是指定的权限(取反)

5. 查找所有只读的文件

6. 查找所有可执行文件

-mmin n 查找按照分钟时间进行搜索

-mtime n 按照多少天进行搜索

通过文件的时间来查找

文件的时间分类:

1. 搜索最近七天内被访问过的所有文件

2. 搜索恰好在七天前被访问过的所有文件

3. 搜索超过七天内被访问过的所有文件

4. 搜索访问时间超过10分钟的所有文件

5. 找出比file.log修改时间更长的所有文件

6. 以今天开始计算时间查找修改时间大于等于30天的文件

基于目录深度搜索

删除匹配的文件

使用 -exec 选项来执行其他命令

实例:

1. 查找当前目录下,所属用户为root的文件,并把匹配到的每一个文件通过 chown 修改所有者为 tom。

2. 查找家目录下面,以 .txt 结尾的文件,并把匹配到的每一个文件执行删除操作,-ok 与 -exec 不同之处在于会提示需要用户是否要执行此操作

3. 查找当前目录下,以 .txt 结尾的文件,并把匹配到的每一个文件通过 cat 命令显示屏幕上,然后执行 > /all.txt 把结果写入到 all.txt 文件中

4. 将30天前的.log文件移动到old目录中

5. 找出当前目录下所有.txt文件并以“File:文件名”的形式打印出来

7. 查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk

8. 查找指定目录,忽略多个目录,用 -o 连接

其他实例:

1. 删除空文件

2. 删除空目录

3. 忽略错误信息,使用 2 > /dev/null 重定向到垃圾桶

4. 在知道部分路径名的情况下,使用 -ipath 来搜索

5. 查找大文件进行删除

6. 配合 grep 使用

关键点说明

记住:


-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 来搜索

find / -type d -name 'img' -ipath "*public_html/ example.com *"

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值