Linux find
  wwLZeziuqjLR 2023年11月02日 25 0

1.find介绍

linux查找命令find是linux运维中很重要、很常用的命令之一,find用于根据指定条件的匹配参数来搜索和查找文件和目录列表,我们可以通过权限、用户、用户组、文件类型、日期、大小等条件来查找文件。

2.find语法

find语法

find [查找路径] [查找条件] [处理动作]

查找路径:指定的具体目标路径 

查找条件:指定的查找标准,可以是权限、用户、用户组、文件类型、日期、大小等标准进行 

处理动作:对符合条件的文件做相关操作,比如:输出屏幕,存放至某个文件中

3.样例

3.1.根据文件名称,使用find命令进行查找

->在当前工作目录中查找名称为ztj.txt的文件

命令:

find ztj.txt

OR

find . -name ztj.txt -maxdepth 1

[root@rhel77 ~]# find ztj.txt
ztj.txt
[root@rhel77 ~]# find . -name ztj.txt -maxdepth 1
find: warning: you have specified the -maxdepth option after a non-option argument -name, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

./ztj.txt
[root@rhel77 ~]#

->在/root目录下,查找名称为ztj.txt的所有文件

命令:

find /root -name ztj.txt

[root@rhel77 ~]# find /root -name ztj.txt
/root/1/ztj.txt
/root/ztj.txt
/root/ztj11/ztj.txt
[root@rhel77 ~]#

->在/root目录中查找名称为z的所有目录

命令:

find /root -type d -name z

[root@rhel77 ~]# find /root -type d -name z
/root/z
[root@rhel77 ~]#

其中,-type类型参数介绍如下:

f:普通文件

l:符号链接

d:目录

c:字符设备

b:块设备

s:套接字

->在/root目录下,查找所有以.zip和.log为扩展名的文件

命令:

find /root -type f \( -name "*.zip" -o -name "*.log" \) -ls

[root@rhel77 ~]# find /root -type f \( -name "*.zip" -o -name "*.log" \) -ls
1731369   80 -rw-r--r--   1 root     root        81141 May  4 16:13 /root/wordpress_auto_install.log
1731610    4 -rw-r--r--   1 root     root         2059 Jul 28 15:14 /root/ztj.zip
3269480    4 -rw-r--r--   1 root     root          586 May 17 22:19 /root/disk.log
3257826   40 -rw-r--r--   1 root     root        40739 May 25 14:01 /root/extundelete-0.2.4/config.log
1731382    4 -rw-r--r--   1 root     root          642 Jul 21 14:04 /root/1.zip
1731605    4 -rw-r--r--   1 root     root          106 Aug 10 09:10 /root/urandom.log
1731400    4 -rw-r--r--   1 root     root         1475 Jul 21 14:08 /root/2.zip
    70    4 -rw-r--r--   1 root     root         3334 Jul 20 08:50 /root/z.zip
3256369    4 -rw-r--r--   1 root     root           48 May 19 09:07 /root/zt1.log
[root@rhel77 ~]#

->在/root目录下,查找所有以.png,.jpg,.php和.js为扩展名的文件

命令:

find /root -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.php" -o -name "*.js" \) -ls
 

[root@rhel77 ~]# find /root -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.php" -o -name "*.js" \) -ls
3247961    0 -rw-r--r--   1 root     root            0 Aug 17 14:25 /root/1.png
3247965    0 -rw-r--r--   1 root     root            0 Aug 17 14:25 /root/2.jpg
3247966    0 -rw-r--r--   1 root     root            0 Aug 17 14:25 /root/3.php
3247967    0 -rw-r--r--   1 root     root            0 Aug 17 14:25 /root/4.js
[root@rhel77 ~]#

3.2.根据文件权限,使用find命令进行查找

->在/root目录下,查找权限为777的文件

命令:

find /root -type f -perm 0777  -maxdepth 1 -ls

[root@rhel77 ~]# find /root -type f -perm 0777  -maxdepth 1 -ls
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

1731602  160 -rwxrwxrwx   1 root     root       160059 Aug 16 09:56 /root/abc.txt
3256377    4 -rwxrwxrwx   1 root     root          259 May 23 16:48 /root/user_info.txt
[root@rhel77 ~]#

->在/root目录下,查找权限不是777的文件

命令:

find /root -type f ! -perm 777  -maxdepth 1 -ls

[root@rhel77 ~]# find /root -type f ! -perm 777  -maxdepth 1 -ls | tail -n 5
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

3256384    4 -rwxr-xr-x   1 root     root          988 May 23 22:33 /root/fdisk.sh
3257852    4 -rw-r--r--   1 root     root           62 May 27 14:42 /root/1234.txt
430437    4 -rwxr-xr-x   1 root     root           64 Jun  5 21:23 /root/ztj-1.sh
1731615    4 -rwxr-xr-x   1 root     root           21 Jun  7 13:55 /root/uuidgen.sh
430376    4 -rw-r--r--   1 root     root           41 Jul 21 13:59 /root/zzz.txt.bz2
[root@rhel77 ~]#

->在/root目录下,查找权限为777的目录,将其权限设置为755

命令:

find /root -type d -perm 777 -maxdepth 1 -ls -exec chmod 755 {} \;
 

[root@rhel77 ~]# find /root -type d -perm 777 -maxdepth 1 -ls -exec chmod 755 {} \;
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

    69   12 drwxrwxrwx  24 root     root         8192 Aug 16 09:27 /root
1729025    0 drwxrwxrwx   2 root     root           90 Jul 20 10:29 /root/test

->在/root目录下,查找名称为cc.txt文件,并将其删除

命令:

find /root -type f -name cc.txt -maxdepth 1 | xargs rm -rf

[root@rhel77 ~]# find /root -type f -name cc.txt -maxdepth 1 | xargs rm -rf
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

[root@rhel77 ~]#

OR

find /root -type f -name cc.txt -maxdepth 1 -exec rm -rf {} \;

[root@rhel77 ~]# find /root -type f -name cc.txt -maxdepth 1 -exec rm -rf {} \;
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

[root@rhel77 ~]#

->在/root目录下,找出权限是600的文件

命令:

find /root/ -perm 600 -ls

[root@rhel77 ~]# find /root/ -perm 600 -ls
3698739    4 -rw-------   1 root     root         1679 Apr 27 14:09 /root/.ssh/id_rsa
  1601   16 -rw-------   1 root     root        14605 Aug 17 10:03 /root/.bash_history
3247958    8 -rw-------   1 root     root         5648 Aug 17 10:03 /root/.viminfo
1731372    0 -rw-------   1 alice    alice           0 May 27 21:04 /root/test/alice.txt
202108776    4 -rw-------   1 root     root           11 Aug 17 13:32 /root/.cache/abrt/lastnotification
542180    4 -rw-------   1 root     root          312 Aug 10 08:51 /root/.lesshst
3247962    4 -rw-------   1 root     root          312 Aug 17 13:32 /root/.Xauthority
1731398    4 -rw-------   1 root     root          306 Aug 10 09:08 /root/nohup.out
3256372    4 -rw-------   1 root     root           15 Jul 17 08:35 /root/.lvm_history
3547010    4 -rw-------   1 root     root           18 Jun  4 12:00 /root/.mysql_history
[root@rhel77 ~]#

上述命令(find /root/ -perm 600 -ls)会找到所有只有rw-------权限的文件。

另外,

如下命令:

find /root/ -perm -600 -ls

-600表示列出只要是包括了rw的其它位任意的文件

[root@rhel77 ~]# find /root/ -perm -600 -ls |head -n 10
    69   16 drwxr-xr-x  24 root     root        12288 Aug 17 13:32 /root/
3180996    4 -rwxr-xr-x   1 root     root           79 Apr 11 09:55 /root/access.sh
135138217    0 drwxr-xr-x   2 root     root            6 Apr 24 19:08 /root/110
502725    4 -rw-r--r--   1 root     root           18 Dec 29  2013 /root/.bash_logout
542640    4 -rwxr-xr-x   1 root     root          192 Mar 15 09:25 /root/earth-1.sh
1729048    4 -rw-r--r--   1 root     root           57 Mar 16 15:57 /root/symlinks-1.list
542203    4 -rwxr-xr-x   1 root     root           94 Mar 30 13:57 /root/apache-1.sh
3193035    4 -rwxr-xr-x   1 root     root          207 Apr 18 22:22 /root/spawn.sh
502727    4 -rw-r--r--   1 root     root          176 Dec 29  2013 /root/.bashrc
3181007    4 -rwxr-xr-x   1 root     root          155 Apr 11 14:05 /root/case-2.sh
[root@rhel77 ~]#

如下命令:

find /root/ -perm /600 -ls

/600表示列出指定的权限符合r--------或w--------的文件。

[root@rhel77 ~]# find /root/ -perm /600 -ls |head -n 10
    69   16 drwxr-xr-x  24 root     root        12288 Aug 17 13:32 /root/
3180996    4 -rwxr-xr-x   1 root     root           79 Apr 11 09:55 /root/access.sh
135138217    0 drwxr-xr-x   2 root     root            6 Apr 24 19:08 /root/110
502725    4 -rw-r--r--   1 root     root           18 Dec 29  2013 /root/.bash_logout
542640    4 -rwxr-xr-x   1 root     root          192 Mar 15 09:25 /root/earth-1.sh
1729048    4 -rw-r--r--   1 root     root           57 Mar 16 15:57 /root/symlinks-1.list
542203    4 -rwxr-xr-x   1 root     root           94 Mar 30 13:57 /root/apache-1.sh
3193035    4 -rwxr-xr-x   1 root     root          207 Apr 18 22:22 /root/spawn.sh
502727    4 -rw-r--r--   1 root     root          176 Dec 29  2013 /root/.bashrc
3181007    4 -rwxr-xr-x   1 root     root          155 Apr 11 14:05 /root/case-2.sh
[root@rhel77 ~]#

3.3.基于文件所有者和所属组,使用find命令进行查找

->在/root目录下,找出文件所有者为root,名称为ztj.txt的文件

命令:

find /root/ -user root -name ztj.txt -ls

[root@rhel77 ~]# find /root/ -user root -name ztj.txt -ls
68916551    4 -rw-r--r--   1 root     root           28 Mar 30 16:54 /root/1/ztj.txt
3247948    0 -rw-r--r--   1 root     root            0 Aug  4 10:00 /root/ztj.txt
203522440    4 -rw-r--r--   1 root     root            4 Apr 23  2022 /root/ztj11/ztj.txt
[root@rhel77 ~]#

->在/root目录下,找出文件属组为root,文件名称为process.txt的文件

命令:

find /root/ -group root -name process.txt -ls

[root@rhel77 ~]# find /root/ -group root -name process.txt -ls
1731367   48 -rw-r--r--   1 root     root        48638 May 16 16:43 /root/process.txt
[root@rhel77 ~]#

->在/root目录,查找所有者为root的所有.txt文件

命令:

find /root -user root -iname "*.txt" -ls

其中,

-iname:忽略name的大小写版本

[root@rhel77 ~]# find /root -user root -iname "*.txt" -ls 
1731370    4 -rw-r--r--   3 root     root            8 May 27 21:29 /root/test/a.txt
1731370    4 -rw-r--r--   3 root     root            8 May 27 21:29 /root/test/b.txt
1731370    4 -rw-r--r--   3 root     root            8 May 27 21:29 /root/test/d.txt
3181009    4 -rw-r--r--   1 root     root           14 Mar 23 15:18 /root/all.txt
3193028    4 -rw-r--r--   1 root     root          604 Apr 18 13:36 /root/system.txt
542186    4 -rw-r--r--   1 root     root            6 Mar  1 14:33 /root/bb.txt
1555878    4 -rw-r--r--   1 root     root            9 Jun  5 21:41 /root/user.txt
3193757    4 -rw-r--r--   1 root     root          154 Apr 26 08:56 /root/test.txt
137253126    4 -rw-r--r--   1 root     root           37 Aug  4 09:57 /root/123/zzz.txt
137119752    4 -rw-r--r--   1 root     root            6 Mar 15 10:12 /root/2/bb.txt
430370    4 -rw-r--r--   1 root     root           37 Aug  4 09:55 /root/zzz.txt
137259616    4 -rw-r--r--   1 root     root            3 May 27 17:05 /root/z/aa.txt
68916537    4 -rw-r--r--   1 root     root            2 Mar 30 16:54 /root/1/aa.txt
68916539    4 -rw-r--r--   1 root     root           14 Mar 30 16:54 /root/1/all.txt
68916543    4 -rw-r--r--   1 root     root            6 Mar 30 16:54 /root/1/bb.txt
68916545    4 -rw-r--r--   1 root     root          464 Mar 30 16:54 /root/1/cc.txt
68916547    4 -rw-r--r--   1 root     root            6 Mar 30 16:54 /root/1/user.txt
68916551    4 -rw-r--r--   1 root     root           28 Mar 30 16:54 /root/1/ztj.txt
3698754    4 -r--r--r--   1 root     root           10 Apr 28 08:58 /root/gg.txt
1731602  180 -rwxrwxrwx   1 root     root       183335 Aug 17 13:54 /root/abc.txt
1731374    4 -rw-r--r--   1 root     root           58 May 13 20:12 /root/online.txt
1731378    4 -rw-r--r--   1 root     root         1636 May 13 20:12 /root/offline.txt
3269441    4 -rw-r--r--   1 root     root          132 May  9 22:15 /root/off.txt
3269442    4 -rw-r--r--   1 root     root            1 May  9 22:15 /root/on.txt
3269445    4 -rw-r--r--   1 root     root         1794 May 19 09:18 /root/a.txt
1731367   48 -rw-r--r--   1 root     root        48638 May 16 16:43 /root/process.txt
3247948    0 -rw-r--r--   1 root     root            0 Aug  4 10:00 /root/ztj.txt
1731387    4 -rw-r--r--   1 root     root          361 May 29 19:53 /root/1.txt
3193031    4 -rw-r--r--   1 root     root           56 Apr 20 10:52 /root/v.txt
1731403 512000 -rw-r--r--   1 root     root     524288000 May 31 08:42 /root/boc.txt
1729071    0 -rw-r--r--   1 root     root            0 Jul 20 08:33 /root/DDA/ztj/1.txt
1731379    0 -rw-r--r--   1 root     root            0 Jul 20 08:34 /root/DDA/ztj/2.txt
1731391    0 -rw-r--r--   1 root     root            0 Jul 20 08:34 /root/DDA/ztj/3.txt
1731394    0 -rw-r--r--   1 root     root            0 Jul 20 08:34 /root/DDA/ztj/4.txt
1731404    0 -rw-r--r--   1 root     root            0 Jul 20 08:34 /root/DDA/ztj/5.txt
1731406    0 -rw-r--r--   1 root     root            0 Jul 20 08:34 /root/DDA/ztj/6.txt
3698742    4 -rw-r--r--   1 root     root          102 Apr 23 21:12 /root/brace.txt
3247960    0 -rw-r--r--   1 root     root            0 Aug 17 13:53 /root/tttt.TXT
203522440    4 -rw-r--r--   1 root     root            4 Apr 23  2022 /root/ztj11/ztj.txt
203522446    0 lrwxrwxrwx   1 root     root            7 Apr 23  2022 /root/ztj11/aa.txt -> ztj.txt
3256377    4 -rwxrwxrwx   1 root     root          259 May 23 16:48 /root/user_info.txt
430371    4 -rw-r--r--   1 root     root         2137 Aug  4 09:55 /root/zzza.txt
3698759    0 lrwxrwxrwx   1 root     root            7 Apr 28 09:06 /root/hh.txt -> ztj.txt
430369    4 -rw-r--r--   1 root     root           23 Jul 19 08:38 /root/ztj1.txt
3269443    4 -rw-r--r--   1 root     root           52 May 19 09:18 /root/b.txt
1731368    4 -rw-r--r--   1 root     root          892 May 16 16:56 /root/stat.txt
3256365    4 -rw-r--r--   1 root     root            6 May 18 21:01 /root/22.txt
3257852    4 -rw-r--r--   1 root     root           62 May 27 14:42 /root/1234.txt
[root@rhel77 ~]#

3.4.根据文件日期和时间,使用find命令进行查找

linux系统里面的文件都有三种属性:
访问(access)时间(-atime/天,-amin/分钟):用户最近一次访问时间
修改(modify)时间(-mtime/天,-mmin/分钟):文件最后一次修改时间
变化(change)时间(-ctime/天,-cmin/分钟):文件元数据(例如权限、属性等)最后一次修改时间

->在/root目录下,查找50天外修改的所有文件

命令:

find /root -mtime +50 -ls

[root@rhel77 ~]# find /root -mtime +50 -ls |head -n 10
3180996    4 -rwxr-xr-x   1 root     root           79 Apr 11 09:55 /root/access.sh
135138217    0 drwxr-xr-x   2 root     root            6 Apr 24 19:08 /root/110
502725    4 -rw-r--r--   1 root     root           18 Dec 29  2013 /root/.bash_logout
542640    4 -rwxr-xr-x   1 root     root          192 Mar 15 09:25 /root/earth-1.sh
1729048    4 -rw-r--r--   1 root     root           57 Mar 16 15:57 /root/symlinks-1.list
542203    4 -rwxr-xr-x   1 root     root           94 Mar 30 13:57 /root/apache-1.sh
3193035    4 -rwxr-xr-x   1 root     root          207 Apr 18 22:22 /root/spawn.sh
502727    4 -rw-r--r--   1 root     root          176 Dec 29  2013 /root/.bashrc
3181007    4 -rwxr-xr-x   1 root     root          155 Apr 11 14:05 /root/case-2.sh
502728    4 -rw-r--r--   1 root     root          100 Dec 29  2013 /root/.cshrc
[root@rhel77 ~]#

->在/root目录下,查找最近1小时内修改的所有文件

命令:

find /root -mmin -60 -ls
 

[root@rhel77 ~]# find /root -mmin -60 -ls
    69   16 drwxr-xr-x  24 root     root        12288 Aug 17 13:53 /root
201482216    0 drwxr-xr-x   2 root     root           30 Aug 17 13:32 /root/.cache/abrt
202108776    4 -rw-------   1 root     root           11 Aug 17 13:32 /root/.cache/abrt/lastnotification
3247962    4 -rw-------   1 root     root          312 Aug 17 13:32 /root/.Xauthority
1731602  180 -rwxrwxrwx   1 root     root       184083 Aug 17 14:11 /root/abc.txt
3247960    0 -rw-r--r--   1 root     root            0 Aug 17 13:53 /root/tttt.TXT
[root@rhel77 ~]#

3.5.根据文件大小,使用find命令进行查找

->在/root目录下,找到大小为15MB的文件

命令:

find /root -size 15M -ls

[root@rhel77 ~]# find /root -size 15M -ls
3268520 14924 -rw-r--r--   1 root     root     15279408 Jun  3 20:18 /root/mysql-community-server-minimal-5.7.42-1.el7.x86_64.rpm
[root@rhel77 ~]#

->在/root目录下,找到大于1MB且小于16MB的所有文件

命令:

find /root -size +1M -size -16M -ls

[root@rhel77 ~]# find /root -size +1M -size -16M -ls
3268520 14924 -rw-r--r--   1 root     root     15279408 Jun  3 20:18 /root/mysql-community-server-minimal-5.7.42-1.el7.x86_64.rpm
3698723 8872 -rw-r--r--   1 root     root      9082696 Apr 30 07:09 /root/wordpress-4.9.4-zh_CN.tar.gz
69023364 1860 -rw-r--r--   1 root     root      1904320 May 25 14:01 /root/extundelete-0.2.4/src/extundelete-extundelete.o
69023371 1296 -rwxr-xr-x   1 root     root      1323312 May 25 14:01 /root/extundelete-0.2.4/src/extundelete
1731386 10224 -rw-r--r--   1 root     root     10466248 May 29 13:50 /root/aa.tar.gz
[root@rhel77 ~]#

3.6.基于inode删除ztj.txt文件(使用频率居高)

命令:

ls -li ztj.txt

find /root -inum 3247948 -exec rm -rf {} \;

[root@rhel77 ~]# pwd
/root
[root@rhel77 ~]# ls -li ztj.txt
3247948 -rw-r--r-- 1 root root 0 Aug  4 10:00 ztj.txt
[root@rhel77 ~]# find /root -inum 3247948 -ls
3247948    0 -rw-r--r--   1 root     root            0 Aug  4 10:00 /root/ztj.txt
[root@rhel77 ~]# find /root -inum 3247948 -exec rm -rf {} \;
[root@rhel77 ~]# ls -li ztj.txt
ls: cannot access ztj.txt: No such file or directory
[root@rhel77 ~]#



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论