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
以上内容到此结束,感谢这么优秀的你还在看我的博客,喜欢的打赏一下吧。哈哈