find :在指定的路径下查找指定文件…

本文详细介绍了Linux下的find命令的使用方法,包括各种查找选项如按文件名、用户、组、文件类型等查找,以及如何结合-exec和-ok选项执行命令。通过多个实例展示了find命令的强大功能。

1、功能说明
find命令用来在指定的路径下查找指定的文件。其格式如下:
find path-name [-options] [-print -exec -ok 命令 { } \ ;]
path-name:find命令查找的目录路径,例如可以用“.”表示当前目录,用“\”表示系统根目录。

-options:find命令的这个选项主要用来控制搜索的方式。下面列出“-options”选项常见的几个格式:
-name ‘字串’:查找文件名匹配所给字串的所有文件,字串内可用通配符*、?、[]
-lname ‘字串’:查找文件名匹配所给字串的所有符号链接文件,字串内可用通配符*、?、[]
-gid n:查找属于ID号为n的用户组的所有文件
-uid n:查找属于ID号为n的用户的所有文件
-empty:查找大小为0的文件或目录
-path ‘字串’:查找路劲发名匹配所给字串的所有文件,字串内可用通配符*、?、[]
-group ‘字串’:查找属于用户组名为所给字串的所有文件
-depth:在查找文件时,首先查找当前目录下的文件,然后在查找其子目录下的文件
-prune 目录:指出搜索时不搜索该目录。如果同时使用-depth选项,那么-prune选项将被find命令忽略
-size n:查找文件长度为n块的文件,带有c时表示文件长度以字节计
-user ‘字串’:查找属于用户名为所给字串的所有的文件
-mtime  +n或-n:按时间搜索,+n表示n天之前的,-n表示今天到n天前之间的
-type 文件类型:按指定文件类型搜索。文件类型包括b(块设备文件)、c(字符设备文件)、f(普通文件)、l(符号链接)、d(目录)、p(管道)、s(socket文件)

-print:搜索结果输出到标准输出。
-exec:对搜索出符合条件的文件执行所给出的Linux命令,而不询问用户是否需要执行该命令。{ }表示shell命令的选项即为所查找到的文件。命令的末尾必须以“;”结束。【注意:“-exec 命令 {}  \;”,在 } 和 \ 之间有空格隔开】
-ok:对搜索出符合条件的文件执行所给出的Linux命令。与-exec不同的是,它会询问用户是否需要执行该命令。

2、举例
(1)在系统根目录下,查找文件为普通文件,属于student用户的,2天以前的,并且查找时不包含/usr/bin目录的文件名为main.c的文件,并将结果输出到屏幕:
find / -path "/usr/bin" -prune -o -name "main.c" -user student -type f -mtime +2 -print
(2)对上例中搜索的结果进行删除操作:
find / -path "/usr/bin" -prune -o -name "main.c" -user student -type f -mtime +2 -print -exec rm {} \;
(3)查找系统中所有大小为0的普通文件,并列出它们的完整路径:
find / -type f -size 0 -exec ls -al {} \;
(4)查找系统/var/logs目录中修改时间在7天以前的普通文件,然后以交互方式删除:
find /var/logs -type f -mtime +7 -ok rm {} \;
(5)在当前目录及子目录下查找所有“*.txt”的文件:
find . -name "*.txt" -print
(6)在用户自己的根目录下查找文件名以一个大写字母开头,紧接着是一个小写字母和两个数字,最后以“*.txt”结尾的文件:
find ~ -name "[A-Z] [a-z] [0-9] [0-9]*.txt"
(7)在/etc目录下查找文件属主为student用户的文件:
find /etc -user student -print

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style> .AlignLeft { text-align: left; } .AlignCenter { text-align: center; } .AlignRight { text-align: right; } body { font-family: sans-serif; font-size: 11pt; } td { vertical-align: top; padding-left: 4px; padding-right: 4px; } tr.SectionGap td { font-size: 4px; border-left: none; border-top: none; border-bottom: 1px solid Black; border-right: 1px solid Black; } tr.SectionAll td { border-left: none; border-top: none; border-bottom: 1px solid Black; border-right: 1px solid Black; } tr.SectionBegin td { border-left: none; border-top: none; border-right: 1px solid Black; } tr.SectionEnd td { border-left: none; border-top: none; border-bottom: 1px solid Black; border-right: 1px solid Black; } tr.SectionMiddle td { border-left: none; border-top: none; border-right: 1px solid Black; } tr.SubsectionAll td { border-left: none; border-top: none; border-bottom: 1px solid Gray; border-right: 1px solid Black; } tr.SubsectionEnd td { border-left: none; border-top: none; border-bottom: 1px solid Gray; border-right: 1px solid Black; } table.fc { border-top: 1px solid Black; border-left: 1px solid Black; width: 100%; font-family: monospace; font-size: 10pt; } td.TextItemInsigAdd { color: #000000; background-color: #EEEEFF; } td.TextItemInsigDel { color: #000000; background-color: #EEEEFF; text-decoration: line-through; } td.TextItemInsigMod { color: #000000; background-color: #EEEEFF; } td.TextItemInsigOrphan { color: #000000; background-color: #FAEEFF; } td.TextItemNum { color: #696969; background-color: #F0F0F0; } td.TextItemSame { color: #000000; background-color: #FFFFFF; } td.TextItemSigAdd { color: #000000; background-color: #FFE3E3; } td.TextItemSigDel { color: #000000; background-color: #FFE3E3; text-decoration: line-through; } td.TextItemSigMod { color: #000000; background-color: #FFE3E3; } td.TextItemSigOrphan { color: #000000; background-color: #F1E3FF; } .TextSegInsigDiff { color: #0000FF; } .TextSegReplacedDiff { color: #0000FF; font-style: italic; } .TextSegSigDiff { color: #FF0000; } .TextSegElement_20851_38190_23383 { font-weight: bold; } .TextSegElement_35782_21035_31526 { } .TextSegElement_25968_23383 { color: #2E9269; } .TextSegElement_23383_31526_20018 { color: #3A7726; } .TextSegElement_32534_35793_22120_25351_20196 { color: #681717; } .TextSegElement_27880_37322 { color: #786A41; } .TextSegElement_25805_20316_31526 { } </style> <title>GA_D82DD83D_00-00-05 VS GA_D82DD83D_00-00-04_Warning</title> </head> <body> GA_D82DD83D_00-00-05 VS GA_D82DD83D_00-00-04_Warning<br/> 已产生: 2025/10/20 11:20:35<br/>     <br/> 模式:  全部   <br/> 左边文件: E:\1_临时代码仓\GA_D37D_03-00-01\mainline\spa_traveo\src\IpcApplication\diagClient\canTp\canTp.c   <br/> 右边文件: E:\1_临时代码仓\GA_D37D_02-00-04\mainline\spa_traveo\src\IpcApplication\diagClient\canTp\canTp.c   <br/> <table class="fc" cellspacing="0" cellpadding="0"> <tr class="SectionBegin"> <td class="TextItemNum AlignRight">1</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> <td class="AlignCenter">=</td> <td class="TextItemNum AlignRight">1</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">2</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  Copyright DENSO Corporation                                                                                                      */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">2</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  Copyright DENSO Corporation                                                                                                      */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">3</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">3</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">4</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  Version  Date        Author   Change Description                                                                                 */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">4</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  Version  Date        Author   Change Description                                                                                 */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">5</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/* --------- ----------  -------  -------------------------------------------------------------------------------------------------- */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">5</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/* --------- ----------  -------  -------------------------------------------------------------------------------------------------- */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">6</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  1.0.0    3/6/2019    LW       New.                                                                                               */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">6</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  1.0.0    3/6/2019    LW       New.                                                                                               */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">7</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  2.0.0    9/26/2021   DC       add a new function which is sending  multi frame request                                           */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">7</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  2.0.0    9/26/2021   DC       add a new function which is sending  multi frame request                                           */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">8</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  2.0.1    10/8/2021   DC       Add PBD.can id and can index for D03B                                                              */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">8</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  2.0.1    10/8/2021   DC       Add PBD.can id and can index for D03B                                                              */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">9</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">9</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">10</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  * LW   = Luo Wei, KOTEI  create                                                                                                  */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">10</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  * LW   = Luo Wei, KOTEI  create                                                                                                  */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">11</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  * DC   = Ding Cong, KOTEI                                                                                                        */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">11</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  * DC   = Ding Cong, KOTEI                                                                                                        */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">12</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">12</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">13</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  CAN TP 15765-2 implement                                                                                                         */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">13</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*  CAN TP 15765-2 implement                                                                                                         */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">14</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">14</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*                                                                                                                                   */</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">15</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">15</td> <td class="TextItemSame"><span class="TextSegElement_27880_37322">/*===================================================================================================================================*/</span></td> </tr> <tr class="SectionMiddle"> <td class="TextItemNum AlignRight">16</td> <td class="TextItemSame"> </td> <td class="AlignCenter"> </td> <td class="TextItemNum AlignRight">16</td> <td class="TextItemSame"> </td> </tr>取得.c
10-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值