浅谈linux下find的最基本用法

find是个使用频率比较高的命令。常常用它在系统特定目录下,查找具有某种特征【名字类型属主权限等】的文件。
find命令的格式: find  [-path ..] -options [-print -exec -ok]
path:要查找的目录路径。
~ 表示$HOME目录
. 表示当前目录
/ 表示根目录
-print :表示将结果输出到标准输出
-exec :对匹配的文件执行该参数所给出的shell命令。形式为 command  {} \; ,注意{}与\; 之间有空格
-ok :与-exec作用相同,区别在于,在执行命令之前,都会给出提示,让用户确认是否执行

options常用的有下选项:
-name 按照名字查找
-perm 安装权限查找
-prune 不再当前指定的目录下查找
-user 文件属主来查找
-group 所属组来查找
-nogroup 查找无有效所属组的文件
-nouser 查找无有效属主的文件

-type 按照文件类型查找


下面通过一些简单的例子来介绍下find的常规用法:

1、按名字查找

在当前目录及子目录中,查找.c文件

-rwxrwxr-x. 1 hj hj 4673 Jul 15 05:20 test
-rwxrwxr-x. 1 hj hj 4880 Jul 15 06:17 test2
-rw-rw-r--. 1 hj hj  171 Jul 15 06:17 test2.c
-rw-rw-r--. 1 hj hj   65 Jul 12 07:55 test.c
[hj@localhost work]$ find . -name '*.c' -print
./test2.c
./test.c

2、按目录查找

在当前目录除test.c之外的子目录(文件)内搜索 .c文件

-rwxrwxr-x. 1 hj hj 4673 Jul 15 05:20 test
-rwxrwxr-x. 1 hj hj 4880 Jul 15 06:17 test2
-rw-rw-r--. 1 hj hj  171 Jul 15 06:17 test2.c
-rw-rw-r--. 1 hj hj   65 Jul 12 07:55 test.c
[hj@localhost work]$ find . -path "./test.c" -prune -o -name "*.c" -print
./test2.c
[hj@localhost work]$ 
3、按权限查找

在当前目录及子目录中,查找属主具有读写执行,所在组具有读写执行,其他具有读执行权限的文件

[hj@localhost work]$ ls -l
total 24
-rwxrwxr-x. 1 hj hj 4673 Jul 15 05:20 test
-rwxrwxr-x. 1 hj hj 4880 Jul 15 06:17 test2
-rw-rw-r--. 1 hj hj  171 Jul 15 06:17 test2.c
-rw-rw-r--. 1 hj hj   65 Jul 12 07:55 test.c
[hj@localhost work]$ find . -perm 775 -print
.
./test2
./test
[hj@localhost work]$
4、按时间查找

在当前目录下查找2天内被更改过的文件

[hj@localhost work]$ ls -l
total 24
-rwxrwxr-x. 1 hj hj 4673 Jul 15 05:20 test
-rwxrwxr-x. 1 hj hj 4880 Jul 15 06:17 test2
-rw-rw-r--. 1 hj hj  171 Jul 15 06:17 test2.c
-rw-rw-r--. 1 hj hj   65 Jul 12 07:55 test.c
[hj@localhost work]$ find . -mtime -2 -type f -print
./test2.c
./test2
./test
[hj@localhost work]$ 

5、按大小查找

在当前目录下查找小于20k的文件

[hj@localhost work]$ ls -l
total 24
-rwxrwxr-x. 1 hj hj 4673 Jul 15 05:20 test
-rwxrwxr-x. 1 hj hj 4880 Jul 15 06:17 test2
-rw-rw-r--. 1 hj hj  171 Jul 15 06:17 test2.c
-rw-rw-r--. 1 hj hj   65 Jul 12 07:55 test.c
[hj@localhost work]$ find . -size -20k -print
.
./test2.c
./test2
./test
./test.c
[hj@localhost work]$ 

6、查找属主被删除的文件

hj@localhost work]$ find / -nouser -type f  -print
find: `/etc/audit': Permission denied
find: `/etc/sudoers.d': Permission denied
find: `/etc/pki/rsyslog': Permission denied
find: `/etc/pki/CA/private': Permission denied
find: `/etc/ntp/crypto': Permission denied
find: `/etc/vmware-tools/GuestProxyData/trusted': Permission denied
find: `/etc/polkit-1/localauthority': Permission denied
find: `/etc/selinux/targeted/modules/active': Permission denied
find: `/etc/cups/ssl': Permission denied
find: `/etc/dhcp': Permission denied
find: `/etc/audisp': Permission denied

7、执行命令

查找test.c并删除,删除前提示确认

[hj@localhost work]$ ls -l
total 24
-rwxrwxr-x. 1 hj hj 4673 Jul 15 05:20 test
-rwxrwxr-x. 1 hj hj 4880 Jul 15 06:17 test2
-rw-rw-r--. 1 hj hj  171 Jul 15 06:17 test2.c
-rw-rw-r--. 1 hj hj   65 Jul 12 07:55 test.c
[hj@localhost work]$ find . -name 'test.c' -ok rm {} \;
< rm ... ./test.c > ? y
[hj@localhost work]$ ls -l
total 20
-rwxrwxr-x. 1 hj hj 4673 Jul 15 05:20 test
-rwxrwxr-x. 1 hj hj 4880 Jul 15 06:17 test2
-rw-rw-r--. 1 hj hj  171 Jul 15 06:17 test2.c
[hj@localhost work]$ 








  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值