每天一个Linux命令(22)find命令_命令详解

    find命令的一些常用参数的常用实例和用时的注意事项。

    实例:

    (1)-name参数:

      1)[sunjimeng@localhost home]$ find ~ -name "less*" -print           在指定目录和指定目录的子目录中查找与文件名匹配的文件

[sunjimeng@localhost home]$ find ~ -name "less*" -print
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2

      ~是一个代位符,表明的是个人目录的地址,因为每个用户都有自己的个人目录地址,所以用 ~ 作为统一替代这个根据用户不同而不同但有规可循的地址,来保证某些情况下的兼容性问题。

      如果以root登录,则~代表/home/root,若以username登录,则~代表/home/username,例如:我的目录就是/home/sunjimeng。

      linux 中的$PATH $HOME 是什么意思?

      在linux及unix的sh中,以$开头的字符串表示的是sh中定义的变量,这些变量可以是系统自动增加的,也可以是用户自己定义的。 $PATH表示的是系统的命令搜索路径,和windows的%path%是一样的;$HOME则表示是用户的主目录,也就是用户登录后工作目录。

      波浪号~代表了你的$HOME目录。

     2)[sunjimeng@localhost Document]$ find . -name "[A-Z]*[1-9]"        查找当前目录下以大写字母开头,数字结尾的文件。(注意[]符号的用法)

[sunjimeng@localhost Document]$ ll
总用量 12
-rw-r--r--. 1 root      root      85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      root      51 5月  18 02:47 newDir
-rw-r--r--. 1 root      root      42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      root      43 5月  18 02:54 t2.txt
[sunjimeng@localhost Document]$ find . -name "[A-Z]*[1-9]"
./B.text3
./C.text6
[sunjimeng@localhost Document]$ find . -name "[A-Z]*"
./B.text3
./C.text6
./D.text

      3)[sunjimeng@localhost Document]$ find / -name "*"        查找Linux文件系统中的所有文件,让系统高负荷运行

[sunjimeng@localhost Document]$ find / -name "*"

      由于Linux的find命令是通过后台执行操作,遍历整个磁盘文件进行文件的查找,不仅包括名称的查找,还包括其他的例如权限,文件的内容的字符串匹配的查找,因此十分耗费资源,因此这里会让系统高负荷运行。而前几个命令,都是高效查询命令,例如which,whereis等,因为他们不是直接深入磁盘中查找,而是通过数据库的键值对应进行快速查找。

 

    (2)-perm参数

    按照文件权限模式用-perm选项,按文件权限模式来查找文件的话。最好使用八进制的权限表示法。

      1)[root@localhost home]# find . -perm 755 |more -5        按权限在目录中查找文件,并执行分页显示

[root@localhost home]# find . -perm 755 |more -5
.
./sunjimeng/.mozilla
./sunjimeng/.mozilla/extensions
./sunjimeng/.mozilla/plugins
./sunjimeng/.config
--more-- //按q键退出more命令 回车下一行,空格下一页

      2)[root@localhost home]# find / -perm -mode |more -5       根据权限查看文件,当权限值前有-号时

[root@localhost home]# find / -perm 555 |more -5
/
/boot
/proc
/proc/asound
/proc/asound/card0
--more--
[root@localhost home]# find / -perm -555 |more -5
/
/boot
/boot/grub
/boot/grub2
/boot/grub2/themes
--more--
[root@localhost home]# find / -perm -005 |more -5
/
/boot
/boot/grub
/boot/grub2
/boot/grub2/themes
--more--

      find -perm,根据文件的权限来查找文件,有三种形式:

      1.find -perm mode 

      表示严格匹配,也就是你的文件权限位转换成对应的十进制数字与mode一模一样,那么匹配成功,需要注意的是如果mode给的数字不足3位,那么前面自动添0(严格的说是不足4位)

      2.find -perm -mode    

      表示mode中转换成二进制的1在文件权限位里面必须匹配,比如mode=644那么转换成二进制为110 100 100,而被查找的文件的权限位也可以被转换成一个二进制数,两者在位上为1的部分必须完全匹配,而0则不管。例如被查找的文件的权限为转换成二进制数是111 111 111那么这个比如被匹配,而假如是100 100 100那么则不会匹配。所以这个'-'的作用归结起来就是匹配比mode权限更充足的文件

      3.find -perm +mode

      与 -mode的区别是+mode只需其中的任意一个1的部分被匹配,-mode是所有1的部分都必须被匹配,同样+mode也不管0位。

      在linux中文件或目录有三者权限r,w,x,代表的含义分别是读、写、可执行。而一个文件或目录的属性中又包括所属用户u、所属组g、其他o三个部分的属性,分别表示所属用户、所属组、其他用户对这个文件所拥有的权限。看起来大概是这个样子:

   所属用户   所属组    其他
     rwx       rwx      rwx
  用户在其拥有权限的位上设置1,没有权限的位设置0。如果将每个部分的这些权限位看成二进制数,每个部分可以用3位二进制数表示,最大值为7(2^3-1),表示可读、可写、可执行。
 
    (3) -prune参数:
    如果在查找文件时希望忽略某个目录,因为你知道那个目录中没有你所要查找的文件,那么可以使用-prune选项来指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略。
    1)[root@localhost sunjimeng]# find Document -path "Document/newDir" -prune -o -print         查询文件时忽略特定目录
[root@localhost sunjimeng]# find Document
Document
Document/newDir
Document/newDir/mvt1.txt
Document/newDir/mvt2.txt
Document/newDir/mvt3.txt
Document/t1.txt
Document/t2.txt
Document/all.txt
Document/B.text3
Document/C.text6
Document/D.text
[root@localhost sunjimeng]# find Document -path "Document/newDir" -prune -o -print
Document
Document/t1.txt
Document/t2.txt
Document/all.txt
Document/B.text3
Document/C.text6
Document/D.text

      2)[root@localhost sunjimeng]# find Document -path "Document/newDir" -prune        find命令后默认只能跟一个参数命令,如果还需要执行其他的命令,需要-o命令连接符

[root@localhost sunjimeng]# find Document -path "Document/newDir" -prune        //-prune不加-print命令的话,输出的是要忽略的文件夹及其路径
Document/newDir
[root@localhost sunjimeng]# find Document -path "Document/newDir" -prune -o  -exec ls -l {} ; -o用于设置执行的两个连续的命令,这里执行-exec命令,上一个命令执行的是-print命令,也可以换成其他的
总用量 12
-rw-r--r--. 1 root      root      85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      root      51 5月  18 02:47 newDir
-rw-r--r--. 1 root      root      42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      root      43 5月  18 02:54 t2.txt
-rw-r--r--. 1 root root 42 5月  18 02:53 Document/t1.txt
-rw-r--r--. 1 root root 43 5月  18 02:54 Document/t2.txt
-rw-r--r--. 1 root root 85 5月  18 02:58 Document/all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:27 Document/B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:27 Document/C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:28 Document/D.text

      3)[root@localhost Documents]# find . ( -path "./Dir" -o -path "./findDir" ) -prune -o -exec ls -l {} ;      忽略多个文件夹

[root@localhost Documents]# ll
总用量 28
-rw-r--r--. 1 root root  27 5月  19 04:21 core.log
-rw-r--r--. 1 root root   0 5月  19 04:16 find
drwxr-xr-x. 2 root root  84 5月  19 04:57 findDir    //是文件夹
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2
--w-------. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
[root@localhost Documents]# mkdir Dir              //新建一个文件夹
[root@localhost Documents]# mv {head_text,less1,less2} Dir      //将几个文件移到里面
[root@localhost Documents]# ll
总用量 16
-rw-r--r--. 1 root root  27 5月  19 04:21 core.log
drwxr-xr-x. 2 root root  46 5月  19 23:29 Dir
-rw-r--r--. 1 root root   0 5月  19 04:16 find
drwxr-xr-x. 2 root root  84 5月  19 04:57 findDir
--w-------. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
[root@localhost Documents]# find .  -exec ls -l {} ;    //执行一次查询当前Documents文件夹下的所有目录及文件操作
总用量 16
-rw-r--r--. 1 root root  27 5月  19 04:21 core.log
drwxr-xr-x. 2 root root  46 5月  19 23:29 Dir
-rw-r--r--. 1 root root   0 5月  19 04:16 find
drwxr-xr-x. 2 root root  84 5月  19 04:57 findDir
--w-------. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
--w-------. 1 root root 259 5月  12 21:53 ./tail_text
--w-------. 1 root root 216 5月  12 22:24 ./tempory
--w-------. 1 root root 0 5月  15 18:21 ./newlocate
--w-------. 1 root root 0 5月  15 18:34 ./uText
总用量 0
--w-------. 1 root root 0 5月  17 04:18 p1.pdf
--w-------. 1 root root 0 5月  17 04:18 p2.pdf
--w-------. 1 root root 0 5月  17 03:50 t1.txt
--w-------. 1 root root 0 5月  17 04:02 T1.txt
-rw-r--r--. 1 root root 0 5月  19 04:58 t2.txt
--w-------. 1 root root 0 5月  17 04:02 T2.txt
--w-------. 1 root root 0 5月  17 03:50 ./findDir/t1.txt
--w-------. 1 root root 0 5月  17 04:02 ./findDir/T1.txt
--w-------. 1 root root 0 5月  17 04:02 ./findDir/T2.txt
--w-------. 1 root root 0 5月  17 04:18 ./findDir/p1.pdf
--w-------. 1 root root 0 5月  17 04:18 ./findDir/p2.pdf
-rw-r--r--. 1 root root 0 5月  19 04:58 ./findDir/t2.txt
-rw-r--r--. 1 root root 0 5月  19 04:16 ./find
-rw-r--r--. 1 root root 27 5月  19 04:21 ./core.log
-rw-r--r--. 1 root root 85 5月  19 04:25 ./t3.txt
总用量 12
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2
--w-------. 1 root root 664 5月   9 07:59 ./Dir/head_text
--w-------. 1 root root 45 5月   9 08:15 ./Dir/less1
--w-------. 1 root root 57 5月   9 08:16 ./Dir/less2
[root@localhost Documents]# find . ( -path "./Dir" -o -path "./findDir" ) -prune -o -exec ls -l {} ;    //忽略两个文件夹,执行操作
总用量 16
-rw-r--r--. 1 root root  27 5月  19 04:21 core.log
drwxr-xr-x. 2 root root  46 5月  19 23:29 Dir
-rw-r--r--. 1 root root   0 5月  19 04:16 find
drwxr-xr-x. 2 root root  84 5月  19 04:57 findDir
--w-------. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
--w-------. 1 root root 259 5月  12 21:53 ./tail_text
--w-------. 1 root root 216 5月  12 22:24 ./tempory
--w-------. 1 root root 0 5月  15 18:21 ./newlocate
--w-------. 1 root root 0 5月  15 18:34 ./uText
-rw-r--r--. 1 root root 0 5月  19 04:16 ./find
-rw-r--r--. 1 root root 27 5月  19 04:21 ./core.log
-rw-r--r--. 1 root root 85 5月  19 04:25 ./t3.txt
[root@localhost Documents]# find . ( -path "./Dir" -o -path "findDir" ) -prune -o -exec ls -l {} ;不加./是无法进行匹配的,-path “findDir”无意义(根据结果可知)
总用量 16
-rw-r--r--. 1 root root  27 5月  19 04:21 core.log
drwxr-xr-x. 2 root root  46 5月  19 23:29 Dir
-rw-r--r--. 1 root root   0 5月  19 04:16 find
drwxr-xr-x. 2 root root  84 5月  19 04:57 findDir
--w-------. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
--w-------. 1 root root 259 5月  12 21:53 ./tail_text
--w-------. 1 root root 216 5月  12 22:24 ./tempory
--w-------. 1 root root 0 5月  15 18:21 ./newlocate
--w-------. 1 root root 0 5月  15 18:34 ./uText
总用量 0
--w-------. 1 root root 0 5月  17 04:18 p1.pdf
--w-------. 1 root root 0 5月  17 04:18 p2.pdf
--w-------. 1 root root 0 5月  17 03:50 t1.txt
--w-------. 1 root root 0 5月  17 04:02 T1.txt
-rw-r--r--. 1 root root 0 5月  19 04:58 t2.txt
--w-------. 1 root root 0 5月  17 04:02 T2.txt
--w-------. 1 root root 0 5月  17 03:50 ./findDir/t1.txt
--w-------. 1 root root 0 5月  17 04:02 ./findDir/T1.txt
--w-------. 1 root root 0 5月  17 04:02 ./findDir/T2.txt
--w-------. 1 root root 0 5月  17 04:18 ./findDir/p1.pdf
--w-------. 1 root root 0 5月  17 04:18 ./findDir/p2.pdf
-rw-r--r--. 1 root root 0 5月  19 04:58 ./findDir/t2.txt
-rw-r--r--. 1 root root 0 5月  19 04:16 ./find
-rw-r--r--. 1 root root 27 5月  19 04:21 ./core.log
-rw-r--r--. 1 root root 85 5月  19 04:25 ./t3.txt

    (4)-user参数

      1)[root@localhost Document]# find . -user sunjimeng -exec ls -l {} ;        查找属于特定用户的文件及文件夹

[root@localhost Document]# find . -user sunjimeng -exec ls -l {} ;
总用量 12
-rw-r--r--. 1 root      root      85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      root      51 5月  18 02:47 newDir
-rw-r--r--. 1 root      root      42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      root      43 5月  18 02:54 t2.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:27 ./B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:27 ./C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:28 ./D.text
[root@localhost Document]# find . -user root -exec ls -l {} ;
总用量 0
-rw-r--r--. 1 root root 0 5月  18 02:46 mvt1.txt
-rw-r--r--. 1 root root 0 5月  18 02:46 mvt2.txt
-rw-r--r--. 1 root root 0 5月  18 02:46 mvt3.txt
-rw-r--r--. 1 root root 0 5月  18 02:46 ./newDir/mvt1.txt
-rw-r--r--. 1 root root 0 5月  18 02:46 ./newDir/mvt2.txt
-rw-r--r--. 1 root root 0 5月  18 02:46 ./newDir/mvt3.txt
-rw-r--r--. 1 root root 42 5月  18 02:53 ./t1.txt
-rw-r--r--. 1 root root 43 5月  18 02:54 ./t2.txt
-rw-r--r--. 1 root root 85 5月  18 02:58 ./all.txt

      2)[root@localhost /]# find . -nouser -exec ls -l {} ;      查找不属于任何用户的文件

[root@localhost /]# find . -nouser -exec ls -l {} ;                  //这里没找着
find: ‘./proc/37913/task/37913/fd/6’: 没有那个文件或目录
find: ‘./proc/37913/task/37913/fdinfo/6’: 没有那个文件或目录
find: ‘./proc/37913/fd/6’: 没有那个文件或目录
find: ‘./proc/37913/fdinfo/6’: 没有那个文件或目录
find: ‘./run/user/1000/gvfs’: 权限不够

      另外,还有其他的参数,例如按文件的大小-size,按查询路径的深度-depth,按文件的新旧程度-newer,按更改时间或访问时间-mtime,按类型-type等等。

      在此并不一一举例。况且前两篇也有一些示例。

原文地址:https://www.cnblogs.com/MenAngel/p/5512085.html