当前位置:学者斋 >

计算机 >操作系统 >

初学者如何快速学习Linux下的find命令

初学者如何快速学习Linux下的find命令

单纯的了解Linux find命令是不够的,我们还要知道怎么使用它,下面小编从find的例子中给大家介绍下find的用法,希望对初学者能有所帮助。下面是本站小编精心为大家整理的初学者如何快速学习Linux下的find命令,希望对初学者有帮助,更多内容请关注应届毕业生网!

初学者如何快速学习Linux下的find命令

让我们先从一个简单例子开始。

$ find / -name test

。/backup/ modules/field/modules/test

$

“查找根目录下名称为’test’的文件”, 这条命令会让系统查找所有文件, 包括挂载的文件设备。 这可能需要花费一段时间, 尤其是查找网络共享硬盘。 不过, 我们可以通过参数-mount告诉, 系统忽略挂载设备:

$ find / -mount -name test

find命令格式如下:

find [path] [options] [tests] [actions]

[path]

路径; 应该不难理解。 这里可以使用绝对路径, 也快成使用相对路径。

[options]

参数; 比较常用的参数用:

-depth: 先查找子目录再查看当前目录 -follow: 跟踪查找连接文件 -maxdepths N: 子目录递归最大深度 -mount(or -xdev): 忽略挂载文件

[tests]

条件匹配;

-atime -N/N/+N: 最后一次访问文件的时间在 N天内/N天/N天前 -mtime -N/N/+N: 最后一次修改文件的时间在 N天内/N天/N天前 -name pattern: 与pattern相匹配的文件(包括目录) -newer f1 !f2: 比文件f1新的文件, 比文件f2旧的'文件 -type b/d/c/p/l/f: 文件类型为: 块设备/目录/字符设备/管道/链接/文件 -user username: 文件的所有者是username

我们可以通过以下操作符, 将匹配条件 连起来:

-not (!): 方向匹配 -and (-a): 而且 -or (-o): 或者

我们还可以通过括号将一些匹配符号合并。 例如

(-newer -o -name ‘*test’ )

现在举一个稍微有点复杂的例子, 查找当天被访问过或修改过的文件, 文件名包含’python’, 而起文件所有者是’anthony’:

# find / ( -atime -1 -or -mtime -1 ) -and -name ‘*python*’ -and -user ‘anthony’

/home/anthony/svn_code/subversion-1.7.2/subversion/bindings/swig/python

/home/anthony/svn_code/subversion-1.7.2/subversion/bindings/ctypes-python

/home/anthony/python

/home/anthony/python/Python-3.2.2/build/x-x86_64-3.2/home/anthony/python

/home/anthony/python/Python-3.2.2/Tools/unicode/python-mappings

/home/anthony/l/lib/python3.2

#

[actions]

操作;

-exec command: 执行命令, 具体介绍见后文。 -ok command: 和-exec一样, 除了命令执行需要用户许可。 -print: 打印文件名 -ls: 列出文件详细信息

现在举例说明-exec command

anthony@z:~$ find -mtime -1 -type f -exec ls -l {} ;

-rw-r--r-- 1 anthony anthony 0 Apr 5 12:04 。/search/

-rw------- 1 anthony anthony 22997 Apr 5 12:04 。/nfo

-rw------- 1 anthony anthony 125 Apr 5 14:25 。/hst

anthony@z:~$

简单地说, -exec或-ok, 将查询到的文件作为参数传递给后面的命令执行, 而参数的位置用{}标识, 即命令中, “{}”替换成find查找出来的文件名, 最后”;”表示结束符。

上面就是Linux find命令的介绍了,从例子中学习find命令效果会比看理论知识会好的多,对于初学者来说,多看例子多动手是很有必要的。

  • 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/jsj/caozuo/m2ndx.html