linux列出一个目录及其子目录下面的某种类型的文件

时间:2009-7-9     作者:smarteng     分类: Linux命令


怎么样把,一个目录及其所有的子目录下面的某种类型(比如*.gif)的文件全部列出来?
这些子目录下面可能还包含有目录,要想全部列出*.gif的文件

find . -name "*.gif"

linux find命令-exec参数的使用说明(笔记)[break]

关键词: Linux   find -exec

前言:最近几天使用find的高级功能,但执行到 -exec命令的时候总是提示错误

信息如下:“find: missing argument to `-ok' ”,花了点时间,研究了下帮助(man),终于是搞清楚了。

说明:find命令,配合-exec参数,可以对查询的文件进行进一步的操作,可以得到很多有用的功能,比如说文件包含特定字符串的查询等,要了解这个功能,最简单直接的就是看find命令帮助,列出

 -exec command ;
               Execute command; true if 0 status is returned.   All   following   arguments   to find are taken to be arguments to the command until an   argument   consisting of #;' is encountered.   The string {}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find.   Both of these constructions might need to be escaped (with a \') or quoted to   protect   them   from   expansion   by the shell.   The command is executed in the starting directory.

其实只要读懂这段话就理解了

废话少说,这里简单说明一下

-exec 参数后面跟的是 command命令,注意点如下:

command命令的终止,使用 ';' (分号)来判定,在后面必须有一个 ';'

'{}',使用{}来表示文件名,也就是find前面处理过程中过滤出来的文件,用于command命令进行处理

特别强调,对于不同的系统,直接使用分号可能会有不同的意义, 使用转义符 '\'在分号前明确说明,对于前面我们遇到的问题,主要就是这个原因引起的!

举例:

1.查询所有保护字符串“Hello”的文件

find / -exec grep "Hello" {} \;

2.删除所有临时文件

find / -name "*.tmp" -exec rm -f {} \;

。。。。。。。。

如何批量更改文件权限

我处理 ftp 上传资源时,最常用的命令是这样,不过要小心后果
代码:

find . -type f -exec chmod 644 "{}" \; find . -type d -exec chmod 755 "{}" \;

别用这个命令处理系统中原有的文件就可以了
find 会遍历每个文件,其中每一个都临时以 "{}" 来标识