Linux find命令详解

简介:

由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统(  NFS),find命令在该文件系统中同样有效,只你具有相应的权限。

在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。

一、find 命令格式

1、find命令的一般形式为;

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

2、find命令的参数;

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

3、find命令选项

-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小时被改变文件数据的文件

4、使用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、查找当前用户主目录下的所有文件:

下面两种方法都可以使用

$ find $HOME  -print
$ find ~ -print



2、让当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件;

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

3、为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径;

$ find / -type f -size 0 -exec ls -l {}  \;

4、查找/var/logs目录中更改时间在7日以前的普通文件,并在删除之前询问它们;

$ find /var/logs -type f -mtime +7 -ok rm {}  \;

5、为了查找系统中所有属于root组的文件;

$find . -group root -exec ls -l {} \;
-rw-r--r--  1 root root 595 10月 31 01:09 ./fie1

6、find命令将删除当目录中访问时间在7日以来、含有数字后缀的admin.log文件。

该命令只检查三位数字,所以相应文件的后缀不要超过999。先建几个admin.log*的文件  ,才能使用下面这个命令

$ find . -name  "admin.log[0-9][0-9][0-9]" -atime -7 -ok
rm { } \;
< rm ...  ./admin.log001 > ? n
< rm ... ./admin.log002 > ? n
< rm ...  ./admin.log042 > ? n
< rm ... ./admin.log942 > ?  n

7、为了查找当前文件系统中的所有目录并排序;

$ find . -type d | sort

8、为了查找系统中所有的rmt磁带设备;

$ find /dev/rmt -print

三、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可以使用户对所匹配到的文件执行几乎所有的命令。

四、find  命令的参数

下面是find一些常用参数的例子,有用到的时候查查就行了,像上面前几个贴子,都用到了其中的的一些参数,也可以用man或查看论坛里其它贴子有find的命令手册

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 "*"  -print 或find . -print

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

$ find / -name "*"  -print

如果想在当前目录查找文件名以两个小写字母开头,跟着是两个数字,最后是.txt的文件,下面的命令就能够返回名为ax37.txt的文件:

$find . -name  "[a-z][a-z][0--9][0--9].txt" -print

2、用perm选项

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

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

$ find . -perm 755  -print

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

# ls -l
-rwxrwxr-x  2 sam adm 0 10月 31 01:01 http3.conf
-rw-rw-rw- 1 sam adm 34890 10月 31 00:57  httpd1.conf
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf
drw-rw-rw- 2  gem group 4096 10月 26 19:48 sam
-rw-rw-rw- 1 root root 2792 10月 31 20:19  temp
# find . -perm 006
# find . -perm  -006
./sam
./httpd1.conf
./temp

-perm mode:文件许可正好符合mode

-perm +mode:文件许可部分符合mode

-perm -mode: 文件许可完全符合mode

3、忽略某个目录

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

如果希望在/apps目录下查找文件,但不希望在/apps/bin目录下查找,可以用:

$ find /apps -path  "/apps/bin" -prune -o -print


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

比如要在/usr/sam目录下查找不在dir1子目录之内的所有文件

find /usr/sam -path  "/usr/sam/dir1" -prune -o -print

find [-path ..]  [expression] 在路径列表的后面的是表达式

-path "/usr/sam" -prune -o -print 是 -path "/usr/sam" -a -prune -o
-print  的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似如果 -path "/usr/sam"  为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑表达式为假。如果 -path "/usr/sam" -a  -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。

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

if -path "/usr/sam"  then
          -prune
else
          -print

避开多个文件夹

find /usr/sam /( -path  /usr/sam/dir1 -o -path /usr/sam/file1 /) -prune -o -print

圆括号表示表达式的结合。

/ 表示引用,即指示 shell  不对后面的字符作特殊解释,而留给 find 命令去解释其意义。

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

#find /usr/sam /(-path  /usr/sam/dir1 -o -path /usr/sam/file1 /) -prune -o -name "temp"  -print


5、使用user和nouser选项

按文件属主查找文件,如在$HOME目录中查找文件属主为sam的文件,可以用:

$ find ~ -user sam  -print

在/etc目录下查找文件属主为uucp的文件:

$ find /etc -user uucp  -print

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

例如,希望在/home目录下查找所有的这类文件,可以用:

$ find /home -nouser  -print


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

其中,!是逻辑非符号。

查找更改时间比文件sam新但比文件temp旧的文件:

例:有两个文件

-rw-r--r-- 1 sam adm 0  10月 31 01:07 fiel
-rw-rw-rw- 1 sam adm 34890 10月 31 00:57  httpd1.conf
-rwxrwxr-x 2 sam adm 0 10月 31 01:01 httpd.conf
drw-rw-rw- 2  gem group 4096 10月 26 19:48 sam
-rw-rw-rw- 1 root root 2792 10月 31 20:19  temp
# find -newer httpd1.conf ! -newer temp -ls
1077669 0 -rwxrwxr-x 2  sam adm 0 10月 31 01:01 ./httpd.conf
1077671 4 -rw-rw-rw- 1 root root 2792 10月  31 20:19 ./temp
1077673 0 -rw-r--r-- 1 sam adm 0 10月 31 01:07  ./fiel

查找更改时间在比temp文件新的文件:

$ find . -newer temp  -print



9、使用type选项

在/etc目录下查找所有的目录,可以用:

$ find /etc -type d  -print

在当前目录下查找除目录以外的所有类型的文件,可以用:

$ find . ! -type d  -print

在/etc目录下查找所有的符号链接文件,可以用

$ find /etc -type l  -print


10、使用size选项

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

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

$ find . -size  +1000000c -print

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

$ find /home/apache  -size 100c -print

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

$ find . -size +10  -print

11、使用depth选项

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

在下面的例子中, find命令从文件系统的根目录开始,查找一个名为CON.FILE的文件。

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

$ find / -name  "CON.FILE" -depth -print


12、使用mount选项

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

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

$ find . -name "*.XC"  -mount -print

-----------------------------------------------------------------------------------------------------------------------

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


This article is written by SathiyaMoorthy

作者: SathiyaMoorthy

Apart from the basic  operation of looking for files under a directory structure, you can also perform  several practical operations using find command that will 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 sample empty  files under your home directory to try some of the find command examples  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.c

1. Find Files Using Name 根据文件名查找

This is a basic usage of the find command. This example finds all files with  name — MyCProgram.c in the current directory and all it’s sub-directories.

这个是Find命令最基本的操作,下面的例子是在当前目录和它所有子目录中查找MyCProgramm.c

# find -name "MyCProgram.c"./backup/MyCProgram.c



./MyCProgram.c

2. Find Files Using Name and Ignoring Case  在1的基础上忽略文件名的大小写

This is a basic usage of the find command. This example finds all files with  name — MyCProgram.c (ignoring the case) in the current directory and all it’s  sub-directories.

这也是Find命令的基本操作之一.下面的例子是在当前目录及所有子目录中查找MyCProgram.c(忽略大小写)

# find -iname "MyCProgram.c"./mycprogram.c



./backup/mycprogram.c



./backup/MyCProgram.c



./MyCProgram.c

3. 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/passwd

4. Executing Commands on the Files Found by the Find  Command.

    查找到文件后,执行其他的命令

In the example below, the find command calculates the md5sum of all the files  with the name MyCProgram.c (ignoring case). {} is replaced by the current file  name.

下面的例子展示了在查找到文件后,计算文件的MD5值

# find -iname "MyCProgram.c" -exec md5sum {} \;d41d8cd98f00b204e9800998ecf8427e  ./mycprogram.c



d41d8cd98f00b204e9800998ecf8427e  ./backup/mycprogram.c



d41d8cd98f00b204e9800998ecf8427e  ./backup/MyCProgram.c



d41d8cd98f00b204e9800998ecf8427e  ./MyCProgram.c

5. 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.c

6. Finding Files by its inode Number. 根据inode number查找

Every file has an unique inode number, using that we can identify that file.  Create two files with similar name. i.e one file with a space 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 space at 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 operation with the  files which are named poorly as shown in the example below. For example, the  file with name — file?.txt has a special character in it. If you try to execute  “rm file?.txt”, all the following three files will get removed. So, follow the  steps below to delete only the “file?.txt” file.

这个技巧还可以适用于当文件名中存在特殊字符的情况

# lsfile1.txt  file2.txt  file?.txt
Find the inode numbers of each file.
# ls -i1804178 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 {} \;        # lsfile1.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 the following  files. Please note that the file-permissions on these files are 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 following command  to find all files that are readable by the world in your home directory,  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_read

8. Find all empty files (zero byte file) in your home  directory and it’s subdirectory

Most 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 Files

The following command will display the top 5 largest file in the current  directory and it’s subdirectory. This may take a while to execute depending on  the total number of files the command has to process.

# find . -type f -exec ls -s {} \; | sort -n -r | head -5

10. Finding the Top 5 Small Files

Technique 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 the ZERO  byte files ( empty files ). So, you can use the following command to list the  smaller files other than the ZERO byte files.

# find . -not -empty -type f -exec ls -s {} \; | sort -n  | head -5

11. Find Files Based on file-type using option -type

Find 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. The following find  command displays all the files that are created/modified after  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_all

13. Find Files by Size

Using 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 Operations

If 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 command

The 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.

-------------------------------------------------------------------------------------------------------------------------------------------------

Linux Find 命令精通指南


简单介绍这一无处不在的命令的强大的方面以及混乱的方面。

2008 年 7 月发布

Linux find 命令是所有 Linux 命令中最有用的一个,同时也是最混乱的一个。它很难,因为它的语法与其他 Linux  命令的标准语法不同。但是,它很强大,因为它允许您按文件名、文件类型、用户甚至是时间戳查找文件。使用find  命令,您不但可以找到具这些属性任意组合的文件,还可以对它找到的文件执行操作。

本文的目的是,通过概述 find 命令的用途和潜能,简化该命令的学习和使用。同时,它将针对 find  命令的某些最强大但最混乱的方面提供一个基本的指南和参考。

[注意:本文使用的 find 版本是 GNU 版本,因此,某些细节可能与其他版本的 find 有所不同。]

基本格式

开始之前,我们先来看一下 find 命令的基本结构:

find   start_directory  test  options   criteria_to_match



action_to_perform_on_results

在以下命令中,find  将开始在当前目录(用“.”表示)中查找任何扩展名为“java”的文件:

find . -name  "*.java"

下面是该命令所找到的命令的缩略清单:

find . -name  "*.java"



./REGEXPvalidate/src/oracle/otnsamples/plsql/ConnectionManager.java



./REGEXPvalidate/src/oracle/otnsamples/plsql/DBManager.java



..

[注意:如果您从本文剪切并粘贴来运行该 find 命令,您可能需要使用自己的键盘替换双引号 (“”) 才能得出正确的结果。]

以下命令将执行相同的操作。在这两种情况下,您都需要对通配符进行转义以确保它传递到 find 命令并且不由 shell  解释。因此,请将您的搜索字符串放到引号里,或者在它前面加上反斜线:

find . -name  /*.java

尽管 find  的所有参数均为可选,但是如果您未指定从哪里开始搜索,搜索默认将在当前目录中开始。如果您不指定要匹配的测试连接、选项或值,您的结果将不完整或者无区别。

运行以下三个  find 命令将得出同样的结果 — 当前目录和所有子目录中的所有文件(包括隐藏文件)的完整清单:

find 



find .



find . -print

这类似于运行一个带 -la 选项的 ls 命令。如果您希望上述命令的输出包含完整的路径名(或许是为了备份),您将需要指定起始目录的完整路径:

find /home/bluher -name /*.java



/home/bluher/plsql/REGEXPvalidate/src/oracle/otnsamples/plsql/ConnectionManager.java



/home/bluher/plsql/REGEXPvalidate/src/oracle/otnsamples/plsql/DBManager.java/



...

您还可以在搜索字符串中指定多个起始目录。如果以具有相应权限的用户身份运行,以下命令将下到 /usr、/home /tmp 目录查找所有 jar 文件:

find /usr /home  /tmp -name "*.jar"

但是,如果您没有相应的权限,您在开始浏览许多系统目录时将生成错误消息。以下是一个示例:

find:  /tmp/orbit-root: Permission denied

您可以通过附加您的搜索字符串来避免混乱的输出,如下所示:

find /usr /home  /tmp -name "*.jar" 2>/dev/null

这会将所有错误消息发送到空文件,因此提供清理器输出。

默认情况下,find 是区分大小写的。对于不区分大小写的 find,将 -iname 测试替换为 -name 测试。

find downloads  -iname "*.gif"



downloads/.xvpics/Calendar05_enlarged.gif



downloads/lcmgcfexsmall.GIF

除文件名外,您还可以按类型搜索文件。例如,您可以使用以下命令查找一个目录中的所有子目录:

find . -type d

您可以使用以下命令查找您的/usr 目录中的所有符号链接:

find /usr -type l

这可能会列出 3,000 多个链接。以下的任何一个命令使用根权限运行都将列出 /usr 目录中的链接以及它所指向的文件:

# find /usr/bin  -type l  -name "z*" -exec ls  -l {} \;



lrwxrwxrwx 1 root  root 8 Dec 12 23:17 /usr/bin/zsh -> /bin/zsh



lrwxrwxrwx 1 root  root 5 Dec 12 23:17 /usr/bin/zless -> zmore



lrwxrwxrwx 1 root  root 9 Dec 12 23:17 /usr/bin/zcat -> /bin/zcat
find /usr/bin -type  l  -name "z*" -ls

但是,第二个更短的命令将列出更多的文件,以及目录和 inode 信息:在本文后面的部分中,我们将讨论 -exec 和 -ls 操作的用法。

其他 find 可以找到的文件类型包括:

b — 块(缓存)特殊
c — 字符(未缓存)特殊
p — 命名管道 (FIFO)
s — 套接字

使用根作为 find  命令的起点会极大地降低系统的速度。如果您必须运行这样一个命令,您可以在非高峰时段或晚上运行它。您可以使用以下语法将输出重定向到一个文件:

find  /   -print > masterfilelist.out

如果您错误地输入一个 find 命令,生成大量不必要的输出,只需按 CTRL-C 中断该命令,这将停止最近执行的命令。

在具多个文件系统的企业网络上,限制 find 查找的文件也是一个特别好用的方法。尽可能多地使用选项和测试以减少系统上的负载。用于此目的的两个最有用的选项是  -xdev 和 -mount。它们通过阻止 find 下到其他文件系统(如 MS-DOS、CD-ROM 或  AFS)上的目录中缩短了搜索范围。这将搜索限制为同一类型的文件系统作为起始目录。

如果运行 mount 命令,双引导系统上的用户可以使用这些选项。假设涉及 Windows 分区,您可以使用类似以下的命令安装它:

mount -t vfat  /dev/sda1 /mnt/msdos

您使用的实际命令取决于您的系统设置。您可以通过运行 df 或执行以下命令验证该分区已安装:

find /mnt/msdos  -name "*.txt" 2> /dev/null

您应该看到 MS Windows 分区上列出了很多的文件。现在,运行以下带 -mount 或 -xdev 选项的命令:

find / -name  "*.txt" -mount 2> /dev/null

或者

find / -name  "*.txt" -xdev 2> /dev/null

还可以使用 -fstype 测试明确告知 find 在哪个文件系统中查找,如以下示例中所示:

find / -name  "*.txt" -fstype vfat 2> /dev/null

查找时间

find 命令有几个用于根据您系统的时间戳搜索文件的选项。这些时间戳包括

mtime  文件内容上次修改时间 
atime —  文件被读取或访问的时间
ctime — 文件状态变化时间

mtime 和 atime 的含义都是很容易理解的,而 ctime 则需要更多的解释。由于 inode  维护着每个文件上的元数据,因此,如果与文件有关的元数据发生变化,则 inode  数据也将变化。这可能是由一系列操作引起的,包括创建到文件的符号链接、更改文件权限或移动了文件等。由于在这些情况下,文件内容不会被读取或修改,因此 mtime 和  atime 不会改变,但 ctime 将发生变化。

这些时间选项都需要与一个值 n 结合使用,指定为 -n、n 或 +n

-n 返回项小于 
+n
 返回项大于 
n
 返回项正好与  n 相等

下面,我们来看几个例子,以便于理解。以下命令将查找在最近 1 小时内修改的所有文件:

find . -mtime -1



./plsql/FORALLSample



./plsql/RegExpDNASample



/plsql/RegExpSample

用 1 取代 -1 运行同一命令将查找恰好在 1 小时以前修改的所有文件:

find . -mtime 1

上述命令不会生成任何结果,因为它要求完全吻合。以下命令查找 1 个多小时以前修改的所有文件:

find . -mtime +1

默认情况下,-mtime、-atime 和 -ctime 指的是最近 24  小时。但是,如果它们前面加上了开始时间选项,则 24 小时的周期将从当日的开始时间算起。您还可以使用 mmin、amin 和 cmin 查找在不到 1  小时的时间内变化了的时间戳。

如果您在登录到您的帐户后立即运行以下命令,您将找到在不到 1 分钟以前读取的所有文件:

find . -amin -1



./.bashrc



/.bash_history



./.xauthj5FCx1

应该注意的是,使用 find 命令查找文件本身将更改该文件的访问时间作为其元数据的一部分。

您还可以使用 -newer、-anewer 和 –cnewer 选项查找已修改或访问过的文件与特定的文件比较。这类似于 -mtime、-atime 和  –ctime。

-newer 指内容最近被修改的文件
-anewer  指最近被读取过的文件
-cnewer 指状态最近发生变化的文件

要查找您的主目录中自上一个 tar 文件以来以某种方式编辑过的所有文件,使用以下命令:

find . -newer  backup.tar.gz

按大小查找文件

-size 选项查找满足指定的大小条件的文件。要查找所有大于 5MB 的用户文件,使用

find / -size  +5000000c 2> /dev/null



/var/log/lastlog



/var/log/cups/access_log.4



/var/spool/mail/bluher

结尾的“c”以字节为单位报告我们的结果。默认情况下,find 以 512  字节块的数量报告大小。如果我们将“c”替换为“k”,我们还会看到以千字节的数量报告的结果,如果使用“w”,则会看到以两字节字的数量报告的结果。

-size 选项经常用于搜索所有零字节文件并将它们移至 /tmp/zerobyte 文件夹。以下命令恰好可以完成这一任务:

find test -type f  -size 0 -exec mv {} /tmp/zerobyte \;

-exec 操作允许 find 在它遇到的文件上执行任何 shell 命令。在本文的后面部分,您将看到其用法的更多示例。大括号允许移动每个空文件。

选项 -empty 还可用于查找空文件:

find test -empty        



test/foo



test/test

按权限和所有者查找

要监视您的系统安全离不开 find 命令。您可以使用符号或八进制表示法查找面向广大用户开放的文件,如下所示:

find . -type f  -perm a=rwx -exec ls -l {} \;

或者

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



-rwxrwxrwx 1 bluher  users 0 May 24 14:14 ./test.txt

在这一部分中,在上面和下面的命令中,我们使用了 -exec ls -l  操作,因此,您可以看到返回的文件的实际权限。以下命令将查找可由“other”和组写入的文件:

find plsql -type f  -perm -ug=rw -exec ls -l {} \; 2>/dev/null

或者

find plsql -type f  -perm -220 -exec ls -l {} \; 2>/dev/null 



-rw-rw-rw- 1 bluher users 4303  Jun  7   2004 plsql/FORALLSample/doc/otn_new.css



-rw-rw-rw- 1 bluher users 10286 Jan  12  2005  plsql/FORALLSample/doc/readme.html



-rw-rw-rw- 1 bluher users 22647 Jan  12  2005  plsql/FORALLSample/src/config.sql



..

下一个命令将查找由用户、组或二者共同写入的文件:  

find plsql -type f  -perm /ug=rw -exec ls -l {} \; 2>/dev/null, or,



find plsql -type f  -perm /220 -exec ls -l {} \; 2>/dev/null 



-rw-r--r-- 1 bluher users 21473  May  3 16:02 plsql/regexpvalidate.zip



-rw-rw-rw- 1 bluher users 4303  Jun  7   2004 plsql/FORALLSample/doc/otn_new.css



-rw-rw-rw- 1 bluher users 10286 Jan  12  2005  plsql/FORALLSample/doc/readme.html



-rw-rw-rw- 1 bluher users 22647 Jan  12  2005  plsql/FORALLSample/src/config.sql

您可能会看到以下命令在 Web 和较早的手册中引用过:

find . -perm +220  -exec ls -l {} \; 2> /dev/null

+ 符号的作用与 / 符号相同,但是现在新版 GNU findutils 中不支持使用该符号。

要查找您的系统上所有人都可以写入的所有文件,使用以下命令:

find / -wholename  '/proc' -prune  -o  -type f -perm -0002 -exec ls -l {} \;



-rw-rw-rw- 1 bluher users 4303  Jun  7   2004/home/bluher/plsql/FORALLSample/doc/otn_new.css



-rw-rw-rw- 1 bluher users 10286 Jan  12  2005  /home/bluher/plsql/FORALLSample/doc/readme.html



...

第 4 个权限将在稍后进行讨论,但最后一个字段中的“2”是文件权限中的“other”字段,也称为写入位。我们在权限模式 0002  前面使用了破折号,以指明我们希望看到为 other 设置了写权限的文件,无论其他权限设置为什么。

上述命令还引入了三个新概念。针对文件模式“/proc”使用 -wholename 测试,如果该模式已找到,-prune 可防止 find  下到该目录中。布尔类型“-o”使 find 可以针对其他目录处理该命令的其余部分。由于每个表达式之间有一个假设的隐式and运算符  (-a),因此,如果左侧的表达式计算结果为 false,and 之后的表达式将不进行计算;因此需要 -o 运算符。Find 还支持布尔类型  -not、!,就像使用括号强行优先一样。

系统管理员经常使用 find 通过用户或组的名称或 ID 搜索特定用户或组的常规文件:

[root] $  find / -type f -user bluher -exec ls -ls {}  \;

下面是这样一个命令的高度精简的输出示例:

4 -rw-r--r-- 1 bluher users 48  May  1 03:09  /home/bluher/public_html/.directory



4 -rw-r--r-- 1 bluher users 925  May  1 03:09 /home/bluher/.profile

您还可以使用 find 按组查找文件:

[root] $ find /  -type f -group users
find / -type d -gid  100

该命令将列出由 ID 为 100 的组拥有的目录。要找到相应的 uid 或 gid,您可以针对 /etc/passwd 或 /etc/group 文件运行  more 或 cat 命令。

除了查找特定已知用户和组的文件外,您还会发现它对于查找没有这些信息的文件也很有用。下一个命令将识别未列在 /etc/passwd 或 /etc/group  文件中的文件:

find / -nouser -o  -nogroup

上述命令可能不会在您的系统上生成实际的结果。但是,它可用于识别或许在经常移动后没有用户或组的文件。

好了,现在我们可以解决本部分开始时提到的格外重要的权限了。

SGID 和 SUID 是特殊访问权限标志,可以分配给基于 UNIX  的操作系统上的文件和目录。设置它们是为了允许访问计算机系统的普通用户使用临时提升的权限执行二进制可执行文件。

find /  /( -perm -2000 -o -perm -4000 /) -ls



167901   12 -rwsr-xr-x   1 root     root         9340 Jun 16  2006 /usr/bin/rsh



167334   12 -rwxr-sr-x   1 root     tty         10532 May  4  2007 /usr/bin/wall

在上述命令中,您可以看到转义括号的使用。您还可以看到权限的不同。第一个文件设置了 SGID 权限,第二个文件设置了 SUID  权限。上述命令中的最后的操作与带 -exec ls -dils 操作的 find 效果类似。

控制 find

与 Linux 中的许多命令不同,find 不需要 -r 或 -R  选项即可下到子目录中。它默认情况下就这样操作。但是,有时您可能希望限制这一行为。因此,选项 -depth、-maxdepth 和 -mindepth 以及操作  -prune 就派上用场了。

我们已经看到了 -prune 是多么有用,下面让我们来看看 -depth、-maxdepth 和 -mindepth 选项。

-maxdepth 和 -mindepth 选项允许您指定您希望 find 搜索深入到目录树的哪一级别。如果您希望 find  只在目录的一个级别中查找,您可以使用 maxdepth 选项。

通过运行以下命令在目录树的前三个级别中查找日志文件,您可以看到 -maxdepth 的效果。使用该选项较之不使用该选项所生成的输出要少得多。

find / -maxdepth 3  -name "*log"

您还可以让 find 在至少下至目录树三个级别的目录中查找:

find / -mindepth 3  -name "*log"

-depth 选项确保先在一个目录中进行查找,然后才在其子目录中进行查找。以下命令提供了一个示例:

find -name "*test*" -depth



./test/test



./test



./localbin/test



./localbin/test_shell_var



./localbin/test.txt



./test2/test/test



./test2/test



./test2

find 世界

我们已经看过了 find 命令的一些更加有用以及有点难懂的功能,但是 find 还可以执行更多的任务。例如,有多个选项可以使 find  与较低的 UNIX 版本和其他操作系统相兼容并允许您执行打印输出到多个文件等操作。阅读本文后,您现在已经有了理解find  参考指南的背景,我鼓励您深入研究这一强大、有用的工具。







find ./ -name "install*"|xargs -t tar cvfz 1.tgz             全部都有,但文件过多就只能打包部分文件
find ./ -name "install*"|xargs -t  tar rvf 1.tar;gzip  1.tar              全部都有,包括文件过多时

tar cvfz 2.tgz `find ./ -name  "install*"`                全部都有,但文件过多就拒绝执行

find ./ -name "install*"|xargs  -t -n 2 tar cvfz 3.tgz            只会有最后2个找到的文件
find ./ -name "install*" -exec  tar cvfz 4.tgz {} \;            只会有最后一个找到的文件
find ./ -name "install*"|xargs  -t -i tar cvfz 5.tgz {}            只会有最后一个找到的文件


find ./ -name "install*" > /tmp/1.txt;tar cvfz 1.tgz -T /tmp/1.txt


查找2014至2015之间的文件

touch -t 201401010000 timestamp1

touch -t 201501010000 timestamp2

find <yourdir> -name <yourfile> -newer timestamp1 ! -newer timestamp2

rm -rf timestamp1

rm -rf timestamp2












本文转自leonardos51CTO博客,原文链接:http://blog.51cto.com/leomars/347592 ,如需转载请自行联系原作者






相关文章
|
2月前
|
Shell Linux
【Shell 命令集合 文件管理】Linux find命令使用教程
【Shell 命令集合 文件管理】Linux find命令使用教程
42 0
|
3月前
|
Linux
认识Linux指令之 “find grep” 命令
认识Linux指令之 “find grep” 命令
认识Linux指令之 “find grep” 命令
|
4月前
|
Linux
【linux】find、xargs、grep 联合查找文件内容
【linux】find、xargs、grep 联合查找文件内容
90 1
|
4月前
|
机器学习/深度学习 Linux
百度搜索:蓝易云【Linux下的find的使用方法】
上述是 `find`命令的一些常用用法和选项,通过灵活组合这些选项,可以实现更复杂的文件查找需求。在实际使用中,可以通过查阅 `find`命令的帮助文档或参考资料,了解更多选项和示例。
28 0
百度搜索:蓝易云【Linux下的find的使用方法】
|
5月前
|
Linux Go 数据安全/隐私保护
linux常用命令手册 用户管理useradd 文件权限管理chmod 搜索管理find grep
linux常用命令手册 用户管理useradd 文件权限管理chmod 搜索管理find grep
66 2
|
7月前
|
Linux
Linux find 文件目录搜索工具
前言 find是一个在Linux系统中非常强大和灵活的文件搜索工具。它用于在文件系统中查找文件和目录,并可以执行各种搜索任务,可帮助系统管理员和用户有效地管理文件系统。
48 0
|
8月前
|
Linux Shell Perl
Linux Find命令详解
Linux Find命令详解
303 1
|
25天前
|
Linux
Linux常用指令-date,cal,histroy,find
Linux常用指令-date,cal,histroy,find
35 0
|
29天前
|
Linux
Linux 指令|date|cal|find|grep|热键
Linux 指令|date|cal|find|grep|热键
|
4月前
|
运维 JavaScript Linux
Linux find
Linux find
17 0