Linux常用命令(十八)find概述

时间:2023-12-20 03:35:34 作者:甜甜无语 综合材料 收藏本文 下载本文

【导语】“甜甜无语”通过精心收集,向本站投稿了7篇Linux常用命令(十八)find概述,下面是小编精心整理后的Linux常用命令(十八)find概述,仅供参考,大家一起来看看吧。

篇1:linux基础命令 find命令概览

Linux下find命令在目录结构中搜索文件,并执行指定的操作,Linux下find 命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它 的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有 网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限 。 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行 ,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文 件系统)。

1.命令格式:

find pathname -options [-print -exec -ok ...]

2.命令功能:

用于在文件树种查找文件,并作出相应 的处理

3.命令参数:

pathname: find命令所查找的目录路径。 例如用.来表示当前目录,用/来表示系统根目录。

-print: find命令将 匹配的文件输出到标准输出。

-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的 形式为'command' {  } \\;,注意{   }和\\;之间的空格。

-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给 出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执 行。

4.命令选项:

-name   按照文件名查找文件。

-perm   按照文件权限来查找文件。

-prune  使用这 一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项, 那么-prune将被find命令忽略。

-user   按照文件属主来查找文件 。

-group  按照文件所属的组来查找文件。

-mtime -n +n  按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内 ,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项, 但它们都和-m time选项。

-nogroup  查找无有效所属组的文件, 即该文件所属的组在/etc/groups中不存在。

-nouser   查找无有 效属主的文件,即该文件的属主在/etc/passwd中不存在。

-newer file1 ! file2  查找更改时间比文件file1新但比文件file2旧的文件。

-type  查找某一类型的文件,诸如:

b - 块设备文件。

d - 目录。

c - 字符设备文件。

p - 管道文件。

l - 符号 链接文件。

f - 普通文件。

-size n:[c] 查找文件长度为n块的 文件,带有c时表示文件长度以字节计。-depth:在查找文件时,首先查找当前 目录中的文件,然后再在其子目录中查找。

-fstype:查找位于某一类型 文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到, 该配置文件中包含了本系统中有关文件系统的信息。

-mount:在查找文 件时不跨越文件系统mount点。

-follow:如果find命令遇到符号链接文 件,就跟踪至链接所指向的文件。

-cpio:对匹配的文件使用cpio命令, 将这些文件备份到磁带设备中。

另外,下面三个的区别:

-amin n   查找系统中最后N分钟访问的文件

-atime n  查找系统中最 后n*24小时访问的文件

-cmin n   查找系统中最后N分钟被改变文 件状态的文件

-ctime n  查找系统中最后n*24小时被改变文件状态 的文件

-mmin n   查找系统中最后N分钟被改变文件数据的文件

-mtime n  查找系统中最后n*24小时被改变文件数据的文件

5.使用实例:

实例1:查找指定时间内修改过的文件

命 令:

find -atime -2

输 出:

[root@peidachang ~]# find -atime - 2

.

./logs/monitor

./.bashrc

./.bash_profile

./.bash_history

说明:

超找48小时内修改过的文件

实例2:根据关键字查找

命令:

find . -name “*.log”

输出:

[root@localhost test]# find . - name “*.log”

./log_link.log

./log2014.log

./tes t4/log3-2.log

./test4/log3-3.log

./test4/log3- 1.log

./log2013.log

./log2012.log

./log.log

./ test5/log5-2.log

./test5/log5- 3.log

./test5/log.log

./test5/log5- 1.log

./test5/test3/log3-2.log

./test5/test3/log3- 3.log

./test5/test3/log3-1.log

./test3/log3- 2.log

./test3/log3-3.log

./test3/log3-1.log

说明:

在当前目录查找 以.log结尾的文件,

“. ”代表当前目录

实例3:按照目录或文件的权限来查找文件

命令:

find /opt/soft/test/ -perm 777

输出:

[root@localhost test]# find /opt/soft/test/ -perm 777

/opt/soft/test/log_link.log

/opt/soft/test/test4

/opt/soft/test/test5/test3

/opt/soft/test/test3

说明:

查找/opt/soft/test/目录下 权限为 777的文件

实例4:按类型 查找

命令:

find . -type f -name “*.log”

输出:

[root@localhost test]# find . -type f -name “*.log”

./log2014.log

./test4/log3- 2.log

./test4/log3-3.log

./test4/log3- 1.log

./log2013.log

./log2012.log

./log.log

./ test5/log5-2.log

./test5/log5- 3.log

./test5/log.log

./test5/log5- 1.log

./test5/test3/log3-2.log

./test5/test3/log3- 3.log

./test5/test3/log3-1.log

./test3/log3- 2.log

./test3/log3-3.log

./test3/log3-1.log

[root@localhost test]#

说明:

查找当目录,以.log结尾的普通 文件

实例5:查找当前所有目录并排序

命令:

find . - type d | sort

输出:

[root@localhost test]# find . -type d | sort

.

./scf

./scf/bin

./scf/doc

./scf/l ib

./scf/service

./scf/service/deploy

./scf/service/ deploy/info

./scf/service/deploy/product

./test3

./t est4

./test5

./test5/test3

[root@localhost test] #

实例6:按大小查找文件

命令:

find . -size +1000c -print

输出:

[root@localhost test]#  find . -size +1000c - print

.

./test4

./scf

./scf/lib

./scf/se rvice

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

./ log2012.log

./test5

./test5/test3

./test3

[root@localhost test]#

说明:

查找当前目录大于1K的文件

篇2:linux下find命令的用法Linux

每一种操作系统都是由成千上万个不同种类的文件所组成的,其中有系统本身自带的文件,用户自己的文件,还有共享文件等等。我们有时候经常忘记某份文件放在硬盘中的哪个地方。在微软的 WINDOWS 操作系统中要查找一份文件是相当简单的事情,只要在桌面上点击“

每一种操作系统都是由成千上万个不同种类的文件所组成的。其中有系统本身自带的文件,用户自己的文件,还有共享文件等等。我们有时候经常忘记某份文件放在硬盘中的哪个地方。在微软的WINDOWS操作系统中要查找一份文件是相当简单的事情,只要在桌面上点击“开始”-“搜索”中就能按照各种方式在本地硬盘上,局域网络,甚至在INTERNET上查找各种文件,文档。

可是使用Linux的用户就没有那么幸运了,在Linux上查找某个文件确实是一件比较麻烦的事情。毕竟在Linux中需要我们使用专用的“查找”命令来寻找在硬盘上的文件。Linux下的文件表达格式非常复杂,不象WINDOWS,DOS下都是统一的AAAAAAA.BBB格式那么方便查找,在WINDOWS中,只要知道要查找的文件的文件名或者后缀就非常容易查找到。Linux中查找文件的命令通常为“find”命令,“find”命令能帮助我们在使用,管理Linux的日常事务中方便的查找出我们需要的文件。对于Linux新手来说,“find”命令也是了解和学习Linux文件特点的方法。因为Linux发行版本繁多,版本升级很快,在Linux书籍上往往写明某个配置文件的所在位置,往往Linux新手按图索骥还是不能找到。比如说REDHAT Linux 7.O和REDHAT Linux 7.1中有些重要的配置文件所在的硬盘位置和文件目录就有了很大的改变,如果不学会使用“find”命令,那么在成千上万的Linux文件中要找到其中的一个配置文件是相当困难的,笔者在没有精通“find”命令之前就吃过这样的苦头。好,下面就详细为大家介绍强大的“find”命令的全部使用方法和用途。

每一种操作系统都是由成千上万个不同种类的文件所组成的。其中有系统本身自带的文件,用户自己的文件,还有共享文件等等。我们有时候经常忘记某份文件放在硬盘中的哪个地方。在微软的WINDOWS操作系统中要查找一份文件是相当简单的事情,只要在桌面上点击“开始”-“搜索”中就能按照各种方式在本地硬盘上,局域网络,甚至在INTERNET上查找各种文件,文档。

可是使用Linux的用户就没有那么幸运了,在Linux上查找某个文件确实是一件比较麻烦的事情。毕竟在Linux中需要我们使用专用的“查找”命令来寻找在硬盘上的文件。Linux下的文件表达格式非常复杂,不象WINDOWS,DOS下都是统一的AAAAAAA.BBB格式那么方便查找,在WINDOWS中,只要知道要查找的文件的文件名或者后缀就非常容易查找到。Linux中查找文件的命令通常为“find”命令,“find”命令能帮助我们在使用,管理Linux的日常事务中方便的查找出我们需要的文件。对于Linux新手来说,“find”命令也是了解和学习Linux文件特点的方法。因为Linux发行版本繁多,版本升级很快,在Linux书籍上往往写明某个配置文件的所在位置,往往Linux新手按图索骥还是不能找到。比如说REDHAT Linux 7.O和REDHAT Linux 7.1中有些重要的配置文件所在的硬盘位置和文件目录就有了很大的改变,如果不学会使用“find”命令,那么在成千上万的Linux文件中要找到其中的一个配置文件是相当困难的,笔者在没有精通“find”命令之前就吃过这样的苦头。好,下面就详细为大家介绍强大的“find”命令的全部使用方法和用途。

通过文件名查找法:

这个方法说起来就和在WINDOWS下查找文件一样容易理解了。如果你把这个文件放在单个的文件夹里面,只要使用常见的“ls“命令就能方便的查找出来,那么使用“find”命令来查找它就不能给你留下深刻的印象,毕竟“find”命令的强大功能不止这个。如果知道了某个文件的文件名,而不知道这个文件放到哪个文件夹,甚至是层层套嵌的文件夹里。举例说明,假设你忘记了httpd.conf这个文件在系统的哪个目录下,甚至在系统的某个地方也不知道,则这是可以使用如下命令:

find / -name httpd.conf

这个命令语法看起来很容易就明白了,就是直接在find后面写上 -name,表明要求系统按照文件名查找,最后写上httpd.conf这个目标文件名即可。稍等一会系统会在计算机屏幕上显示出查找结果列表:

etc/httpd/conf/httpd.conf

这就是httpd.conf这个文件在Linux系统中的完整路径。查找成功。

如果输入以上查找命令后系统并没有显示出结果,那么不要以为系统没有执行find/ -name httpd.conf命令,而可能是你的系统中没有安装Apache服务器,这时只要你安装了Apache Web服务器,然后再使用find / -name httpd.conf就能找到这个配置文件了。

无错误查找技巧:

在Linux系统中“find”命令是大多数系统用户都可以使用的命令,并不是ROOT系统管理员的专利。但是普通用户使用“find”命令时也有可能遇到这样的问题,那就是Linux系统中系统管理员ROOT可以把某些文件目录设置成禁止访问模式。这样普通用户就没有权限用“find”命令来查询这些目录或者文件。当普通用户使用“find”命令来查询这些文件目录是,往往会出现”Permissiondenied.“(禁止访问)字样。系统将无法查询到你想要的文件。为了避免这样的错误,我们可是使用转移错误提示的方法尝试着查找文件,输入

find / -name aclearcase/” target=“_blank” >ccess_log 2>/dev/null

这个方法是把查找错误提示转移到特定的目录中去。系统执行这个命令后,遇到错误的信息就直接输送到stderrstream 2 中,access_log 2就是表明系统将把错误信息输送到stderrstream 2中,/dev/null是一个特殊的文件,表明空的或者错误的信息,这样查询到的错误信息将被转移了,不会再显示了。

在Linux系统查找文件也会遇到这样一个实际问题。如果我们在整个硬盘,这个系统中查找某个文件就要花费相当长的一段时间,特别是大型Linux系统和容量较大的硬盘,文件放在套嵌很深的目录中的时候。如果我们知道了这个文件存放在某个大的目录中,那么只要在这个目录中往下找就能节省很多时间了。使用find /etc -name httpd.conf 就可以解决这个问题。上面的命令就是表示在etc目录中查询httpd.conf这个文件。这里再说明一下“/ ”这个函数符号的含义,如果输入 “find/ ”就是表示要求Linux系统在整个ROOT目录下查找文件,也就是在整个硬盘上查找文件,而“find/etc”就是只在 etc目录下查找文件。因为“find/etc”表示只在etc目录下查找文件,所以查找的速度就相应要快很多了。

根据部分文件名查找方法:

这个方法和在WINDOWS中查找已知的文件名方法是一样的。不过在Linux中根据部分文件名查找文件的方法要比在WINDOWS中的同类查找方法要强大得多。例如我们知道某个文件包含有srm这3个字母,那么要找到系统中所有包含有这3个字母的文件是可以实现的,输入:

find /etc -name '*srm*'

这个命令表明了Linux系统将在/etc整个目录中查找所有的包含有srm这3个字母的文件,比如 absrmyz, tibc.srm等等符合条件的文件都能显示出来。如果你还知道这个文件是由srm 这3个字母打头的,那么我们还可以省略最前面的星号,命令如下:

find/etc -name 'srm*'

这是只有像srmyz 这样的文件才被查找出来,象absrmyz或者 absrm这样的文件都不符合要求,不被显示,这样查找文件的效率和可靠性就大大增强了。

根据文件的特征查询方法:

如果只知道某个文件的大小,修改日期等特征也可以使用“find”命令查找出来,这和WINDOWS系统中的“搜索”功能是基本相同的。在微软的“搜索”中WINDOWS中的“搜索助理”使得搜索文件和文件夹、打印机、用户以及网络中的其他计算机更加容易。它甚至使在Inte.net上搜索更加容易。“搜索助理”还包括一个索引服务,该服务维护了计算机中所有文件的索引,使得搜索速度更快。使用“搜索助理”时,用户可以指定多个搜索标准。例如,用户可以按名称、类型及大小搜索文件和文件夹。用户甚至可以搜索包含特定文本的文件。如果用户正使用 Active Directory,这时还可以搜索带有特定名称或位置的打印机。

例如我们知道一个Linux文件大小为1,500 bytes,那么我们可是使用如下命令来查询find / -size 1500c,字符 c 表明这个要查找的文件的大小是以bytes为单位。如果我们连这个文件的具体大小都不知道,那么在Linux中还可以进行模糊查找方式来解决,

例如我们输入find/ -size +10000000c 这个命令,则标明我们指定系统在根目录中查找出大于10000000字节的文件并显示出来。命令中的“+”是表示要求系统只列出大于指定大小的文件,而使用“-”则表示要求系统列出小于指定大小的文件。下面的列表就是在Linux使用不同“ find“命令后系统所要作出的查找动作,从中我们很容易看出在Linux中使用“find”命令的方式是很多的,“ find“命令查找文件只要灵活应用,丝毫不必在WINDOWS中查找能力差。

find / -amin -10 # 查找在系统中最后10分钟访问的文件

find / -atime -2 # 查找在系统中最后48小时访问的文件

find / -empty # 查找在系统中为空的文件或者文件夹

find / -group cat # 查找在系统中属于 groupcat的文件

find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件

find / -mtime -1 #查找在系统中最后24小时里修改过的文件

find / -nouser #查找在系统中属于作废用户的文件

find / -user fred #查找在系统中属于FRED这个用户的文件

下面的列表就是对find命令所可以指定文件的特征进行查找的部分条件。在这里并没有列举所有的查找条件,参考有关Linux有关书籍可以知道所有find命令的查找函数。

-amin n

查找系统中最后N分钟访问的文件

-atime n

查找系统中最后n*24小时访问的文件

-cmin n

查找系统中最后N分钟被改变状态的文件

-ctime n

查找系统中最后n*24小时被改变状态的文件

-empty

查找系统中空白的文件,或空白的文件目录,或目录中没有子目录的文件夹

-false

查找系统中总是错误的文件

-fstype type

查找系统中存在于指定文件系统的文件,例如:ext2 .

-gid n

查找系统中文件数字组 ID 为 n的文件

-group gname

查找系统中文件属于gnam文件组,并且指定组和ID的文件

Find命令的控制选项说明:

Find命令也提供给用户一些特有的选项来控制查找操作。下表就是我们总结出的最基本,最常用的find命令的控制选项及其用法。

选项

用途描述

-daystart

.测试系统从今天开始24小时以内的文件,用法类似-amin

-depth

使用深度级别的查找过程方式,在某层指定目录中优先查找文件内容

-follow

遵循通配符链接方式查找; 另外,也可忽略通配符链接方式查询

-help

显示命令摘要

-maxdepth levels

在某个层次的目录中按照递减方法查找

-mount

不在文件系统目录中查找, 用法类似 -xdev.

-noleaf

禁止在非UNUX文件系统,MS-DOS系统,CD-ROM文件系统中进行最优化查找

-version

打印版本数字

使用-follow选项后,find命令则遵循通配符链接方式进行查找,除非你指定这个选项,否则一般情况下find命令将忽略通配符链接方式进行文件查找。

-maxdepth选项的作用就是限制find命令在目录中按照递减方式查找文件的时候搜索文件超过某个级别或者搜索过多的目录,这样导致查找速度变慢,查找花费的时间过多。例如,我们要在当前(.)目录技巧子目录中查找一个名叫fred的文件,我们可以使用如下命令

find . -maxdepth 2 -name fred

假如这个fred文件在./sub1/fred目录中,那么这个命令就会直接定位这个文件,查找很容易成功。假如,这个文件在./sub1/sub2/fred目录中,那么这个命令就无法查找到。因为前面已经给find命令在目录中最大的查询目录级别为2,只能查找2层目录下的文件。这样做的目的就是为了让find命令更加精确的定位文件,如果你已经知道了某个文件大概所在的文件目录级数,那么加入-maxdepth n 就很快的能在指定目录中查找成功。

使用混合查找方式查找文件

find命令可以使用混合查找的方法,例如我们想在/tmp目录中查找大于100000000字节并且在48小时内修改的某个文件,我们可以使用-and 来把两个查找选项链接起来组合成一个混合的查找方式。

find /tmp -size +10000000c -and -mtime +2

学习过计算机语言的朋友都知道,在计算机语言里,使用and ,or 分别表示“与”和“或”的关系。在Linux系统的查找命令中一样通用。

还有这样的例子,

find / -user fred -or -user george

我们可以解释为在/tmp目录中查找属于fred或者george这两个用户的文件。

在find命令中还可以使用“非”的关系来查找文件,如果我们要在/tmp目录中查找所有不属于panda的文件,使用一个简单的

find /tmp ! -user panda

命令就可以解决了。很简单。

查找并显示文件的方法

查找到某个文件是我们的目的,我们更想知道查找到的文件的详细信息和属性,如果我们采取现查找文件,在使用LS命令来查看文件信息是相当繁琐的,现在我们也可以把这两个命令结合起来使用。

find / -name ”httpd.conf“ -ls

系统查找到httpd.conf文件后立即在屏幕上显示httpd.conf文件信息。

12063 34 -rw-r--r-- 1 root root 33545 Dec 30 15:36 /etc/httpd/conf/httpd.conf

下面的表格就是一些常用的查找文件并显示文件信息的参数和使用方法

选项

用途描述

-exec command;

查找并执行命令

-fprint file

打印文件完整文件名

-fprint0 file

打印文件完整文件名包括空的文件

-fprintf file format

打印文件格式

-ok command;

给用户命令执行操作,根据用户的Y 确认输入执行

-printf format

打印文件格式

-ls

打印同种文件格式的文件.

总结:到这里为止我们已经学习了这名多关于find命令的使用方法,也列出了很多常用的find命令的选项,如果我们能熟练掌握在Linux中find命令的使用方法,那么在Linux中查找文件也不是一件困难的事情。

原文转自:www.ltesting.net

篇3:linux基础命令 find命令之exec

find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的 作用就显现出来了,

exec解释:

-exec  参数后面跟的是command命令,它的终止是以;为结束标志的,所以 这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。

{}   花括号代 表前面find查找出来的文件名。

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec 选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件 之前,最好先用ls命令看一下,确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ }, 一个空格和一个\\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只 输出从当前路径起的相对路径及文件名。

实例1:ls -l命令放在find命令的-exec选项中

命令:

find . -type f -exec ls -l {} \\;

输出:

[root@localhost test]# find . -type f -exec ls -l {} \\;

-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3 -2.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log

-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log

-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log

-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log

-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log

[root@localhost test]#

说明:

上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项 中使用ls -l命令将它们列出。

实例2:在目录中查找更改时间在n日以前的文件并删除它们

命令:

find . -type f -mtime +14 -exec rm {} \\;

输出:

[root@localhost test]# ll

总计 328

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     33 10-28 16:54 log2013.log

-rw-r--r-- 1 root root    127 10-28 16:51 log2014.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log ->log.log

-rw-r--r-- 1 root root     25 10-28 17:02 log.log

-rw-r--r-- 1 root root     37 10-28 17:07 log.txt

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 10-28 14:47 test3

drwxrwxrwx 2 root root   4096 10-28 14:47 test4

[root@localhost test]# find . -type f -mtime +14 -exec rm {} \\;

[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log ->log.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

说明:

在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec 选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。

实例3:在目录中查找更改时间在n日以前的文件并 删除它们,在删除之前先给出提示

命令:

find . -name ”*.log“ -mtime +5 -ok rm {} \\;

输出:

[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log ->log.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -name ”*.log“ -mtime +5 -ok rm {} \\;

< rm ... ./log_link.log >? y

< rm ... ./log2012.log >? n

[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

说明:

在上面的例子中, find命令在当 前目录中查找所有文件名以.log结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示,

按y键删除文 件,按n键不删除。

实例4:-exec中使用grep命令

命令:

find /etc -name ”passwd*“ -exec grep ”root“ {} \\;

输出:

[root@localhost test]# find /etc -name ”passwd*“ -exec grep ”root“ {} \\;

root:x:0:0:root:/root:/bin/bash

root:x:0:0:root:/root:/bin/bash

[root@localhost test]#

说明 :

任何形式的命令都可以在-exec选项中使用。  在上面的例子中我们使用grep命令。find命令首先匹配所有文件 名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户 。

实例5:查找文件移动到指定目录

命令:

find . -name ”*.log“ -exec mv {} .. \\;

输出:

[root@localhost test]# ll

总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-12 22:49 test3

drwxrwxr-x 2 root root 4096 11-12 19:32 test4

[root@localhost test]# cd test3/

[root@localhost test3]# ll

总计 304

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

[root@localhost test3]# find . -name ”*.log“ -exec mv {} .. \\;

[root@localhost test3]# ll

总计 0[root@localhost test3]# cd ..

[root@localhost test]# ll

总 计 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr- xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:50 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

实例6:用 exec选项执行cp命令

命令:

find . -name ”*.log“ -exec cp {} test3 \\;

输出:

[root@localhost test3]# ll

总计 0[root@localhost test3]# cd ..

[root@localhost test]# ll

总计 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:50 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -name ”*.log“ -exec cp {} test3 \\;

cp: “./test3/log2014.log” 及 “test3/log2014.log” 为同一文件

cp: “./test3/log2013.log” 及 “test3/log2013.log” 为同一文件

cp: “./test3/log2012.log” 及 “test3/log2012.log” 为同一文件

[root@localhost test]# cd test3

[root@localhost test3]# ll

总计 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw- r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test3]#

查看全套教程:www.bianceng.cn/OS/Linux/201301/35075.htm

篇4:linux基础命令 find命令之xargs

在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行,但有些系统对能够 传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数 列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。

find命令把匹配到的文件传递给xargs命令,而xargs 命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如 此继续下去。

在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件 全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高; 而使用xargs命令则只有 一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据 该命令的选项及系统内核中相应的可调参数来确定。

使用实例:

实例1: 查找系统中的每一个普通文件,然后使 用xargs命令来测试它们分别属于哪类文件

命令:

find . -type f -print | xargs file

输出:

[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw- r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -type f -print | xargs file

./log2014.log: empty

./log2013.log: empty

./log2012.log: ASCII text

[root@localhost test]#

实例2:在整个系统中查找内存信息转储文件 (core dump) ,然后把结果保存到/tmp/core.log 文件中

命令:

find / -name ”core“ -print | xargs echo ”“ >/tmp/core.log

输出:

[root@localhost test]# find / -name ”core“ -print | xargs echo ”“ >/tmp/core.log

[root@localhost test]# cd /tmp

[root@localhost tmp]# ll

总计 16

-rw-r--r-- 1 root root 1524 11-12 22:29 core.log

drwx------ 2 root root 4096 11-12 22:24 ssh-TzcZDx1766

drwx------ 2 root root 4096 11-12 22:28 ssh- ykiRPk1815

drwx------ 2 root root 4096 11-03 07:11 vmware-root

实例3:在当前目录下查找所有用户具有读 、写和执行权限的文件,并收回相应的写权限

命令:

find . -perm -7 -print | xargs chmod o-w

输出 :

[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -perm -7 -print | xargs chmod o-w

[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 19:32 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

说明:

执行命令后,文件夹scf、test3和test4的权限都发生改变

实例4:用grep命令在所有的普通文件中搜索 hostname这个词

命令:

find . -type f -print | xargs grep ”hostname“

输出:

[root@localhost test]# find . -type f -print | xargs grep ”hostname“

./log2013.log:hostnamebaidu=baidu.com

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[root@localhost test]#

实例5:用grep命令在当前目录下的所有普通文件中搜索 hostnames这个词

命令:

find . -name \\* -type f -print | xargs grep ”hostnames“

输出:

[root@peida test]# find . -name \\* -type f -print | xargs grep ”hostnames“

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[root@localhost test]#

说明:

注意,在上面的例子中, \\用来取消find命令中的*在shell中的特殊含义。

实例6:使用xargs执行mv

命令:

find . -name ”*.log“ | xargs -i mv {} test4

输出:

[root@localhost test]# ll

总计 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:54 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]# cd test4/

[root@localhost test4]# ll

总计 0[root@localhost test4]# cd ..

[root@localhost test]# find . -name ”*.log“ | xargs -i mv {} test4

[root@localhost test]# ll

总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-13 05:50 test3

drwxrwxr-x 2 root root 4096 11-13 05:50 test4

[root@localhost test]# cd test4/

[root@localhost test4]# ll

总计 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test4]#

实例7:find后执行xargs提示 xargs: argument line too long解决方法:

命令:

find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

输出:

[root@pd test4]#  find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

rm -f

[root@pdtest4]#

说明:

-l1是一次处理一个;-t是处理之前打印出命令

实例8:使用-i参数默 认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[]

命令:

输出:

[root@localhost test]# ll

总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-13 05:50 test3

drwxrwxr-x 2 root root 4096 11-13 05:50 test4

[root@localhost test]# cd test4

[root@localhost test4]# find . -name ”file“ | xargs -I [] cp [] ..

[root@localhost test4]# ll

总计 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test4]# cd ..

[root@localhost test]# ll

总计 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 05:50 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]#

说明:

使用-i参数默认的前面输出 用{}代替,-I参数可以指定其他代替字符,如例子中的[]

实例9:xargs的-p参数的使用

命令:

find . - name ”*.log“ | xargs -p -i mv {} ..

输出:

[root@localhost test3]# ll

总计 0

-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

[root@localhost test3]# cd ..

[root@localhost test]# ll

总计 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:06 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# cd test3

[root@localhost test3]#  find . -name ”*.log“ | xargs -p -i mv {} ..

mv ./log2015.log .. ?...y

[root@localhost test3]# ll

总计 0[root@localhost test3]# cd ..

[root@localhost test]# ll

总计 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw- r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

drwxr- xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]#

说明:

-p参数会提示让你确认是否执行后面的命令,y执行,n不执行,

查看全套教程:www.bianceng.cn/OS/Linux/201301/35075.htm

篇5:linux基础命令 find 命令的参数详解

find一些常用参数的一些常用实例和一些具体用法和注意事项,

1.使用name选项:

文件名选项是find命令最常用 的选项,要么单独使用该选项,要么和其他选项一起使用。 可以使用某种文件名模式来匹配文件,记住要用引号将文件名模式 引起来。  不管当前路径是什么,如果想要在自己的根目录$HOME中查找文件名符合*.log的文件,使用~作为 'pathname'参数,波浪号~代表了你的$HOME目录。

find ~ -name ”*.log“ -print

想要在当 前目录及子目录中查找所有的‘ *.log‘文件,可以用:

find . -name ”*.log“ -print

想要的当前 目录及子目录中查找文件名以一个大写字母开头的文件,可以用:

find . -name ”[A-Z]*“ -print

想 要在/etc目录中查找文件名以host开头的文件,可以用:

find /etc -name ”host*“ -print

想要查找 $HOME目录中的文件,可以用:

find ~ -name ”*“ -print 或find . -print

要想让系统高负荷运行, 就从根目录开始查找所有的文件。

find / -name ”*“ -print

如果想在当前目录查找文件名以一个个 小写字母开头,最后是4到9加上.log结束的文件:

命令:

find . -name ”[a-z]*[4-9].log“ - print

输出:

[root@localhost test]# ll

总计 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# find . -name ”[a-z]*[4-9].log“ - print

./log2014.log

./log2015.log

./test4/log2014.log

[root@localhost test]#

2.用 perm选项:

按照文件权限模式用-perm选项,按文件权限模式来查找文件的话。最好使用八进制的权限表示法。

如 在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件,可以用:

[root@localhost test]# find . -perm 755 - print

.

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/dep loy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

[root@localhost test]#

还有一种表达方法:在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-005相当于555,

命令:

find . -perm -005

输出:

[root@localhost test]# ll

总计 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# find . -perm - 005

.

./test4

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf /service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

./test3

[root@localhost test]#

3.忽略某个目录:

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

命令:

find test -path ”test/test3“ -prune -o -print

输出:

[root@localhost soft]# find test -path ”test/test3“ -prune -o - print

test

test/log2014.log

test/log2015.log

test/test4

test/test4/log2014.log

test/test4/log2013.log

test/test4/log2012.log

test/scf

test/scf/lib

test/scf/service< /p>

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

tes t/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

[root@localhost soft]#

4 .使用find查找文件的时候怎么避开某个文件目录:

实例1:在test 目录下查找不在test4子目录之内的所有文件

命令:

find test -path ”test/test4“ -prune -o -print

输出:

[root@localhost soft]# find test

test

test/log2014.log

test/log2015.log

test/test4

test/test4/log2014.log

< p>test/test4/log2013.log

test/test4/log2012.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test /scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

test/test3

[root@localhost soft]# find test -path ”test/test4“ -prune -o - print

test

test/log2014.log

test/log2015.log

test/scf

test/scf/lib

test/scf/ service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

test/test3

[root@localhost soft]#

说明:

find [-path ..] [expression]

在路径列表的后面的是表达式

- path ”test“ -prune -o -print 是 -path ”test“ -a -prune -o -print 的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似如果

-path ”test“ 为真,则求值 -prune , - prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑表达式为假,

如果 -path ”test“ -a -prune 为假 ,则求值 -print ,-print返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。

这个表达式组合特例 可以用伪码写为:

if -path ”test“ then

-prune

else

-print

实例2:避开多个 文件夹:

命令:

find test \\( -path test/test4 -o -path test/test3 \\) -prune -o -print

输出:

[root@localhost soft]# find test \\( -path test/test4 -o -path test/test3 \\) -prune -o -print

test

test/log2014.log

test/log2015.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test /scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

[root@localhost soft]#

说 明:

圆括号表示表达式的结合。 \\表示引用,即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。

实例3:查找某一确定文件,-name等选项加在-o 之后

命令:

find test \\(-path test/test4 -o -path test/test3 \\) -prune -o -name ”*.log“ -print

输出:

[root@localhost soft]# find test \\( -path test/test4 -o -path test/test3 \\) -prune -o -name ”*.log“ -print

test/log2014.log

test/log2015.log

test/log2013.log

test/log2012.log

[root@localhost soft]#

5.使用user和nouser选项:

按文件属主查找文件:

实例1:在$HOME目录中查找文件属主为peida 的文件

命令:

find ~ -user peida -print

实例2:在/etc目录下查找文件属主为peida的文件:

命 令:

find /etc -user peida -print

说明:

实例3:为了查找属主帐户已经被删除的文件,可以使用- nouser选项。在/home目录下查找所有的这类文件

命令:

find /home -nouser -print

说明:

这样 就能够找到那些属主在/etc/passwd文件中没有有效帐户的文件。在使用-nouser选项时,不必给出用户名; find命令能够为你 完成相应的工作。

6.使用group和nogroup选项:

就像user和nouser选项一样,针对文件所属于的用户组, find 命令也具有同样的选项,为了在/apps目录下查找属于gem用户组的文件,可以用:

find /apps -group gem - print

要查找没有有效所属用户组的所有文件,可以使用nogroup选项。下面的find命令从文件系统的根目录处查找这样 的文件:

find / -nogroup-print

7.按照更改时间或访问时间等查找文件:

如果希望按照更改时间来查找 文件,可以使用mtime,atime或ctime选项。如果系统突然没有可用空间了,很有可能某一个文件的长度在此期间增长迅速,这时 就可以用mtime选项来查找这样的文件。

用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距 今n日以前的文件。

希望在系统根目录下查找更改时间在5日以内的文件,可以用:

find / -mtime -5 - print

为了在/var/adm目录下查找更改时间在3日以前的文件,可以用:

find /var/adm -mtime +3 - print

8.查找比某个文件新或旧的文件:

如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可 以使用-newer选项。

它的一般形式为:

newest_file_name ! oldest_file_name

其中,!是逻辑非符号。

实例1:查找更改时间比文件log2012.log新但比文件log2017.log旧的文件

命令:

find -newer log2012.log ! -newer log2017.log

输出:

[root@localhost test]# ll

总计 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

-rw-r--r-- 1 root root      0 11-16 14:41 log2016.log

-rw-r--r-- 1 root root      0 11-16 14:43 log2017.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr -x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# find -newer log2012.log ! -newer log2017.log

.

./log2015.log

./log2017.log

./log2016.log

./test3

[root@localhost test]#

实例2:查找更改时间在比log2012.log文件新的文件

命令:

find . -newer log2012.log -print

输出:

[root@localhost test]# find -newer log2012.log

.

./log2015.log

./log2017.log

./log2016.log

./test3

[root@localhost test]#

9.使用type选项:

实例1:在/etc目录下查找所有的目录

命令:

find /etc -type d -print

实例2:在当前目录下查找除目录以外的所有类型的文件

命令:

find . ! -type d -print

实例3:在/etc目录下查找所有的符号链接文件

命令:

find /etc -type l -print

10.使 用size选项:

可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。 以字节计量文件长度的表达形式为N c;以块计量文件长度只用数字表示即可。

在按照文件长度查找文件时,一般使用这 种以字节表示的文件长度,在查看文件系统的大小,因为这时使用块来计量更容易转换。

实例1:在当前目录下查找文件 长度大于1 M字节的文件

命令:

find . -size +1000000c -print

实例2:在/home/apache目录下查找文件 长度恰好为100字节的文件:

命令:

find /home/apache -size 100c -print

实例3:在当前目录下查找长 度超过10块的文件(一块等于512字节)

命令:

find . -size +10 -print

11.使用depth选项:

在使用find命令时,可能希望先匹配所有的文件,再在子目录中查找。使用depth选项就可以使find命令这样做。这样做的一个 原因就是,当在使用find命令向磁带上备份文件系统时,希望首先备份所有的文件,其次再备份子目录中的文件。

实例1 :find命令从文件系统的根目录开始,查找一个名为CON.FILE的文件。

命令:

find / -name ”CON.FILE“ -depth -print

说明:

它将首先匹配所有的文件然后再进入子目录中查找

12.使 用mount选项:

在当前的文件系统中查找文件(不进入其他文件系统),可以使用find命令的mount选项。

实例1 :从当前目录开始查找位于本文件系统中文件名以XC结尾的文件

命令:

find . -name ”*.XC“ -mount -print

查看全套教程:www.bianceng.cn/OS/Linux/201301/35075.htm

篇6:Linux中find命令mtime参数与用法linux操作系统

本文章来给学习linux的朋友详细介绍关于在linux中find命令mtime参数,同时也介绍find命令的使用方法,各位同学可进入参考,

大家在使用find命令中的mtime参数时候,会看到官方的解释如下:

-mtime n

File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the

interpretation of file modification times.

但是在各种参考的使用方式中有用+号,-号,不带符号的用法,那么这里又有什么区别呢?

注意这里的n,如果n为带有+号的值,意思为删除n天前所有的文件,比如n=+1且今天是15号,那么删除14号以前的数据,不包括14号,如果是负号(n=-1)则为删除一天内的文件,比如今天15号,那么删除15号的数据,如果是(n=-2)则代表删除一天前到今天的所有数据,比如今天15号,那么从14号开始删除。如果不带有符号,那么则删除指定前n天中这一天的数据,比如(n=1)且今天是15号,则删除14号这一天所有数据。

注意这里的一天是指当前系统时间算起的,而不是0-24小时算一天

·find  path  -option  [  -print ]  [ -exec  -ok  command ]  {} ;

find命令的参数;

pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。

-print: find命令将匹配的文件输出到标准输出。

-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } ;,注意{ }和;之间的空格。

-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

#-print 将查找到的文件输出到标准输出

#-exec  command  {} ;     —–将查到的文件执行command操作,{} 和 ;之间有空格

#-ok 和-exec相同,只不过在操作前要询用户

例:find . -name .svn | xargs rm -rf

====================================================

-name  filename            #查找名为filename的文件

-perm                       #按执行权限来查找

-user   username            #按文件属主来查找

-group groupname           #按组来查找

-mtime  -n +n               #按文件更改时间来查找文件,-n指n天以内,+n指n天以前

-atime   -n +n              #按文件访问时间来查GIN: 0px”>

-ctime   -n +n             #按文件创建时间来查找文件,-n指n天以内,+n指n天以前

-nogroup                    #查无有效属组的文件,即文件的属组在/etc/groups中不存在

-nouser                    #查无有效属主的文件,即文件的属主在/etc/passwd中不存

-newer  f1 !f2             找文件,-n指n天以内,+n指n天以前

-ctime   -n +n              #按文件创建时间来查找文件,-n指n天以内,+n指n天以前

-nogroup                    #查无有效属组的文件,即文件的属组在/etc/groups中不存在

-nouser                     #查无有效属主的文件,即文件的属主在/etc/passwd中不存

-newer  f1 !f2              #查更改时间比f1新但比f2旧的文件

-type   b/d/c/p/l/f        #查是块设备、目录、字符设备、管道、符号链接、普通文件

-size     n[c]              #查长度为n块[或n字节]的文件

-depth                      #使查找在进入子目录前先行查找完本目录

-fstype                    #查更改时间比f1新但比f2旧的文件

-type   b/d/c/p/l/f        #查是块设备、目录、字符设备、管道、符号链接、普通文件

-size     n[c]              #查长度为n块[或n字节]的文件

-depth                      #使查找在进入子目录前先行查找完本目录

-fstype                     #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到

-mount                      #查文件时不跨越文件系统mount点

-follow                     #如果遇到符号链接文件,就跟踪链接所指的文件

-cpio               %;     #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到

-mount                      #查文件时不跨越文件系统mount点

-follow                     #如果遇到符号链接文件,就跟踪链接所指的文件

-cpio                       #对匹配的文件使用cpio命令,将他们备份到磁带设备中

-prune                      #忽略某个目录

=====================================================

$find  ~  -name  “*.txt”  -print   #在$HOME中查.txt文件并显示

$find  .   -name  “*.txt”  -print

$find  .   -name  “[A-Z]*”  -print  #查以大写字母开头的文件

$find  /etc  -name  “host*”  -print #查以host开头的文件

$find  .  -name  “[a-z][a-z][0–9][0–9].txt”   -print  #查以两个小写字母和两个数字开头的txt文件

$find .  -perm  755  -print

$find  .  -perm -007  -exec ls -l {} ;  #查所有用户都可读写执行的文件同-perm 777

$find  . -type d  -print

$find  .  !  -type  d  -print

$find  .  -type l  -print

$find  .  -size  +1000000c  -print       #查长度大于1Mb的文件

$find  .  -size  100c        -print      # 查长度为100c的文件

$find  .  -size  +10  -print             #查长度超过期作废10块的文件(1块=512字节)

$cd /

$find  etc  home  apps   -depth  -print  | cpio  -ivcdC65536  -o  /dev/rmt0

$find  /etc -name “passwd*”  -exec grep  “cnscn”  {}  ;  #看是否存在cnscn用户

$find . -name “yao*”  | xargs file

$find  . -name “yao*”  |  xargs  echo   “” >/tmp/core.log

$find  . -name “yao*”  | xargs  chmod  o-w

======================================================

find  -name april*                    在当前目录下查找以april开始的文件

find  -name  april*  fprint file       在当前目录下查找以april开始的文件,并把结果输出到file中

find  -name ap* -o -name may*  查找以ap或may开头的文件

find  /mnt  -name tom.txt  -ftype vfat  在/mnt下查找名称为tom.txt且文件系统类型为vfat的文件

find  /mnt  -name t.txt ! -ftype vfat  在/mnt下查找名称为tom.txt且文件系统类型不为vfat的文件

find  /tmp  -name wa* -type l           在/tmp下查找名为wa开头且类型为符号链接的文件

find  /home  -mtime  -2                在/home下查最近两天内改动过的文件

find /home   -atime -1                 查1天之内被存取过的文件

find /home -mmin   +60                 在/home下查60分钟前改动过的文件

find /home  -amin  +30                 查最近30分钟前被存取过的文件

find /home  -newer  tmp.txt            在/home下查更新时间比tmp.txt近的文件或目录

find /home  -anewer  tmp.txt           在/home下查存取时间比tmp.txt近的文件或目录

find  /home  -used  -2                 列出文件或目录被改动过之后,在2日内被存取过的文件或目录

find  /home  -user cnscn               列出/home目录内属于用户cnscn的文件或目录

find  /home  -uid  +501                 列出/home目录内用户的识别码大于501的文件或目录

find  /home  -group  cnscn             列出/home内组为cnscn的文件或目录

find  /home  -gid 501                  列出/home内组id为501的文件或目录

find  /home  -nouser                   列出/home内不属于本地用户的文件或目录

find  /home  -nogroup                  列出/home内不属于本地组的文件或目录

find  /home   -name tmp.txt   -maxdepth  4  列出/home内的tmp.txt 查时深度最多为3层

find  /home  -name tmp.txt  -mindepth  3  从第2层开始查

find  /home  -empty                    查找大小为0的文件或空目录

find  /home  -size  +512k               查大于512k的文件

find  /home  -size  -512k              查小于512k的文件

find  /home  -links  +2               查硬连接数大于2的文件或目录

find  /home  -perm  0700               查权限为700的文件或目录

find  /tmp  -name tmp.txt  -exec cat {} ;

find  /tmp  -name  tmp.txt  -ok  rm {} ;

find   /  -amin   -10    # 查找在系统中最后10分钟访问的文件

find   /  -atime  -2       # 查找在系统中最后48小时访问的文件

find   /  -empty            # 查找在系统中为空的文件或者文件夹

find   /  -group  cat       # 查找在系统中属于 groupcat的文件

find   /  -mmin  -5        # 查找在系统中最后5分钟里修改过的文件

find   /  -mtime  -1      #查找在系统中最后24小时里修改过的文件

find   /  -nouser          #查找在系统中属于作废用户的文件

find   /  -user   fred    #查找在系统中属于FRED这个用户的文件

篇7:Linux命令详解之find

作用

搜索文件

格式

find pathoption [-print] [-exec -ok command] {} \\

默认搜索路径为当前路径

默认为-print,输出至标准输出

对于查找到的文件执行command命令

option为搜索条件

主要参数

-name filename

限制文件名

-user username

按文件属主来搜索

-group groupname

按组来查找

-mtime -n +n

按文件更改时间来查找,-n指n天以内,+n指n天以前

-atime -n +n

按文件访问时间来查

-ctime -n +n

按文件创建时间来查找

-nogroup

-nouser

-type

按文件类型搜索

-size n

-depth

查找子目录前先搜索完本目录

-follow

如果遇到符号链接文件,就跟踪链接所指文件

-prune

忽略某个文件

-o

逻辑或

-a

逻辑与

逻辑否

\\( \\)

转义,可括住搜索条件

示例

查找当前目录下的所有普通文件

find .-type f -exec ls -l {} \\;

在home目录下查找更改时间在5日以前的文件并删除它们

find /home-mtime +5 -exec -ok rm {} \\;

查询今天修改过的文件

find /-mtime -1 -exec ls -l {} \\;

查询今天修改过的文件并提示是否显示

find /-mtime -1 -ok ls -l {} \\;

查找小于100k的文件并显示

find /home-size -100k -exec ls -l {} \\;

查找后缀为.txt的所有文件

find ~-name “*.txt” -ok ls -l {} \\;

国务院命令范文

命令格式优秀

任职命令范文

提干命令范文

公文命令格式

授衔命令范文

项目概述范文

组织概述范文

网络安全概述

年度工作概述

Linux常用命令(十八)find概述(精选7篇)

欢迎下载DOC格式的Linux常用命令(十八)find概述,但愿能给您带来参考作用!
推荐度: 推荐 推荐 推荐 推荐 推荐
点击下载文档 文档为doc格式
点击下载本文文档