Linux Find 命令详解

查找路径/tmp/gaohaiyang及子目录下所有以t开头的文件,列出文件的详细信息:
find /tmp/gaohaiyang  -type f  -name "t*" -exec ls  -l {} /;

注意:-exec ls  -l {} /;的格式,exec选项后面跟随着所要执行的命令,然后是一对{},一个空格和一个/,最后是一个分号。

可以使用下面两个命令来验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。
先执行这条命令:
find /tmp/gaohaiyang  -type f  -name "t*"
观察列出文件的路径,然后执行:
cd /tmp/gaohaiyang
find . -type f  -name "t*"
会发现只输出从当前路径起的相对路径及文件名。


为了在logs目录中查找更改时间在5日以前的文件并删除它们,可以用:
$ find logs -type f -mtime +5 -exec rm {} /;

在下面的例子中,find命令在当前目录中查找所有文件名以.log结尾、更改时间在150日以上的文件,并删除它们,只不过在删除之前先给出提 示。
find . -name "*.log" -mtime +150 -ok rm {} /;
按y键删除文件,按n键不删除。

下一个命令将查找由用户、组或二者共同执行的文件(错误输出丢掉):  
find /tmp/gaohaiyang -type f  -perm -ug=rx -exec ls -l {} /; 2>/dev/null
或者:
find /tmp/gaohaiyang -type f  -perm -110 -exec ls -l {} /; 2>/dev/null

一:按照owner和权限查找文件和文件夹。

The following command recursively print all file names whose permission  mode  exactly  matches  read, write, and execute
access for user, and read and execute access for  group  and other:
find . -perm u=rwx,g=rx,o=rx
or use this command:find . -perm a=rwx,g-w,o-w
print them with details: find . -perm a=rwx,g-w,o-w -exec ls  -l {} /;

在当前目录及子目录中查找owner是hg06574,权限是644的所有文件(文件的权限必须是644):
find . -user hg06574 -type f -perm 644 -print
在当前目录及子目录中查找owner是hg06574,权限包括644的所有 文件(文件的权限必须包括644,如果文件的权限是755,也可以):
find . -user hg06574 -type f -perm -644 -print

在当前目录及子目录中查找owner是hg06574,权限是755的所有文件夹:
find . -user hg06574 -type d -perm 755
在当前目录及子目录中查找owner是hg06574,权限包括755的所有文件夹(文件夹的权限必须包括755,如果 文件的权限是775,也可以):
find . -user hg06574 -type d -perm 755

#(case 1)-perm mode (比如: -perm 775)
解释:775 前面没有横线是代表只要找到百分之百一样的权限才算
好比说一个档案是 -rwxrwxr-x (775)就是100%的match
但是另 一个档案是 -rwxrwxrwx (777)不是100%的match,others 多了写的权限就不是100%,所以不match!

# (case 2) -perm -mode (例如: -perm -775)
解释:-775 前面有横线表示只要标示的权限match,其他的无所谓,所以上面的第二个档案(777的)在这个case也会match的。

英文资料:http://unixhelp.ed.ac.uk/CGI/man-cgi?find

-perm mode
File's  permission  bits are  exactly mode (octal or symbolic). Since an exact match is required,
if you want to use  this  form for  symbolic  modes,  you  may have to specify a rather complex mode string. 
For example '-perm g=w'  will  only  match  files which  have  mode 0020 (that is, ones for which group write permission is the only permission set). 
It is more likely that you will want to use the '/' or '-' forms, for example '-perm -g=w', which matches any file with group  write permission.

-perm -mode
All  of the permission bits mode are set for the file.  Symbolic modes are accepted in this form,
and this is usually the way  in which  would want to use them.  You must specify 'u', 'g' or 'o' if you use a symbolic mode.  
See the EXAMPLES section for  some illustrative examples.


二:查找文件并改变文件的权限

1:查找当前目录及子目录下owner是hg06574并且权限是644的文件,然后把这些文件的权限改为664
find . -user hg06574 -type f -perm 644 -exec chmod 664 {} /;

查找当前目录及子目录下owner是hg06574并且权限包含644的文件,然后把这些文件的权限改为664
find . -user hg06574 -type f -perm -644 -exec chmod 664 {} /;

查找/tmp/gaohaiyang及子目录下所有以t开头的文件,然后把这些文件的权限改为600
find . -user hg06574 -type f -name "t*" -exec chmod 600 {} /;

2:查找当前目录及子目录下owner是hg06574并且权限是775的文件夹,然后把这些文件夹的权限改为755
find . -user hg06574 -type d -perm 775 -exec chmod 755 {} /;

三:常用的find命令

find . -name “hello” -print 寻找目前目录及所有的子目录内叫hello的文档.
find . -size +2000m -exec ls -l {} /;找出大小超过2000 bytes的文档
find /tmp -user b1234567 -print 在/tmp下属於b1234567的文档
find . -name '*.c' -exec rm {} /;删除所有的.c档
find . -name test/* -print 显示当前目录及其子目录文件名前4位为test的文件
find . -name "test*" -print

四:忽略某个目录
如果在查找文件时希望忽略某个目录,因为你知道那个目录中没有你所要查找的文件,那么可以使用-prune选项来指出需要 忽略的目录。
在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略。

查找当前目录下所有的以.txt结尾的文件
find . -name "*.txt"
列出详细信息:
find . -name "*.txt" -exec ls -l {} /;
可以统计有多少个文件,
find . -name "*.txt" -exec ls -l {} /;|wc -l

查找当前目录下所有的以.txt结尾的文件,但是跳过当前目录下文件夹testfolder1及路径中包含“testfolder1”的子目录,
find . -name testfolder1 -prune -o -name "*.txt"
上面的命令不会查找当前目录下文件夹 testfolder1及路径中包含“testfolder1”的子目录,但是会打印出这些目录的路径。
加上-exec选项,打印出详细信息时, 就不会包括当前目录下文件夹testfolder1及路径中包含“testfolder1”的子目录,
find . -name testfolder1 -prune -o -name "*.txt" -exec ls -l {} /;
可以统计有多少个文件,
find . -name testfolder1 -prune -o -name "*.txt" -exec ls -l {} /;|wc -l

使用-depth后,-prune被忽略:
find . -name testfolder1 -prune -o -name "*.txt" -depth
find . -name testfolder1 -prune -o -name "*.txt" -depth -exec ls -l {} /;|wc -l

五:使用-xargs代替-exec

在使用find命令的-exec选项处理匹配到的文件时,find命令将所有匹配到的文件一起传递给exec执行。不幸的是,有些系统对能够传递给 exec的命令长度有限制,这样在find命令运行
几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是 xargs命令的用处所在,特别是与find命令一起使用。find命令把匹配到的文件传递给xargs命
令,而xargs命令每次只获取一部分 文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。在有些系统中,使用-exec选项会
为 处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效 率不高;而使用
xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数 的数目都会根据该命令的选项及系统内核中相应的可调参
数来确定。
让我们来看看xargs命令是如何同find命令一起使用的,并给出一些 例子。

下面的例子在/tmp/gaohaiyang/testfolder1目录下查找所有other用户具有读、写和执行权限的文件,并收回相应的写权 限
$ find /tmp/gaohaiyang/testfolder1 -perm -7 -print | xargs chmod o-w

<think> 我们根据用户的要求,需要提供Linux系统中find命令的详细解释和使用说明。参考之前的引用内容,我们可以整理出以下信息: 1. find命令的基本功能:在指定目录下查找文件和目录。 2. 基本语法:`find [路径] [选项] [表达式]` 3. 如果没有指定路径,默认在当前目录及其子目录下查找。 4. 常用选项和表达式: - `-name`:按文件名查找 - `-type`:按文件类型查找(f表示文件,d表示目录等) - `-maxdepth`:限制搜索深度 - 逻辑运算符:`-a`(与),`-o`(或),`!`或`-not`(非) - 其他条件:权限、用户、组、时间、大小等 此外,我们还可以补充一些常用的用法和高级用法。 下面我们将详细说明find命令使用。 ### 一、基本语法 ```bash find [搜索路径] [选项] [表达式] ``` ### 二、常用选项 1. **按文件名查找 (-name)** - 查找当前目录及其子目录下所有以.txt结尾的文件: ```bash find . -name "*.txt" ``` - 注意:文件名模式要用引号括起来,防止shell扩展。 2. **按文件类型查找 (-type)** - 查找当前目录下的所有目录: ```bash find . -type d ``` - 常用类型: - `f`:普通文件 - `d`:目录 - `l`:符号链接 3. **限制搜索深度 (-maxdepth, -mindepth)** - 只在当前目录(不进入子目录)查找: ```bash find . -maxdepth 1 -name "*.txt" ``` - 查找深度至少为2的目录(即至少包含一级子目录): ```bash find . -mindepth 2 -type f ``` 4. **按时间查找** - `-atime`:访问时间(天) - `-mtime`:修改时间(天) - `-ctime`:状态改变时间(天) - 查找7天内被访问过的文件: ```bash find . -atime -7 ``` - 注意:`-7`表示7天以内,`+7`表示7天以前。 5. **按大小查找 (-size)** - 查找大于1MB的文件: ```bash find . -size +1M ``` - 单位:`c`(字节),`k`(千字节),`M`(兆字节),`G`(吉字节) 6. **逻辑运算符** - 与(-a,默认可以省略): ```bash find . -name "*.txt" -type f ``` - 或(-o): ```bash find . -name "*.txt" -o -name "*.sh" ``` - 非(! 或 -not): ```bash find . ! -name "*.txt" ``` ### 三、执行操作 find命令可以对查找到的文件执行操作,使用`-exec`或`-ok`(询问确认)。 - 删除当前目录下所有.txt文件: ```bash find . -type f -name "*.txt" -exec rm {} \; ``` 其中,`{}`表示匹配到的文件名,`\;`表示命令结束。 - 也可以使用`-delete`选项直接删除(不需要`-exec`): ```bash find . -type f -name "*.txt" -delete ``` ### 四、其他常用选项 - **按权限查找 (-perm)** ```bash find . -perm 644 # 查找权限为644的文件 find . -perm /u=x # 查找用户有执行权限的文件 ``` - **按用户和组查找** ```bash find . -user root # 查找属主为root的文件 find . -group www # 查找属组为www的文件 ``` ### 五、高级用法 - **忽略大小写 (-iname)** ```bash find . -iname "*.TXT" # 匹配.txt, .TXT等 ``` - **结合xargs** 当处理大量文件时,使用`xargs`可以提高效率: ```bash find . -name "*.log" | xargs rm ``` 但注意,如果文件名包含空格或特殊字符,推荐使用`-print0`和`xargs -0`: ```bash find . -name "*.log" -print0 | xargs -0 rm ``` ### 六、注意事项 1. 在使用`-exec`或`-delete`等有破坏性的操作时,建议先运行不加这些操作的命令确认结果。 2. 当使用通配符时,使用引号避免shell提前展开。 通过上述说明,用户可以全面了解find命令使用方法。下面我们生成相关问题。 </think> ### Linux `find` 命令详细使用说明 `find` 是 Linux/Unix 系统中用于**递归搜索文件和目录**的核心工具,支持丰富的过滤条件和操作。以下是详细说明: --- #### **一、基础语法** ```bash find [搜索路径] [选项] [表达式] ``` - 若省略路径,默认从当前目录递归搜索 - 表达式支持逻辑组合(`-a` 与, `-o` 或, `!` 非) --- #### **二、核心功能** 1. **按名称搜索** ```bash find /home -name "*.txt" # 搜索.txt文件(区分大小写) find . -iname "*.PDF" # 忽略大小写搜索 ``` 2. **按类型过滤** ```bash find /var -type f # 只搜文件 find ~ -type d # 只搜目录 find . -type l # 只搜符号链接 ``` > 类型标识:`f`=文件, `d`=目录, `l`=软链接, `b`=块设备 3. **限制搜索深度** ```bash find . -maxdepth 1 -name "*.log" # 仅当前目录(不递归) find /etc -mindepth 2 -type f # 至少2层子目录 ``` 4. **按时间筛选** ```bash find /tmp -mtime -7 # 7天内修改的文件 find ~ -atime +30 # 30天前访问的文件 ``` > `-mtime`=修改时间, `-atime`=访问时间, `-ctime`=状态变更时间 > `+n`=n天前, `-n`=n天内 5. **按大小过滤** ```bash find /var -size +10M # >10MB的文件 find . -size -100k # <100KB的文件 ``` > 单位:`c`=字节, `k`=KB, `M`=MB, `G`=GB --- #### **三、高级用法** 1. **逻辑组合筛选** ```bash find . \( -name "*.sh" -o -name "*.py" \) ! -user root # 搜sh或py文件且非root用户 ``` 2. **权限过滤** ```bash find /etc -perm 644 # 精确匹配644权限 find . -perm /u=x # 用户有执行权限 ``` 3. **执行操作** ```bash find ~/Downloads -name "*.tmp" -delete # 删除临时文件 find /var/log -size +50M -exec rm {} \; # 删除>50MB日志 find . -type f -print0 | xargs -0 chmod 644 # 处理含空格文件名 ``` > `{}` 代表匹配的文件名,`\;` 结束命令 4. **处理特殊字符** ```bash find . -print0 | xargs -0 command # 安全处理空格/换行符文件名 ``` --- #### **四、实用示例** ```bash # 查找7天内修改的PHP文件并备份 find /var/www -name "*.php" -mtime -7 -exec cp {} {}.bak \; # 搜索空目录 find /data -type d -empty # 查找SUID权限文件 find / -perm /4000 -type f ``` --- #### **注意事项** 1. 对系统目录搜索需 `sudo` 权限 2. 破坏性操作(`-delete`, `-exec rm`)建议先用 `-print` 测试 3. 复杂条件用 `()` 明确优先级,如 `\( cond1 -o cond2 \) -a cond3` > 引用说明:搜索深度限制[^2]、逻辑运算符[^3]、基础功能[^1] ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值