Linux中的find命令

本文详细介绍了 Linux 下 find 命令的使用方法,包括如何利用通配符进行精确搜索、按所有者、时间戳及文件大小查找文件等高级用法。通过实战案例演示了 find 命令的强大功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 语法

find [搜索范围] [搜索条件]
搜索文件
注意:
1、避免大范围搜索,会非常耗费系统资源。
2、find是在系统当中搜索符号条件的文件名。如果匹配,使用通配符匹配, 通配符匹配是完全匹配。
 
二 Linux中的通配符
          匹配任意内容
?          匹配任意一个字符
[]         匹配任意一个中括号的字符
 
三 举例
1、不区分大小写
find /root -iname install.log
2、按照所有者搜索
find /root -user root
3、查找没有所有者的文件(垃圾文件,/sys或/proc中的文件,外来文件如U盘)
find /root -nouser
4、根据事件查找文件
find /var/log -mtime +10
-10 10天内修改文件
10   10天当天修改的文件
+10 10天前修改的文件
atime 文件访问时间
ctime 改变文件属性
mtime 修改文件内容
5、按照大小搜索
find . -size 25k
查找文件大小是25KB的文件
-25k  小于25KB的文件
25k   等于25KB的文件
+25k 大于25KB的文件
6、根据i节点查找
find . -inum 26743
7、比较复杂的查找


 
 
四 实战
  1. [root@localhost ~]# find ChangeLog-2.6.0
  2. ChangeLog-2.6.0
  3. [root@localhost ~]# find -name "Cha*"
  4. ./ChangeLog-2.6.0
  5. [root@localhost test]# ls
  6. abc abcdef abc.soft
  7. [root@localhost test]# find -name "abc[d-f]"
  8. [root@localhost test]# find -name "abc[d-f]*"
  9. ./abcdef
  10. [root@localhost test]# find -user root
  11. .
  12. ./abc
  13. ./abc.soft
  14. ./abcdef
  15. [root@localhost test]# find -nouser
  16. [root@localhost test]# find -mtime -10
  17. .
  18. ./abc
  19. ./abc.soft
  20. ./abcdef
  21. [root@localhost test]# find .-size -25k
  22. .
  23. ./abc
  24. ./abc.soft
  25. ./abcdef
  26. [root@localhost test]# find .-size +1k
  27. [root@localhost test]# find .-size -1M
  28. ./abc
  29. ./abcdef
  30. ./1k
  31. [root@localhost test]# find .-size -1m
  32. find: invalid -size type `m'
  33. [root@localhost test]# find . -size -1K
  34. find: invalid -size type `K'
  35. [root@localhost test]# ls -i
  36. 67170464 1k 67170460 abc 67170463 abcdef 67170462 abc.soft
  37. [root@localhost test]#
  38. [root@localhost test]#
  39. [root@localhost test]#
  40. [root@localhost test]# find . -inum 67170463
  41. ./abcdef
  42. [root@localhost test]# find /etc -size +20k -a -size -50k
  43. /etc/selinux/targeted/modules/active/modules/apache.pp
  44. /etc/selinux/targeted/modules/active/modules/init.pp
  45. /etc/selinux/targeted/modules/active/modules/staff.pp
  46. /etc/selinux/targeted/modules/active/modules/sysadm.pp
  47. /etc/selinux/targeted/modules/active/modules/unprivuser.pp
  48. /etc/selinux/targeted/modules/active/modules/virt.pp
  49. /etc/selinux/targeted/modules/active/modules/xguest.pp
  50. /etc/selinux/targeted/modules/active/modules/xserver.pp
  51. /etc/sysconfig/network-scripts/network-functions-ipv6
  52. /etc/dnsmasq.conf
  53. /etc/brltty/fr-abrege.ctb
  54. /etc/brltty/de-kurzschrift.ctb
  55. /etc/brltty/en-nabcc.ttb
  56. /etc/brltty/en-us-g2.ctb
  57. /etc/brltty.conf
  58. /etc/postfix/access
  59. /etc/postfix/header_checks
  60. /etc/postfix/main.cf
  61. [root@localhost test]# find /etc -size +20k -a -size -50k -exec ls -lh {} \;
  62. -rw-r--r--. 1 root root 25K Nov 21 2015 /etc/selinux/targeted/modules/active/modules/apache.pp
  63. -rw-r--r--. 1 root root 29K Nov 21 2015 /etc/selinux/targeted/modules/active/modules/init.pp
  64. -rw-r--r--. 1 root root 34K Nov 21 2015 /etc/selinux/targeted/modules/active/modules/staff.pp
  65. -rw-r--r--. 1 root root 45K Nov 21 2015 /etc/selinux/targeted/modules/active/modules/sysadm.pp
  66. -rw-r--r--. 1 root root 29K Nov 21 2015 /etc/selinux/targeted/modules/active/modules/unprivuser.pp
  67. -rw-r--r--. 1 root root 27K Nov 21 2015 /etc/selinux/targeted/modules/active/modules/virt.pp
  68. -rw-r--r--. 1 root root 21K Nov 21 2015 /etc/selinux/targeted/modules/active/modules/xguest.pp
  69. -rw-r--r--. 1 root root 29K Nov 21 2015 /etc/selinux/targeted/modules/active/modules/xserver.pp
  70. -rw-r--r--. 1 root root 26K Sep 16 2015 /etc/sysconfig/network-scripts/network-functions-ipv6
  71. -rw-r--r--. 1 root root 25K Aug 6 2015 /etc/dnsmasq.conf
  72. -rw-r--r--. 1 root root 49K Mar 6 2015 /etc/brltty/fr-abrege.ctb
  73. -rw-r--r--. 1 root root 37K Mar 6 2015 /etc/brltty/de-kurzschrift.ctb
  74. -rw-r--r--. 1 root root 21K Mar 6 2015 /etc/brltty/en-nabcc.ttb
  75. -rw-r--r--. 1 root root 39K Mar 6 2015 /etc/brltty/en-us-g2.ctb
  76. -rw-r--r--. 1 root root 22K Mar 6 2015 /etc/brltty.conf
  77. -rw-r--r--. 1 root root 21K Jun 10 2014 /etc/postfix/access
  78. -rw-r--r--. 1 root root 22K Jun 10 2014 /etc/postfix/header_checks
  79. -rw-r--r--. 1 root root 27K Jun 10 2014 /etc/postfix/main.cf
  80. [root@localhost test]# find abc
  81. abc
  82. [root@localhost test]# find abc -exec rm -rf {} \;
  83. [root@localhost test]# find abc
  84. find: ?.bc?. No such file or directory
  • 大小: 211.8 KB
find命令Linux系统中一个非常常用的命令,该命令可以查找文件和目录,并根据不同的条件进行过滤。其基本语法格式如下: ```shell find [path] [option] [expression] ``` 其中,path表示要查找的路径,可以是相对路径或绝对路径;option表示查找时的一些选项,可以用来指定查找深度、时间、权限等条件;expression表示要查找的条件,可以是文件名、文件类型、文件大小等。 下面介绍几个常用的find命令选项和表达式: 1. -name:根据文件名进行查找。 ```shell find /home -name "test.txt" # 在/home下查找文件名为test.txt的文件 ``` 2. -type:根据文件类型进行查找。 ```shell find /home -type f # 在/home下查找普通文件 find /home -type d # 在/home下查找目录 ``` 3. -mtime:根据文件修改时间进行查找。 ```shell find /home -mtime +7 # 查找/home目录下7天前修改的文件 find /home -mtime -7 # 查找/home目录下7天内修改的文件 ``` 4. -size:根据文件大小进行查找。 ```shell find /home -size +1M # 查找/home目录下大小大于1MB的文件 find /home -size -1M # 查找/home目录下大小小于1MB的文件 ``` 5. -user:根据文件所有者进行查找。 ```shell find /home -user alice # 查找/home目录下所有者为alice的文件 ``` 6. -perm:根据文件权限进行查找。 ```shell find /home -perm 644 # 查找/home目录下权限为644的文件 ``` 7. -exec:对查找到的文件进行操作。 ```shell find /home -name "test.txt" -exec rm {} \; # 查找/home目录下名为test.txt的文件并删除 ``` 以上仅是find命令的一些常用选项和表达式,实际使用中还可以根据具体需求进行更多的定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值