find命令专辑

find命令使用技巧

查找文件,移动到某个目录

使用find和xargs

15条 linux Find 命令实际使用方法

find 命令用法

find命令使用经验

find用法小结

find与xargs的用法------实际例子(ZT)

 

find命令使用技巧

1、查找文件
Linux中查找文件的命令为“find”命令,syntax:
find [起始目录] 寻找条件 操作
如,从根目录下开始查找abc.cpp文件
 find / -name abc.cpp 
在当前目录下所有.cpp文件
 find . -name "*.cpp"
find命令,配合-exec参数,可以对查询的文件进行进一步的操作
-exec 参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加使用转义符反斜杠 '\'。
 xargs展开find获得的结果,使其作为grep的参数
find ./ -name "*.tmp" -exec rm -rf "{}" /;
删除所有的临时文件
但是rm mv等命令对大量文件操作是报错 -bash: /bin/rm: Argument list too long
 可用xargs 解决
 删除当前目录下所有.cpp文件
 find . -name "*.tmp" | xargs rm
find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。
find与grep配合使用,用来查找指定目录下的文件内容。grep用于查找文件中的字符串,或能匹配正则表达式的行
grep命令
grep (global search regular expression_r(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
grep正则表达式元字符集(基本集)
^ 锚定行的开始 如:'^grep'匹配所有以grep开头的行。
$ 锚定行的结束 如:'grep$'匹配所有以grep结尾的行。
. 匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。
* 匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。 .*一起用代表任意字符。
[] 匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。
[^] 匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。
\(..\) 标记匹配字符,如:'\(love\)',love被标记为1。
\< 锚定单词的开始,如:'\
\> 锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。
x\{m\} 连续重复字符x,m次,如:'o\{5\}'匹配包含连续5个o的行。
x\{m,\} 连续重复字符x,至少m次,如:'o\{5,\}'匹配至少连续有5个o的行。
x\{m,n\} 连续重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配连续5--10个o的行。
\w 匹配一个文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。
\W w的反置形式,匹配一个非单词字符,如点号句号等。\W*则可匹配多个。
\b 单词锁定符,如: '\bgrep\b'只匹配grep,即只能是grep这个单词,两边均为空格。
find、grep、xargs经常配合使用,如下:
find -name “.cpp” |xargs grep 'test' 查找当前目录下所有的cpp文件中包含test的行。
 

查找文件,移动到某个目录

把当前目录下面的file(不包括目录),移动到/opt/shell


find  .  -type f  -exec mv {}   /opt/shell   \;

find .  -type f  |  xargs  -I  '{}'  mv  {}  /opt/shell



下面from    http://weiruoyu.blog.51cto.com/951650/832497 

find多少天以前的文件,按时间移动,并分批打包


显示前十个文件

[root@localhost smgpbi]# ls -1 | sort -u | head -10

1.首先查看文件个数,进入所在的文件

# find . -name "*" | wc -l

或者

# ll |grep "^-" |wc -l

2.查看文件个数

查看120天钱的文件个数

# find . -mtime +120 | wc -l

一般如果是小文件,控制在10-20万左右。

解释:-mtime +30 --设置时间为30天前;

-exec mv --查找完毕后执行移动操作;

3.按照时间移动到指定目录里

# find . -mtime +90 -exec mv {} /var/tmp/date_90 \;

4.计算大小

# du -sh date_90

大小一般控制在10-15G最好

5.压缩并打包

#tar -zcvf date_90.tar.gz date_90/

 

 

使用find和xargs

 

第2章 使用find和xargs有时可能需要在系统中查找具有某一特征的文件(例如文件权限、文件属主、文件长度、文件类型等等)。这样做可能有很多原因。可能 出于安全性的考虑,或是一般性的系统管理任务,或许只是为了找出一个不知保存在什么地方的文件。Find是一个非常有效的工具,它可以遍历当前目录甚至于 整个文件系统来查找某些文件或目录。
  在本章中,我们介绍以下内容:
  ? find命令选项。
  ? 用find命令不同选项的例子。
  ? 配合find使用xargs命令的例子。
  由于find具有如此强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统(NFS),find命令在该文件系统中同样有效,只要你具有相应的权限。
  在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。
  Find命令的一般形式为:
  find pathname  -options [-print -exec -ok]
  让我们来看看该命令的参数:
  pathname find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
  -print find命令将匹配的文件输出到标准输出。
  -exec find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'comm-
  and'{}\;,注意{}和\;之间的空格。
  -ok和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
  

  2.1 find
命令选项

  

  find命令有很多选项或表达式,每一个选项前面跟随一个横杠-。让我们先来看一下该命令的主要选项,然后再给出一些例子。
  -name按照文件名查找文件。
  -perm按照文件权限来查找文件。
  -prune使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用了-depth选项,那么-prune选项将被find命令忽略。
  -user按照文件属主来查找文件。
  -group按照文件所属的组来查找文件。
  -mtime-n+n按照文件的更改时间来查找文件,-n表示文件更改时间距现在n天以内,+n表示文件更改时间距现在n天以前。Find命令还有-atime和-ctime选项,但它们都和-mtime选项相似,所以我们在这里只介绍-mtime选项。
  -nogroup查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
  -nouser查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
  -newerfile1!file2查找更改时间比文件file1新但比文件file2旧的文件。
  -type查找某一类型的文件,诸如:
  b-块设备文件。
  d-目录。
  c-字符设备文件。
  p-管道文件。
  l-符号链接文件。
  f-普通文件。
  -sizen[c]查找文件长度为n块的文件,带有c时表示文件长度以字节计。
  -depth在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
  -fstype查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
  -mount在查找文件时不跨越文件系统mount点。
  -follow如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
  -cpio对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
  

  2.1.1
使用name选项

  

  文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用。可以使用某种文件名模式来匹配文件,记住要用引号将文件名模式引起来。
  不管当前路径是什么,如果想要在自己的根目录$HOME中查找文件名符合*.txt的文件,使用~作为'pathname参数,波浪号~代表了你的$HOME目录。
  $ find ~ -name  "*.txt" -print
  想要在当前目录及子目录中查找所有的‘*.txt’文件,可以用:
  $ find . -name  "*.txt" -print
  想要的当前目录及子目录中查找文件名以一个大写字母开头的文件,可以用:
  $ find . -name  "[A-Z]*" -print
  想要在/etc目录中查找文件名以host开头的文件,可以用:
  $ find /etc -name  "host*" -print
  想要查找$HOME目录中的文件,可以用:
  $ find~-name"*"-printfind.-print
  要想让系统高负荷运行,就从根目录开始查找所有的文件。如果希望在系统管理员那里保留一个好印象的话,最好在这么做之前考虑清楚!
  $ find / -name  "*" -print
  如果想在当前目录查找文件名以两个小写字母开头,跟着是两个数字,最后是*.txt的文件,下面的命令就能够返回名为ax37.txt的文件:
  $ find . -name  "[a-z][a-z][0--9][0--9].txt" -print
  

  2.1.2
使用perm选项

  

  如果希望按照文件权限模式来查找文件的话,可以采用-perm选项。你可能需要找到所有用户都具有执行权限的文件,或是希望查看某个用户目录下的文件权限类型。在使用这一选项的时候,最好使用八进制的权限表示法。
  为了在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件,可以用:
  $ find . -perm  755 -print
如果希望在当前目录下查找所有用户都可读、写、执行的文件(要小心这种情况),我们可以使用find命令的-perm选项。在八进制数字前面要加一个横杠 -。在下面的命令中-perm代表按照文件权限查找,而‘007’和你在chmod命令的绝对模式中所采用的表示法完全相同。
  $ find . -perm  -007 -print
  

  2.1.3
忽略某个目录

  

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

  2.1.4
使用user和nouser选项

  

  如果希望按照文件属主查找文件,可以给出相应的用户名。例如,在$HOME目录中查找文件属主为dave的文件,可以用:
  $ find ~ -user  dave -print
  在/etc目录下查找文件属主为uucp的文件:
  $ find /etc -user  uucp -print
为了查找属主帐户已经被删除的文件,可以使用-nouser选项。这样就能够找到那些属主在/etc/passwd文件中没有有效帐户的文件。在使用 -nouser选项时,不必给出用户名;find命令能够为你完成相应的工作。例如,希望在/home目录下查找所有的这类文件,可以用:
  $ find /home -nouser  -print
  

  2.1.5
使用group和nogroup选项

  

  就像user和nouser选项一样,针对文件所属于的用户组,find命令也具有同样的选项,为了在/apps目录下查找属于accts用户组的文件,可以用:
  $ find /apps -group  accts -print
  要查找没有有效所属用户组的所有文件,可以使用nogroup选项。下面的find命令从文件系统的根目录处查找这样的文件
  $ find / -nogroup  -print
  

  2.1.6
按照更改时间查找文件

  

如果希望按照更改时间来查找文件,可以使用mtime选项。如果系统突然没有可用空间了,很有可能某一个文件的长度在此期间增长迅速,这时就可以用 mtime选项来查找这样的文件。用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。
  希望在系统根目录下查找更改时间在5日以内的文件,可以用:
  $ find/-mtime-5-print
  为了在/var/adm目录下查找更改时间在3日以前的文件,可以用:
  $ find /var/adm  -mtime +3 -print
  

  2.1.7
查找比某个文件新或旧的文件

  

  如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项。它的一般形式为:
  newest_file_name  ! oldest_file_name
  其中,!是逻辑非符号。
  这里有两个文件,它们的更改时间大约相差两天。
  -rwxr-x-r-x     1 root     root        92    Apr  18 11:18         age.awk
  -rwxrwxr-x     1 root     root   1045    Apr 20 19:37         belts.awk
  下面给出的find命令能够查找更改时间比文件age.awk新但比文件belts.awk旧的文件:
  $ find . -newer age.awk ! -newer belts.awk -exec ls -l  {} \;
  -rwxrwxr-x     1 root     root        62    Apr  18 11:32         ./who.awk
  -rwxr-xr-x      1 root     root        49    Apr  18 12:05         ./group.awk
  -rw-r--r--              1 root     root    201    Apr  20 19:30         ./grade2.txt
  -rwxrwxr-x     1 root     root   1054    Apr 20 19:37         ./belts.awk
如果想使用find命令的这一选项来查找更改时间在两个小时以内的文件,除非有一个现成的文件其更改时间恰好在两个小时以前,否则就没有可用来比较更改时 间的文件。为了解决这一问题,可以首先创建一个文件并将其日期和时间戳设置为所需要的时间。这可以用touch命令来实现。
  假设现在的时间是23:40,希望查找更改时间在两个小时以内的文件,可以首先创建这样一个文件:
  $ touch -t 05042140 dstamp
  -rw-r--r--              1 dave    admin     0     May 4  21:40        dstamp
  一个符合要求的文件已经被创建;这里我们假设今天是五月四日,而该文件的更改时间是21:40,比现在刚好早两个小时。
  现在我们就可以使用find命令的-newer选项在当前目录下查找所有更改时间在两个小时以内的文件:
  $find . -newer  dstamp -print
  

  2.1.8
使用type选项

  

  UNIX或LINUX系统中有若干种不同的文件类型,这部分内容我们在前面的章节已经做了介绍,这里就不再赘述。如果要在/etc目录下查找所有的目录,可以用:
  $find /etc -type  d -print
  为了在当前目录下查找除目录以外的所有类型的文件,可以用:
  $find . ! -type  d -print
  为了在/etc目录下查找所有的符号链接文件,可以用:
  $find /etc -type  l -print
  

  2.1.9
使用size选项

  

  可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计量文件长度的表达形式为Nc;以块计量文件长度只用数字表示即可。
  就我个人而言,我总是使用以字节计的方式,在按照文件长度查找文件时,大多数人都喜欢使用这种以字节表示的文件长度,而不用块的数目来表示,除非是在查看文件系统的大
  小,因为这时使用块来计量更容易转换。
  为了在当前目录下查找文件长度大于1M字节的文件,可以用:
  $find . -size  +1000000c -print
  为了在/home/apache目录下查找文件长度恰好为100字节的文件,可以用:
  $find /home /apache  -size 100c -print
  为了在当前目录下查找长度超过10块的文件(一块等于512字节),可以用:
  $find . -size  +10 -print
  

  2.1.10
使用depth选项

  

  在使用find命令时,可能希望先匹配所有的文件,再在子目录中查找。使用depth选项就可以使find命令这样做。这样做的一个原因就是,当在使用find命令向磁带上备份文件系统时,希望首先备份所有的文件,其次再备份子目录中的文件。
  在下面的例子中,find命令从文件系统的根目录开始,查找一个名为CON.FILE的文件。它将首先匹配所有的文件然后再进入子目录中查找。
  $find / -name  "CON.FILE" -depth -print
  2.1.11使用mount选项
  在当前的文件系统中查找文件(不进入其他文件系统),可以使用find命令的mount选项。在下面的例子中,我们从当前目录开始查找位于本文件系统中文件名以XC结尾的文件:
  $find . -name  "*.XC" -mount -print
  2.1.12使用cpio选项
  cpio命令可以用来向磁带设备备份文件或从中恢复文件。可以使用find命令在整个文件系统中(更多的情况下是在部分文件系统中)查找文件,然后用cpio命令将其备份到磁带上。
  如果希望使用cpio命令备份/etc、/home和/apps目录中的文件,可以使用下面所给出的命令,不过要记住你是在文件系统的根目录下:
  $ cd /
  $ find etc home apps -depth -print | cpio -ivcdC65536  -o /dev/rmt0
  (在上面的例子中,第一行末尾的\告诉shell命令还未结束,忽略\后面的回车。)
在上面的例子中,应当注意到路径中缺少/。这叫作相对路径。之所以使用相对路径,是因为在从磁带中恢复这些文件的时候,可以选择恢复文件的路径。例如,可 以将这些文件先恢复到另外一个目录中,对它们进行某些操作后,再恢复到原始目录中。如果在备份时使用了绝对路径,例如/etc,那么在恢复时,就只能恢复 到/etc目录中去,别无其他选择。在上面的例子中,我告诉find命令首先进入/etc目录,然后是/home和/apps目录,先匹配这些目录下的文 件,然后再匹配其子目录中的文件,所有这些结果将通过管道传递给cpio命令进行备份。
  顺便说一下,在上面的例子中cpio命令使用了C65536选项,我本可以使用B选项,不过这样每块的大小只有512字节,而使用了C65536选项后,块的大小变成了64K字节(65536/1024)。
  

  2.1.13
使用exec或ok来执行shell命令

  

当匹配到一些文件以后,可能希望对其进行某些操作,这时就可以使用-exec选项。一旦find命令匹配到了相应的文件,就可以用-exec选项中的命令 对其进行操作(在有些操作系统中只允许-exec选项执行诸如ls或ls-l这样的命令)。大多数用户使用这一选项是为了查找旧文件并删除它们。这里我强 烈地建议你在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。
  exec选项后面跟随着所要执行的命令,然后是一对儿{},一个空格和一个\,最后是一个分号。
  为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。
  为了用ls-l命令列出所匹配到的文件,可以把ls-l命令放在find命令的-exec选项中,例如:
  $ find . -type f -exec ls-l {} \;
  -rwxr-xr-x      10 root   wheel            1222       Jan  4     1993       ./sbin/C80
  -rwxr-xr-x      10 root   wheel            1222       Jan  4     1993       ./sbin/Normal
  -rwxr-xr-x      10 root   wheel            1222       Jan  4     1993       ./sbin/Revvid
  上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls-l命令将它们列出。
  为了在/logs目录中查找更改时间在5日以前的文件并删除它们,可以用:
  $ find logs -type  f -mtime +5 -exec rm {} \;
  记住,在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!
  当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。在下面的例子中,find命令在当前目录中查找所有文件名以.LOG结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。
  $ find . -name “*.LOG” -mtime +5 -ok rm {} \;
  <rm  … ./nets.LOG> ?y
  按y键删除文件,按n键不删除。
任何形式的命令都可以在-exec选项中使用。在下面的例子中我们使用grep命令。find命令首先匹配所有文件名为“passwd*”的文件,例如 passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个rounder用户。
  $ find /etc -name “passwd*” -exec grep “rounder” {} \;
  rounder:JL9TtUqk8EHwc:500:500::/home/apps/nets/rounder:/bin/sh
  

  2.1.14
find命令的例子

  

  我们已经介绍了find命令的基本选项,下面给出find命令的一些其他的例子。
  为了匹配$HOME目录下的所有文件,下面两种方法都可以使用:
  $ find $HOME -print
  $ find ~ -print
  为了在当前目录中查找suid置位,文件属主具有读、写、执行权限,并且文件所属组的用户和其他用户具有读和执行的权限的文件,可以用:
  $ find . -type  f -perm 4755 -print
  为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径,可以用:
  $ find / -type  f -size 0 -exec ls-l {} \;
  为了查找/var/logs目录中更改时间在7日以前的普通文件,并删除它们,可以用:
  $ find /var/logs  -type f -mtime +7 -exec rm {} \;
  为了查找系统中所有属于audit组的文件,可以用:
  $ find / -name  -group audit -print
我们的一个审计系统每天创建一个审计日志文件。日志文件名的最后含有数字,这样我们一眼就可以看出哪个文件是最新的,哪个是最旧的。Admin.log文 件编上了序号:admin.log.001、admin.log.002等等。下面的find命令将删除/logs目录中访问时间在7日以前、含有数字后 缀的admin.log文件。该命令只检查三位数字,所以相应日志文件的后缀不要超过999。
  $ find / logs  -name 'admin.log[0-9][0-9]' [-0a-t9i]me  +7 -exec rm {} \;
  为了查找当前文件系统中的所有目录并排序,可以用:
  $ find . -type  d -print -local -mount | sort
  为了查找系统中所有的rmt磁带设备,可以用:
  $ find /dev/rmt  -print
  

  2.2xargs

  

在使用find命令的-exec选项处理匹配到的文件时,find命令将所有匹配到的文件一起传递给exec执行。不幸的是,有些系统对能够传递给 exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令 的用处所在,特别是与find命令一起使用。Find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像 -exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。在有些系统中,使用-exec选项会为处理每一个匹配到的文 件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;而使用 xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的 选项及系统内核中相应的可调参数来确定。
  让我们来看看xargs命令是如何同find命令一起使用的,并给出一些例子。
  下面的例子查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件:
  $ find / -type f -print | xargs file
  /etc/protocols:  Enghlish text
  /etc/securetty:  ASCII test
  
  下面的例子在整个系统中查找内存信息转储文件(coredump),然后把结果保存到/tmp/core.log文件中:
  $ find . -name  "core" -print | xargs echo "" >/tmp/core.log
  下面的例子在/apps/audit目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限:
  $ find /apps/audit  -perm -7 -print | xargs chmod o -w
  在下面的例子中,我们用grep命令在所有的普通文件中搜索device这个词:
  $ find / -type  f -print | xargs grep "device"
  在下面的例子中,我们用grep命令在当前目录下的所有普通文件中搜索DBO这个词:
  $ find . -name *\ -type f -print  | xargs grep "DBO"
  注意,在上面的例子中,\用来取消find命令中的*在shell中的特殊含义。
  

  2.3
小结

  

  find命令是一个非常优秀的工具,它可以按照用户指定的准则来匹配文件。使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。

 

 

 

15条 linux Find 命令实际使用方法

 

This article is written by SathiyaMoorthy
作者: SathiyaMoorthy

Apart from thebasic operation of looking for files under a directory structure, youcan also perform several practical operations using find command thatwill make your command line journey easy.
Find命令除了在目录结构中查找文件之外,还可以用来做一些其他比较有实际意义的操作。

In this article, let us review 15 practical examples of Linux find command that will be very useful to both newbies and experts.
本文中的15条tips,对老鸟和菜鸟都会很有帮助

First, create the following sampleempty files under your home directory to try some of the find commandexamples mentioned below.
首先在你的HOME目录下建立一些空的文件,并尝试下面的一些命令。
# vim create_sample_files.sh  //使用vi创建文件,并输入下面的内容
touch MybashProgram.sh
touch mycprogram.c
touch MyCProgram.c
touch Program.c

mkdir backup
cd backup

touch MybashProgram.sh
touch mycprogram.c
touch MyCProgram.c
touch Program.c

# chmod +x create_sample_files.sh  //添加可运行属性

# ./create_sample_files.sh                //运行脚本

# ls -R                                                  //查看执行的结果
.:
backup                  MybashProgram.sh  MyCProgram.c
create_sample_files.sh  mycprogram.c      Program.c

./backup:
MybashProgram.sh  mycprogram.c  MyCProgram.c  Program.c1. Find Files Using Name 根据文件名查找This is a basic usage of the find command. This example finds allfiles with name ? MyCProgram.c in the current directory and all it’ssub-directories.
这个是Find命令最基本的操作,下面的例子是在当前目录和它所有子目录中查找MyCProgramm.c
# find -name "MyCProgram.c"
./backup/MyCProgram.c
./MyCProgram.c2. Find Files Using Name and Ignoring Case 在1的基础上忽略文件名的大小写This is a basic usage of the find command. This example finds allfiles with name ? MyCProgram.c (ignoring the case) in the currentdirectory and all it’s sub-directories.
这也是Find命令的基本操作之一.下面的例子是在当前目录及所有子目录中查找MyCProgram.c(忽略大小写)
# find -iname "MyCProgram.c"
./mycprogram.c
./backup/mycprogram.c
./backup/MyCProgram.c
./MyCProgram.c3. Limit Search To Specific Directory Level Using mindepth and maxdepth     查找指定目录并且设置查找的目录深度
Find the passwd file under all sub-directories starting from root directory.
下面的例子是在根目录及所有子目录中查找passwd文件
# find / -name passwd
./usr/share/doc/nss_ldap-253/pam.d/passwd
./usr/bin/passwd
./etc/pam.d/passwd
./etc/passwd
Find the passwd file under root and one level down. (i.e root ? level 1, and one sub-directory ? level 2)
查找根目录和根目录的和只展开一级的子目录中查找# find -maxdepth 2 -name passwd
./etc/passwd
Find the passwd file under root and two levels down. (i.e root ? level 1, and two sub-directories ? level 2 and 3 )
在根目录和根目录下展开两级查找passwd文件
# find / -maxdepth 3 -name passwd
./usr/bin/passwd
./etc/pam.d/passwd
./etc/passwd
Find the password file between sub-directory level 2 and 4.
在根目录的第二级和第四级之间查找
# find -mindepth 3 -maxdepth 5 -name passwd
./usr/bin/passwd
./etc/pam.d/passwd4. Executing Commands on the Files Found by the Find Command.     查找到文件后,执行其他的命令
In the example below, the find command calculates the md5sum of allthe files with the name MyCProgram.c (ignoring case). {} is replaced bythe current file name.
下面的例子展示了在查找到文件后,计算文件的MD5值
# find -iname "MyCProgram.c" -exec md5sum {} \;
d41d8cd98f00b204e9800998ecf8427e  ./mycprogram.c
d41d8cd98f00b204e9800998ecf8427e  ./backup/mycprogram.c
d41d8cd98f00b204e9800998ecf8427e  ./backup/MyCProgram.c
d41d8cd98f00b204e9800998ecf8427e  ./MyCProgram.c5. Inverting the match. 查找不匹配的文件Shows the files or directories whose name are not MyCProgram.c.Since the maxdepth is 1, this will look only under current directory.
查找文件名不是MyCProgramm.c的文件,注意由于maxdepth是1,将只在当前目录中查找。
# find -maxdepth 1 -not -iname "MyCProgram.c"
.
./MybashProgram.sh
./create_sample_files.sh
./backup
./Program.c6. Finding Files by its inode Number. 根据inode number查找Every file has an unique inode number, using that we can identifythat file. Create two files with similar name. i.e one file with aspace at the end.
某些时候,有些文件的文件名非常相似,肉眼比较难以分辨,这时候可以根据他们唯一的inode number进行查找
# touch "test-file-name"

# touch "test-file-name "         //这个文件名最后有一个空格

[Note: There is a space at the end]

# ls -1 test*
test-file-name
test-file-name

From the ls output, you cannot identify which file has the spaceat the end. Using option -i, you can view the inode number of the file,which will be different for these two files.
使用ls命令查看的时候,我们不能分辨这两个文件,使用ls命令的选项-i则可以查看他们的inode number
# ls -i1 test*
16187429 test-file-name
16187430 test-file-name
You can specify inode number on a find command as shown below.In this example, find command renames a file using the inode number.
在使用find命令的时候可以指定inode number
# find -inum 16187430 -exec mv {} new-test-file-name \;

# ls -i1 *test*
16187430 new-test-file-name
16187429 test-file-name
You can use this technique when you want to do some operationwith the files which are named poorly as shown in the example below.For example, the file with name ? file?.txt has a special character init. If you try to execute “rm file?.txt”, all the following three fileswill get removed. So, follow the steps below to delete only the“file?.txt” file.
这个技巧还可以适用于当文件名中存在特殊字符的情况
# ls
file1.txt  file2.txt  file?.txt
Find the inode numbers of each file.# ls -i1
804178 file1.txt
804179 file2.txt
804180 file?.txt
Use the inode number to remove the file that had special character in it as shown below.
使用inodenumber删除包含特殊字符的文件
# find -inum 804180 -exec rm {} \;

# ls

file1.txt  file2.txt
[Note: The file with name "file?.txt" is now removed]
7. Find file based on the File-Permissions根据文件的权限查找
Following operations are possible.
可以进行以下的这几种操作

  • Find files that match exact permission
  • Check whether the given permission matches, irrespective of other permission bits
  • Search by giving octal / symbolic representation


For this example, let us assume that the directory contains thefollowing files. Please note that the file-permissions on these filesare different.
# ls -l
total 0
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
---------- 1 root root 0 2009-02-19 20:31 no_for_all
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
Find files which has read permission to group. Use the followingcommand to find all files that are readable by the world in your homedirectory, irrespective of other permissions for that file.
# find . -perm -g=r -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 0 2009-02-19 20:30 ./everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 ./all_for_all
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read
-rw-r----- 1 root root 0 2009-02-19 20:27 ./others_can_also_read
Find files which has read permission only to group.
# find . -perm g=r -type f -exec ls -l {} \;
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read
Find files which has read permission only to group [ search by octal ]
# find . -perm 040 -type f -exec ls -l {} \;
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read8. Find all empty files (zero byte file) in your home directory and it’s subdirectoryMost files of the following command output will be lock-files and place holders created by other applications.
# find ~ -empty
List all the empty files only in your home directory.
# find . -maxdepth 1 -empty
List only the non-hidden empty files only in the current directory.
# find . -maxdepth 1 -empty -not -name ".*"9. Finding the Top 5 Big FilesThe following command will display the top 5 largest file in thecurrent directory and it’s subdirectory. This may take a while toexecute depending on the total number of files the command has toprocess.
# find . -type f -exec ls -s {} \; | sort -n -r | head -510. Finding the Top 5 Small FilesTechnique is same as finding the bigger files, but the only difference the sort is ascending order.
# find . -type f -exec ls -s {} \; | sort -n  | head -5
In the above command, most probably you will get to see only theZERO byte files ( empty files ). So, you can use the following commandto list the smaller files other than the ZERO byte files.
# find . -not -empty -type f -exec ls -s {} \; | sort -n  | head -511. Find Files Based on file-type using option -typeFind only the socket files.
# find . -type s
Find all directories
# find . -type d
Find only the normal files
# find . -type f
Find all the hidden files
# find . -type f -name ".*"
Find all the hidden directories
# find -type d -name ".*"12. Find files by comparing with the modification time of other file.Show files which are modified after the specified file. Thefollowing find command displays all the files that are created/modifiedafter ordinary_file.
# ls -lrt
total 0
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
---------- 1 root root 0 2009-02-19 20:31 no_for_all

# find -newer ordinary_file
.
./everybody_read
./all_for_all
./no_for_all13. Find Files by SizeUsing the -size option you can find files by size.

Find files bigger than the given size
# find ~ -size +100M
Find files smaller than the given size
# find ~ -size -100M
Find files that matches the exact given size
# find ~ -size 100M
Note: ? means less than the give size, + means more than the given size, and no symbol means exact given size.
14. Create Alias for Frequent Find OperationsIf you find some thing as pretty useful, then you can make it as an alias. And execute it whenever you want.

Remove the files named a.out frequently.
# alias rmao="find . -iname a.out -exec rm {} \;"
# rmao

Remove the core files generated by c program.
# alias rmc="find . -iname core -exec rm {} \;"
# rmc
15. Remove big archive files using find commandThe following command removes *.zip files that are over 100M.
# find / -type f -name *.zip -size +100M -exec rm -i {} \;"
Remove all *.tar file that are over 100M using the alias rm100m(Remove 100M). Use the similar concepts and create alias like rm1g,rm2g, rm5g to remove file size greater than 1G, 2G and 5G respectively.
# alias rm100m="find / -type f -name *.tar -size +100M -exec rm -i {} \;"
# alias rm1g="find / -type f -name *.tar -size +1G -exec rm -i {} \;"
# alias rm2g="find / -type f -name *.tar -size +2G -exec rm -i {} \;"
# alias rm5g="find / -type f -name *.tar -size +5G -exec rm -i {} \;"

# rm100m
# rm1g
# rm2g
# rm5g

This article was written by SathiyaMoorthy, author of numbertotext Vim plugin, which will replace the numbers with the equivalent text inside Vim. The Geek Stuff welcomes your tips and guest articles.

Howdo you use find command? What is your favorite find command example?Please leave a comment and let us know. Also, if you would like to knowhow to use find command under a specific scenario, please let us knowyour questions in the comments section.

 

 

find 命令用法

find / -name access_log 2>/dev/null
find /etc -name ‘*srm*’
find / -amin -10 # 查找在系统中最后10分钟访问的文件
find / -atime -2 # 查找在系统中最后48小时访问的文件
find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件
find / -mtime -1 #查找在系统中最后24小时里修改过的文件
find / -cmin -5 # 查找在系统中最后5分钟里被改变状态的文件
find / -ctime -1 #查找在系统中最后24小时里被改变状态的文件
find / -user reda #查找在系统中属于fred这个用户的文件
find / -not -user reda #查找在系统中不属于FRED这个用户的文件
find / -group redagrp # 查找在系统中属于redagrp组的文件
find / -gid 501 #查找系统中属于组id为501的文件
find / -user fred -a -group redagrp
find / -user reda -o -user tracy
find / -nouser #查找在系统中属于作废用户的文件
find / -empty # 查找在系统中为空的文件或者为空的文件夹
find / -false #查找系统中总是错误的文件
find / -size +5k #查找系统中大于5k字节的文件
find / -size +5c #查找系统中大于5字节的文件
find / -perm +6000
find / -type b

文件类型:
b   块(缓冲)设备.
c   字符设备.
d   目录.
p   有名管道(FIFO).
f    规则文件.
l    符号链结.
s    SOCKET.
find / -maxdepth 2 -name fred
find /tmp -size +10000000c -and -mtime +2
find / -user reda -or -user tracy
find /tmp ! -user reda
find / -name "httpd.conf" -ls
find / -user reda -exec ls -l {} \;
find / -user reda -ok #确认后执行
find / -user reda | xargs ls -l

 

find命令使用经验

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

pathname find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
  -print find命令将匹配的文件输出到标准输出。
  -exec find命令对匹配的文件执行该参数所给出的shell命令。
  相应命令的形式为'command'{} \;,注意{}和\;之间的空格。
  -ok和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行


文件状态判断:

-mtime: 指定时间文件内容被修改过
-ctime: 指定时间文件权限被修改过
-atime: 指定时间文件被读取过

找出3天“以前”被修改过的文档
# find /var/log/ -mtime +3 -type f -print

找出3天“”被修改过的文档
# find /var/log/ -mtime -3 -type f -print

找出第3天被修改过的文档.
# find /var/log/ -mtime 3 -type f -print
或这样写:
#find /var/log/ -mtime +2 -mtime -4 -type f -print

注:
访问过用amin,修改过用mmin,文件状态改变过用cmin
精确到分钟的用amin,mmin,cmin,精确到天的用atime,mtime,ctime

find 与exec xargs  是常用组合哦,我补发一点。

xargs

xargs - build and execute command lines from standard input

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

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

在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;

而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

来看看xargs命令是如何同find命令一起使用的,并给出一些例子。

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

#find . -type f -print | xargs file
./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text
./.kde/Autostart/.directory:      ISO-8859 text\
......

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

$ find / -name "core" -print | xargs echo "" >/tmp/core.log

上面这个执行太慢,我改成在当前目录下查找

#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6

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

# ls -l
drwxrwxrwx    2 sam      adm          4096 10月 30 20:14 file6
-rwxrwxrwx    2 sam      adm             0 10月 31 01:01 http3.conf
-rwxrwxrwx    2 sam      adm             0 10月 31 01:01 httpd.conf

# find . -perm -7 -print | xargs chmod o-w
# ls -l
drwxrwxr-x    2 sam      adm          4096 10月 30 20:14 file6
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 http3.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf

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

# find . -type f -print | xargs grep "hostname"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

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

# find . -name \* -type f -print | xargs grep "hostnames"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

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

find命令配合使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。

 

EXEC

使用exec或ok来执行shell命令

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的

在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。

exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

例如:为了用ls -l命令列出所匹配到的文件,可以把ls -l命令放在find命令的-exec选项中

# find . -type f -exec ls -l {  } \;
-rw-r--r--    1 root     root        34928 2003-02-25  ./conf/httpd.conf
-rw-r--r--    1 root     root        12959 2003-02-25  ./conf/magic
-rw-r--r--    1 root     root          180 2003-02-25  ./conf.d/README

上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。
在/logs目录中查找更改时间在5日以前的文件并删除它们:

$ find logs -type f -mtime +5 -exec rm {  } \;

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

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

$ find . -name "*.conf"  -mtime +5 -ok rm {  } \;
< rm ... ./conf/httpd.conf > ? n

 

 

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

任何形式的命令都可以在-exec选项中使用。

在下面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个sam用户。

# find /etc -name "passwd*" -exec grep "sam" {  } \;
sam:x:501:501::/usr/sam:/bin/bash

 

find用法小结

说明:以下内容为搜索整理。



===============================
1. 通过文件的特征查找:
===============================

1) 按文件名
   find /    -name httpd.conf
   find /usr -name httpd.conf
   find /etc -name '*srm*'

2) 按大小
   find / -size 1500c      # 查找文件大小为1,500 bytes的文件,字符 c 表明这个要查找的文件的大小是以bytes为单位。
   find/ -size +10000000c  # "+”是表示要求系统只列出大于指定大小的文件,  "-”表示小于
   find / -empty           # 查找在系统中为空的文件或者文件夹

   -size:表示文件大小,+表示大于某个数,-表示小于某个数。c表示单位是字节,你可以将c换成k,M,G。

3) 按时间
   find / -amin -10        # 查找在系统中最后10分钟访问的文件
   find / -atime -2        # 查找在系统中最后48小时访问的文件

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

   find / -cmin -5         # 查找在系统中最后5分钟里被改变状态的文件
   find / -ctime -1        # 查找在系统中最后24小时里被改变状态的文件
   
  访问过用amin,修改过用mmin,文件状态改变过用cmin
  精确到分钟的用amin,mmin,cmin,精确到天的用atime,mtime,ctime
  在5分钟之内的用-5,在5分钟以上的用+5

4) 按用户
   find / -user fred       # 查找在系统中属于FRED这个用户的文件
   find / -group cat       # 查找在系统中属于 groupcat的文件
   find / -nouser          # 查找在系统中属于作废用户的文件
  
5) 其他
   -false 查找系统中总是错误的文件
   -fstype type 查找系统中存在于指定文件系统的文件,例如:ext2 .
   -gid n 查找系统中文件数字组 ID 为 n的文件
   -group gname 查找系统中文件属于gnam文件组,并且指定组和ID的文件
  


===============================
2. 通过文件的特征查找:
===============================

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

   选项               用途描述
-daystart     .   .测试系统从今天开始24小时以内的文件,用法类似-amin
-depth             使用深度级别的查找过程方式,在某层指定目录中优先查找文件内容
-follow            遵循通配符链接方式查找; 另外,也可忽略通配符链接方式查询

-maxdepth levels   在某个层次的目录中按照递减方法查找
-mount             不在文件系统目录中查找, 用法类似 -xdev.
-noleaf            禁止在非UNUX文件系统,MS-DOS系统,CD-ROM文件系统中进行最优化查找

-help              显示命令摘要
-version           打印版本数字


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

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

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


===============================
3. 使用混合查找方式:
===============================

find /tmp -size +10000000c -and -mtime +2       // -and
find /    -user fred       -or -user george     // -or   在/tmp目录中查找属于fred或者george这两个用户的文件
find /tmp ! -user panda                         // -or   在/tmp目录中查找所有不属于panda的文件
命令就可以解决了。很简单。






查找并显示文件的方法
查找到某个文件是我们的目的,我们更想知道查找到的文件的详细信息和属性,
find / -name "httpd.conf" -ls


下面的表格就是一些常用的查找文件并显示文件信息的参数和使用方法
选项      用途描述
-exec command;   查找并执行命令
-fprint file   打印文件完整文件名
-fprint0 file   打印文件完整文件名包括空的文件
-fprintf file format     打印文件格式
-ok command;             给用户命令执行操作,根据用户的Y 确认输入执行
-printf format           打印文件格式
-ls                      打印同种文件格式的文件.





===============================
2. 普通用户无错误查找:
===============================

    find / -name access_log 2>/dev/null

    说明:当普通用户使用"find”命令来查询某些没有相应权限文件目录时(Linux系统中系统管理员ROOT可以把某些文件目录设置成禁止访问模式)
,往往会出现"Permissiondenied."(禁止访问)字样。 2>/dev/null就是表明系统将把错误信息输送到stderrstream 2中.


--------------------------------
-exec command;
--------------------------------
删除一个目录中的全部文件
cd “目的目录”
find . -name * -exec rm -f {} \;



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

1) command命令的终止,使用 ';' (分号)来判定,在后面必须有一个 ';'
   出于不明原因, ';'需要用'\'来转义  ,所以命令整体形式为: -exec rm -f {} \;

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



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

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

2.删除所有临时文件

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

 

3. 使用混合查找方式:
===============================

find /tmp -size +10000000c -and -mtime +2       // -and
find /    -user fred       -or -user george     // -or   在/tmp目录中查找属于fred或者george这两个用户的文件
find /tmp ! -user panda                         // -or   在/tmp目录中查找所有不属于panda的文件
命令就可以解决了。很简单。


这样出错

我系统是REDHAT AS4

我用这样可以

find /tmp !  \( -user 0 -o -user 500 -o -user 501 \) -exec ls -l {} \;

 

把用户名改成UID就可以了

 

find与xargs的用法------实际例子(ZT)

(1) find . -type f -exec ls -l {} \;
解释:查找当前路径下的所有普通文件,并把它们列出来。

(2)find logs -type f -mtime +5 -exec rm {} \;
解释:删除logs目录下更新时间为5日以上的文件。

(3)find . -name "*.log" -mtime +5 -ok rm {} \;
解释:删除当前路径下以。log结尾的五日以上的文件,删除之前要确认。

(4) find ~ -type f -perm 4755 -print
解释:查找$HOME目录下suid位被设置,文件属性为755的文件打印出来。

说明: find在有点系统中会一次性得到将匹配到的文件都传给exec,但是有的系统对exec的命令长度做限制,就会报:”参数列太长“,这就需要使用xargs。xargs是部分取传来的文件。

(5)find / -type f -print |xargs file
解释:xargs测试文件分类

(6)find . -name "core*" -print|xargs echo " ">/tmp/core.log
解释:将core文件信息查询结果报存到core。log日志。

(7)find / -type f -print | xargs chmod o -w

(8)find . -name * -print |xargs grep "DBO"

posted @ 2013-09-05 19:44  摩斯电码  阅读(627)  评论(0编辑  收藏  举报