12.Linux每日一个命令@find

1.find含义

  命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
find 是 Linux 中强大的搜索命令,不仅可以按照文件名搜索文件,还可以按照权限、大小、时间、inode 号等来搜索文件。但是 find 命令是直接在硬盘中进行搜索的,如果指定的搜索范围过大,find命令就会消耗较大的系统资源,导致服务器压力过大。
所以,在使用 find 命令搜索时,不要指定过大的搜索范围。

2.find 命令的基本信息如下:

  • 命令名称:find。
  • 英文原意:search for files in a directory hierarchy.
  • 所在路径:/bin/find。
  • 执行权限:所有用户。
  • 功能描述:在目录中查找文件。

3.命令格式

[root@localhost ~]# find 搜索路径 [选项] 搜索内容

find 是比较特殊的命令,它有两个参数:

  • 第一个参数用来指定搜索路径;
  • 第二个参数用来指定搜索内容。
find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;
参数说明 :

find 根据下列规则判断 path 和 expression,在命令列上第一个 - ( ) , ! 之前的部份为 path,之后的是 expression。如果 path 是空字串则使用目前路径,如果 expression 是空字串则使用 -print 为预设 expression。

expression 中可使用的选项有二三十个之多,在此只介绍最常用的部份。

-mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件

-amin n : 在过去 n 分钟内被读取过

-anewer file : 比文件 file 更晚被读取过的文件

-atime n : 在过去 n 天内被读取过的文件

-cmin n : 在过去 n 分钟内被修改过

-cnewer file :比文件 file 更新的文件

-ctime n : 在过去 n 天内创建的文件

-mtime n : 在过去 n 天内修改过的文件

-empty : 空的文件-gid n or -group name : gid 是 n 或是 group 名称是 name

-ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写

-name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写

-size n : 文件大小 是 n 单位,b 代表 512 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。

-type c : 文件类型是 c 的文件。

d: 目录

c: 字型装置文件

b: 区块装置文件

p: 具名贮列

f: 一般文件

l: 符号连结

s: socket

-pid n : process id 是 n 的文件

你可以使用 ( ) 将运算式分隔,并使用下列运算。

exp1 -and exp2

! expr

-not expr

exp1 -or exp2

exp1, exp2

 4.find 举例说明

4.1 find 命令是完全匹配的,必须和搜索关键字一模一样才会列出。

[root@VM-4-13-centos shell]#  ll
-rwxr-xr-x 1 root root 1630 9月  30 09:55 file.sh
[root@VM-4-13-centos shell]# find . -name file
[root@VM-4-13-centos shell]# find . -name file.sh
./file.sh

4.2 Linux 中的文件名是区分大小写的,也就是说,搜索小写文件,是找不到大写文件的。如果想要大小通吃,就要使用 -iname 来搜索文件。

[root@VM-4-13-centos find]# ll
总用量 0
-rw-r--r-- 1 root root 0 10月  9 09:53 huaile.txt
[root@VM-4-13-centos find]# mv huaile.txt Huaile.txt
[root@VM-4-13-centos find]# ll
总用量 0
-rw-r--r-- 1 root root 0 10月  9 09:53 Huaile.txt
[root@VM-4-13-centos find]# find . -name huaile.txt  # . 表示当前目录,如果没写,默认当前路径。
[root@VM-4-13-centos find]# find . -name Huaile.txt
./Huaile.txt
[root@VM-4-13-centos find]# find -iname huaile.txt 
./Huaile.txt
[root@VM-4-13-centos find]# 

 4.3 每个文件都有 inode 号,如果我们知道 inode 号,则也可以按照 inode 号来搜索文件。

1 [root@VM-4-13-centos find]# ls -i
2 1469364 Huaile.txt
3 [root@VM-4-13-centos find]# ls -i Huaile.txt 
4 1469364 Huaile.txt
5 [root@VM-4-13-centos find]# find -inum 1469364
6 ./Huaile.txt
View Code

 4.4 通过查询文件的inode号,可以发现文件是否是硬链接。

[root@VM-4-13-centos find]# ln Huaile.txt huaile
[root@VM-4-13-centos find]# ll
总用量 0
-rw-r--r-- 2 root root 0 10月  9 09:53 huaile
-rw-r--r-- 2 root root 0 10月  9 09:53 Huaile.txt
[root@VM-4-13-centos find]# ll -i
总用量 0
1469364 -rw-r--r-- 2 root root 0 10月  9 09:53 huaile
1469364 -rw-r--r-- 2 root root 0 10月  9 09:53 Huaile.txt
[root@VM-4-13-centos find]# cat Huaile.txt 
123
[root@VM-4-13-centos find]# cat huaile 
123
[root@VM-4-13-centos find]# pwd
/home/Linux-commands/find
[root@VM-4-13-centos find]# find /home/Linux-commands/find/ -inum 1469364
/home/Linux-commands/find/Huaile.txt
/home/Linux-commands/find/huaile

4.5 按文件大小搜索

[root@localhost ~]#find 搜索路径 [选项] 搜索内容

选项:

  • -size[+-]大小:按照指定大小搜索文件

这里的"+"的意思是搜索比指定大小还要大的文件,"-" 的意思是搜索比指定大小还要小的文件。

[root@VM-4-13-centos taobao-logs]# find -size +1000k
./log-debug-2022-10-08.2.log
./log-debug-2022-10-08.3.log
./catalina.2022-09-29.log
./log-debug-2022-10-08.1.log
[root@VM-4-13-centos taobao-logs]# find -size -1000k
.
./urlfile.sh
./cpuid
./beifen.sh
./baklog.sh
./scriptlogs.log
./scriptlogs.txt

千字节必须是小写的"k",而兆字节必然是大写的"M"

4.6 按照修改时间搜索

Linux 中的文件有访问时间(atime)、数据修改时间(mtime)、状态修改时间(ctime)这三个时间,我们也可以按照时间来搜索文件。

[root@localhost ~]# find 搜索路径 [选项] 搜索内容

选项:

  • -atime [+-]时间: 按照文件访问时间搜索
  • -mtime [+-]时间: 按照文改时间搜索
  • -ctime [+-]时间: 按照文件修改时间搜索
atime是指access time,即文件被读取或者执行的时间 (修改文件是不会改变access time的)。

ctime即change time,文件状态改变时间,指文件的i结点被修改的时间,如通过chmod修改文件属性,ctime就会被修改。

mtime即modify time,指文件内容被修改的时间。

这三个时间的区别我们在 stat 命令中已经解释过了,这里用 mtime 数据修改时间来举例,重点说说 "[+-]"时间的含义。

  • -5:代表过去5天内修改的文件。
  • 5:代表前5~6天那一天修改的文件。
  • +5:代表6天前修改的文件。

我们画一个时间轴,来解释一下,如图 1 所示。

              图 1 find时间轴

[root@VM-4-13-centos taobao-logs]# find -atime -5
.
./urlfile.sh
./cpuid
./log-debug-2022-10-08.2.log
./beifen.sh
./log-debug-2022-10-08.3.log
./baklog.sh
./scriptlogs.log
./scriptlogs.txt
./log-debug-2022-10-08.1.log
[root@VM-4-13-centos taobao-logs]# find -atime +5
./catalina.2022-09-29.log

find 不仅可以按照 atmie、mtime、ctime 来査找文件的时间,也可以按照 amin、mmin 和 cmin 来査找文件的时间,区别只是所有 time 选项的默认单位是天,而 min 选项的默认单位是分钟。

4.7 按照文件类型搜索

[root@localhost ~]# find 搜索路径 [选项] 搜索内容

选项:

  • -type d:查找目录
  • -type f:查找普通文件
  • -type l:查找软链接文件

这个命令也很简单,主要按照文件类型进行搜索。在一些特殊情况下,比如需要把普通文件和目录文件区分开,比如需要把普通文件和目录文件区分开,使用这个选项就很方便。

[root@VM-4-13-centos shell]# ll
总用量 32
-rw-r--r-- 1 root root    0 9月  29 09:59 10
-rw-r--r-- 1 root root    0 9月  29 09:59 100
-rw-r--r-- 1 root root    0 9月  29 10:27 1.txt
-rwxr-xr-x 1 root root  302 9月  30 10:03 2.sh
-rw-r--r-- 1 root root    0 9月  29 09:59 50
drwxr-xr-x 2 root root 4096 9月  29 08:54 awk
-rwxr-xr-x 1 root root  468 9月  30 09:56 b.sh
-rwxr-xr-x 1 root root 1630 9月  30 09:55 file.sh
-rwxr-xr-x 1 root root  255 9月  29 10:07 iftest.sh
prw-r--r-- 1 root root    0 9月  29 11:37 pguandao-file
-rwxr-xr-x 1 root root   99 9月  27 11:24 readarray.sh
drwxr-xr-x 2 root root 4096 9月  28 08:36 sed
lrwxrwxrwx 1 root root    8 9月  29 11:07 testll.txt -> test.txt
-rw-r--r-- 1 root root    8 9月  28 10:05 test.txt
lrwxrwxrwx 1 root root    8 9月  29 11:35 vdb -> /dev/vdb
[root@VM-4-13-centos shell]# find -type d
.
./sed
./awk
[root@VM-4-13-centos shell]# find -type f
./file.sh
./iftest.sh
./50
./b.sh
./10
./test.txt
./awk/bb.txt
./awk/ll
./awk/renyuan.txt
./awk/aa.txt
./.b.sh.swp
./1.txt
./100
./readarray.sh
./2.sh
[root@VM-4-13-centos shell]# find -type l
./testll.txt
./vdb

 4.8 结合rm使用

[root@zbx-server find]# ls
1.txt  2.txt
[root@zbx-server find]# pwd
/home/command/find
[root@zbx-server find]# ls
1.txt  2.txt
[root@zbx-server find]# find /home/command/find/ -name 1.txt  -exec rm  {} \;
[root@zbx-server find]# ls
2.txt

{} 占位符,对每个找到的文件,exec 执行删除操作

posted @ 2022-10-09 09:26  家乐福的搬砖日常  阅读(45)  评论(0编辑  收藏  举报