Shell命令(每天学一个shell命令)第十天 find 命令实例:查找文件或目录

find命令是Linux系统中最重要的也是最常用的命令之一。find命令用于根据你指定的参数搜索和定位文件和目录的列表。find命令可以在多种情况下使用,比如你可以通过权限、用户、用户组、文件类型、日期、大小和其他可能的条件来查找文件。

  • 简单的使用find命令查找指定目录下的某个文件的方法如下所示:

[root@vagrant-centos65 ~]# find /etc -name inittab
/etc/inittab

  • 在当前目录下,查找名称为inittab的文件:

[root@vagrant-centos65 linux_shell_example]# find . -name more.php
./more.php

  • 在当前目录下,文件名不区分大小写是【example】的所有文件:

[root@vagrant-centos65 linux_shell_example]# touch example
[root@vagrant-centos65 linux_shell_example]# touch Example
[root@vagrant-centos65 linux_shell_example]# ls
example Example more.php
[root@vagrant-centos65 linux_shell_example]# find . -iname example
./example
./Example

在此处的选项i可以猜测看成是ignore的缩写,不一定是ignore这个单词,但是可以帮住你记忆。

  • 找出当前目录下,目录名是tmp的目录:

[root@vagrant-centos65 linux_shell_example]# mkdir tmp
[root@vagrant-centos65 linux_shell_example]# ls
example Example more.php tmp
[root@vagrant-centos65 linux_shell_example]# find . -type d -name tmp
./tmp

语法:find . -type d -name tmp
.:代表当前文件夹

-type:类型

d:猜测是dir的意思

  • 找出当前目录下所有的php文件:

[root@vagrant-centos65 linux_shell_example]# touch hello.php
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php more.php tmp
[root@vagrant-centos65 linux_shell_example]# find . -type f -name “*.php”
./hello.php
./more.php

在这里插入图片描述

  • 找出当前目录下,文件权限是777的所有文件:

[root@vagrant-centos65 linux_shell_example]# chmod 777 more.php
[root@vagrant-centos65 linux_shell_example]# find . -type f -perm 0777
./more.php

语法:find . -type f -perm 0777
.:代表当前文件夹

-type:类型

f:猜测是file的意思

perm:猜测是 permission 的意思

  • 找出当前文件夹下,权限不是777的所有文件:

[root@vagrant-centos65 linux_shell_example]# find . -type f -perm 0777
./more.php
[root@vagrant-centos65 linux_shell_example]# find . -type f ! -perm 0777
./hello.php
./example
./Example
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php more.php tmp
[root@vagrant-centos65 linux_shell_example]#

!:应该是编程中“非” 的意思吧

  • 找出当前目录下所有的只读文件:

[root@vagrant-centos65 linux_shell_example]# find . -type f -perm /a+r
./hello.php
./example
./more.php
./Example
[root@vagrant-centos65 linux_shell_example]# ls -ll
total 8
-rw-r–r-- 1 root root 0 Jul 5 03:32 example
-rw-r–r-- 1 root root 0 Jul 5 03:32 Example
-rw-r–r-- 1 root root 0 Jul 5 03:48 hello.php
-rwxrwxrwx. 1 root root 759 Jun 19 07:10 more.php
drwxr-xr-x 2 root root 4096 Jul 5 03:43 tmp

  • 找出当前文件夹中所有的可执行文件

[root@vagrant-centos65 linux_shell_example]# find . -type f -perm /a+x
./more.php
[root@vagrant-centos65 linux_shell_example]# ls -ll
total 8
-rw-r–r-- 1 root root 0 Jul 5 03:32 example
-rw-r–r-- 1 root root 0 Jul 5 03:32 Example
-r–r--r-- 1 root root 0 Jul 5 03:48 hello.php
-rwxrwxrwx. 1 root root 759 Jun 19 07:10 more.php
drwxr-xr-x 2 root root 4096 Jul 5 03:43 tmp

  • 找出当前目录下所有的 .log文件并将其删除:

[root@vagrant-centos65 linux_shell_example]# touch chat.log
[root@vagrant-centos65 linux_shell_example]# touch secret.log
[root@vagrant-centos65 linux_shell_example]# touch access.log
[root@vagrant-centos65 linux_shell_example]# ls -ll
total 8
-rw-r–r-- 1 root root 0 Jul 5 07:04 access.log
-rw-r–r-- 1 root root 0 Jul 5 07:04 chat.log
-rw-r–r-- 1 root root 0 Jul 5 03:32 example
-rw-r–r-- 1 root root 0 Jul 5 03:32 Example
-r–r--r-- 1 root root 0 Jul 5 03:48 hello.php
-rwxrwxrwx. 1 root root 759 Jun 19 07:10 more.php
-rw-r–r-- 1 root root 0 Jul 5 07:04 secret.log
drwxr-xr-x 2 root root 4096 Jul 5 03:43 tmp
[root@vagrant-centos65 linux_shell_example]# clear
[root@vagrant-centos65 linux_shell_example]# find . -type f -name “*.log” -exec rm -f {} ;
[root@vagrant-centos65 linux_shell_example]# ls
example Example hello.php more.php tmp

  • 找出当前目录下所有的空文件:

[root@vagrant-centos65 linux_shell_example]# find . -type f -empty
./hello.php
./example
./Example
[root@vagrant-centos65 linux_shell_example]# ls -ll
total 8
-rw-r–r-- 1 root root 0 Jul 5 03:32 example
-rw-r–r-- 1 root root 0 Jul 5 03:32 Example
-r–r--r-- 1 root root 0 Jul 5 03:48 hello.php
-rwxrwxrwx. 1 root root 759 Jun 19 07:10 more.php
drwxr-xr-x 2 root root 4096 Jul 5 03:43 tmp

  • 找出当前目录下所有的空目录:

[root@vagrant-centos65 linux_shell_example]# find -type d -empty
./tmp

  • 找出当前文件夹下所有的隐藏文件:

[root@vagrant-centos65 linux_shell_example]# touch .williams
[root@vagrant-centos65 linux_shell_example]# ls -all
total 16
drwxr-xr-x. 3 root root 4096 Jul 5 07:29 .
drwxr-xr-x. 12 root root 4096 Jun 19 02:01 …
-rw-r–r-- 1 root root 0 Jul 5 03:32 example
-rw-r–r-- 1 root root 0 Jul 5 03:32 Example
-r–r--r-- 1 root root 0 Jul 5 03:48 hello.php
-rwxrwxrwx. 1 root root 759 Jun 19 07:10 more.php
drwxr-xr-x 2 root root 4096 Jul 5 03:43 tmp
-rw-r–r-- 1 root root 0 Jul 5 07:29 .williams
[root@vagrant-centos65 linux_shell_example]# find -type f -name “.*”
./.williams

  • 找出当前目录下所有者是root的文件和目录:

[root@vagrant-centos65 linux_shell_example]# find . -user root
.
./hello.php
./tmp
./example
./more.php
./.williams
./Example

  • 找出当前目录下,用户组是developer的文件和目录:

[root@vagrant-centos65 linux_shell_example]# find . -group developer
find: `developer’ is not the name of an existing group

提示我当前的系统中,不存在一个叫developer的用户组

  • 找出当前账号的主目录下,3天前修改的文件:

[root@vagrant-centos65 linux_shell_example]# find ~ -type f -mtime 3

  • 找出当前用户主目录下,30天以前修改的所有文件:

find ~ -type f -mtime +30

  • 找到当前用户主目录下,3天内修改的所有文件:

find . -type f -mtime -3

  • 找到当前用户主目录下,30天以前60天以内修改的所有文件:

[root@vagrant-centos65 linux_shell_example]# find . -type f -mtime +30 -mtime -60

  • 找出当前目录下,一小时内变更过的文件:

[root@vagrant-centos65 linux_shell_example]# find . -type f -cmin -60
./.williams

  • 找出当前目录下,一小时内访问过的文件:

[root@vagrant-centos65 linux_shell_example]# find . -type f -amin -60
./.williams

以上内容到此结束,感谢这么优秀的你还在看我的博客,喜欢的打赏一下吧。哈哈在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值