理论详解:

命令格式:find path -option [ -print ] [ -exec/-ok command ] {} /;

#-print 将查找到的文件输出到标准输出
#-exec command {} /;     -----将查到的文件执行command操作,{} 和 /;之间有空格
#-ok 和-exec相同,只不过在操作前要询用户

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

-name filename             #查找名为filename的文件
-perm                       #按执行权限来查找
-user   username            #按文件属主来查找
-group groupname            #按组来查找
-mtime -n +n               #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime   -n +n              #按文件访问时间来查
-perm                        #按执行权限来查找
-user   username            #按文件属主来查找
-group groupname            #按组来查找
-mtime -n +n               #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime   -n +n              #按文件访问时间来查找文件,-n指n天以内,+n指n天以前
-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                      #忽略某个目录

实践命令:

[root@uyhd000225 logs]# find . -mtime +3 -exec ls -l {} \;
/usr/lib/.khostd/find: 遗漏“-exec”的参数
[root@uyhd000225 logs]# find . -mtime +3 -exec ls -l '{} \;'
-rw-r--r-- 1 root root 377909190 09-22 23:56 ./normal.20130922
-rw-r--r-- 1 root root 1028096 2013-02-18 ./.dbLog.20120801.swp
-rw-r--r-- 1 root root 972704729 09-24 23:59 ./normal.20130924
-rw-r--r-- 1 root root 1463026812 09-23 23:55 ./normal.20130923

=================================================
在/ l o g s目录中查找更改时间在5日以前的文件并删除它们:
$ find logs -type f -mtime +5 -exec rm {} /;


=================================================
查询当天修改过的文件
[root@book class]# find ./ -mtime -1 -type f -exec ls -l {} /;


=================================================
查询文件并询问是否要显示
[root@book class]# find ./ -mtime -1 -type f -ok ls -l {} /;
< ls ... ./classDB.inc.php > ? y
-rw-r--r--    1 cnscn    cnscn       13709 1月 12 12:22 ./classDB.inc.php
[root@book class]# find ./ -mtime -1 -type f -ok ls -l {} /;
< ls ... ./classDB.inc.php > ? n

linux下使用find xargs grep查找文件及文件内容

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

[root@uyhd000225 bin]# find /etc -name "passwd*" -exec grep "root" '{} \;'
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

xasrgs用法

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

[root@uyhd000225 Desktop]# cat>wenchao
wenchao test
[root@uyhd000225 Desktop]# more wenchao
wenchao test
[root@uyhd000225 Desktop]# find .-name wenchao -print | xargs echo " " >/tmp/core.log
[root@uyhd000225 Desktop]# cd -
/tmp
[root@uyhd000225 tmp]# more core.log
  wenchao
[root@uyhd000225 Desktop]# find .-name wenchao -print | xargs >./core.log
[root@uyhd000225 Desktop]# more core.log
wenchao
[root@uyhd000225 Desktop]# more wenchao
wenchao test
[root@uyhd000225 Desktop]# find .-name wenchao -print | xargs >./core.log
[root@uyhd000225 Desktop]# more core.log
wenchao
[root@uyhd000225 Desktop]# find .-name wenchao -print | xargs cat>./core.log
[root@uyhd000225 Desktop]# more core.log
wenchao test
普通文件中包含wenchao的词
[root@uyhd000225 Desktop]# find . -type f -print|xargs grep wenchao
./core.log:wenchao test
./wenchao:wenchao test

[root@uyhd000225 Desktop]# cat>wenchao
wenchao test
[root@uyhd000225 Desktop]# more wenchao
wenchao test
[root@uyhd000225 Desktop]# find .-name wenchao -print | xargs echo " " >/tmp/core.log
[root@uyhd000225 Desktop]# cd -
/tmp
[root@uyhd000225 tmp]# more core.log
  wenchao
[root@uyhd000225 Desktop]# find .-name wenchao -print | xargs >./core.log
[root@uyhd000225 Desktop]# more core.log
wenchao
[root@uyhd000225 Desktop]# more wenchao
wenchao test
[root@uyhd000225 Desktop]# find .-name wenchao -print | xargs >./core.log
[root@uyhd000225 Desktop]# more core.log
wenchao
[root@uyhd000225 Desktop]# find .-name wenchao -print | xargs cat>./core.log
[root@uyhd000225 Desktop]# more core.log
wenchao test
#普通文件中包含wenchao的词
[root@uyhd000225 Desktop]# find . -type f -print|xargs grep wenchao
./core.log:wenchao test
./wenchao:wenchao test
#文件类型查看
[root@uyhd000225 ms]# find . -type f -print|xargs file
./-20131120.doc: Microsoft Office Document

查找上一次状态改变在一天前的文件,并删除文件。 find /jhfapp/app/logs -mtime +1  -type f  |xargs -t -i rm -rf {}

查找上一次状态改变在一天前的文件,并清空文件内容。  find /jhfapp/app/logs -mtime -1  -type f  |xargs -i sh -c “> {}”


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

1)在/tmp中查找所有的*.h,并在这些文件中查找“SYSCALL_VECTOR",最后打印出所有包含"SYSCALL_VECTOR"的文件名

A) find /tmp -name "*.h" | xargs -n50 grep SYSCALL_VECTOR
B) grep SYSCALL_VECTOR /tmp/*.h | cut   -d':' -f1| uniq > filename
C) find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} /; -print


2)find / -name filename -exec rm -rf {} /;
find / -name filename -ok rm -rf {} /;


3)比如要查找磁盘中大于3M的文件:
find . -size +3000k -exec ls -ld {} ;


4)将find出来的东西拷到另一个地方
find *.c -exec cp '{}' /tmp ';'

如果有特殊文件,可以用cpio,也可以用这样的语法:
find dir -name filename -print | cpio -pdv newdir


6)查找2004-11-30 16:36:37时更改过的文件
# A=`find ./ -name "*php"` | ls -l --full-time $A 2>/dev/null | grep "2004-11-30 16:36:37"

[root@uyhd000225 testDir]# B=`find ./ -type f` | ls -l --full-time $B 2>/dev/null|grep "2013-11-19"
-rw-r--r-- 1 root root 220702 2013-11-19 16:09:40.000000000 +0800 mscfqk.zip
[root@uyhd000225 testDir]# ll
总计 244
-rw-r--r-- 1 root root     13 11-21 11:07 core.log
-rw-r--r-- 1 root root 220702 11-19 16:09 mscfqk.zip
-rw-r--r-- 1 root root   8229 10-11 00:51 TestSocketOfRecAndSend.java
-rw-r--r-- 1 root root    901 08-19 15:16 TracingScript.java
-rw-r--r-- 1 root root     13 11-21 11:03 wenchao




1. 用文件名查找文件

这是find命令的一个基本用法。下面的例子展示了用MyCProgram.c作为查找名在当前目录及其子目录中查找文件的方法。

1
2
3
# find -name "MyCProgram.c"
. /backup/MyCProgram .c
. /MyCProgram .c

2.用文件名查找文件,忽略大小写

这是find命令的一个基本用法。下面的例子展示了用MyCProgram.c作为查找名在当前目录及其子目录中查找文件的方法,忽略了大小写。

1
2
3
4
5
# find -iname "MyCProgram.c"
. /mycprogram .c
. /backup/mycprogram .c
. /backup/MyCProgram .c
. /MyCProgram .c


3. 使用mindepth和maxdepth限定搜索指定目录的深度

在root目录及其子目录下查找passwd文件。

1
2
3
4
5
# find / -name passwd
. /usr/share/doc/nss_ldap-253/pam .d /passwd
. /usr/bin/passwd
. /etc/pam .d /passwd
. /etc/passwd

在root目录及其1层深的子目录中查找passwd. (例如root — level 1, and one sub-directory — level 2)

1
2
# find -maxdepth 2 -name passwd
. /etc/passwd

在root目录下及其最大两层深度的子目录中查找passwd文件. (例如 root — level 1, and two sub-directories — level 2 and 3 )

1
2
3
4
# find / -maxdepth 3 -name passwd
. /usr/bin/passwd
. /etc/pam .d /passwd
. /etc/passwd

在第二层子目录和第四层子目录之间查找passwd文件。

1
2
3
# find -mindepth 3 -maxdepth 5 -name passwd
. /usr/bin/passwd
. /etc/pam .d /passwd

4. 在find命令查找到的文件上执行命令

下面的例子展示了find命令来计算所有不区分大小写的文件名为“MyCProgram.c”的文件的MD5验证和。{}将会被当前文件名取代。

1
2
3
4
5
find -iname "MyCProgram.c" - exec md5sum {} \;
d41d8cd98f00b204e9800998ecf8427e  . /mycprogram .c
d41d8cd98f00b204e9800998ecf8427e  . /backup/mycprogram .c
d41d8cd98f00b204e9800998ecf8427e  . /backup/MyCProgram .c
d41d8cd98f00b204e9800998ecf8427e  . /MyCProgram .c

5. 相反匹配

显示所有的名字不是MyCProgram.c的文件或者目录。由于maxdepth是1,所以只会显示当前目录下的文件和目录。

1
2
3
4
5
6
find -maxdepth 1 -not -iname "MyCProgram.c"
.
. /MybashProgram .sh
. /create_sample_files .sh
. /backup
. /Program .c

6. 使用inode编号查找文件

任何一个文件都有一个独一无二的inode编号,借此我们可以区分文件。创建两个名字相似的文件,例如一个有空格结尾,一个没有。

1
2
3
4
5
6
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

从ls的输出不能区分哪个文件有空格结尾。使用选项-i,可以看到文件的inode编号,借此可以区分这两个文件。

1
2
3
ls -i1 test *
16187429 test - file -name
16187430 test - file -name

你可以如下面所示在find命令中指定inode编号。在此,find命令用inode编号重命名了一个文件。

1
2
3
4
5
find -inum 16187430 - exec mv {} new- test - file -name \;
# ls -i1 *test*
16187430 new- test - file -name
16187429 test - file -name

你可以在你想对那些像上面一样的糟糕命名的文件做某些操作时使用这一技术。例如,名为file?.txt的文件名字中有一个特殊字符。若你想执行“rm file?.txt”,下面所示的所有三个文件都会被删除。所以,采用下面的步骤来删除”file?.txt”文件。

1
2
ls
file1.txt  file2.txt   file ?.txt

找到每一个文件的inode编号。

1
2
3
4
ls -i1
804178 file1.txt
804179 file2.txt
804180 file ?.txt

如下所示:?使用inode编号来删除那些具有特殊符号的文件名。

1
2
3
4
find -inum 804180 - exec rm {} \;
# ls
file1.txt  file2.txt
[Note: The file with name "file?.txt" is now removed]

7. 根据文件权限查找文件

下面的操作时合理的:

  • 找到具有指定权限的文件

  • 忽略其他权限位,检查是否和指定权限匹配

  • 根据给定的八进制/符号表达的权限搜索

此例中,假设目录包含以下文件。注意这些文件的权限不同。

1
2
3
4
5
6
7
8
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

找到具有组读权限的文件。使用下面的命令来找到当前目录下对同组用户具有读权限的文件,忽略该文件的其他权限。

1
2
3
4
5
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

找到对组用户具有只读权限的文件。

1
2
find . -perm g=r - type f - exec ls -l {} \;
----r----- 1 root root 0 2009-02-19 20:27 . /others_can_only_read

找到对组用户具有只读权限的文件(使用八进制权限形式)。

1
2
find . -perm 040 - type f - exec ls -l {} \;
----r----- 1 root root 0 2009-02-19 20:27 . /others_can_only_read

8. 找到home目录及子目录下所有的空文件(0字节文件)

下面命令的输出文件绝大多数都是锁定文件盒其他程序创建的place hoders

1
find ~ -empty

只列出你home目录里的空文件。

1
find . -maxdepth 1 -empty

只列出当年目录下的非隐藏空文件。

1
find . -maxdepth 1 -empty -not -name ".*"

9. 查找5个最大的文件

下面的命令列出当前目录及子目录下的5个最大的文件。这会需要一点时间,取决于命令需要处理的文件数量。

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

10. 查找5个最小的文件

方法同查找5个最大的文件类似,区别只是sort的顺序是降序。

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

上面的命令中,很可能你看到的只是空文件(0字节文件)。如此,你可以使用下面的命令列出最小的文件,而不是0字节文件。

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

11. 使用-type查找指定文件类型的文件

只查找socket文件

1
find . - type s

查找所有的目录

1
find . - type d

查找所有的一般文件

1
find . - type f

查找所有的隐藏文件

1
find . - type f -name ".*"

查找所有的隐藏目录

1
find - type d -name ".*"

12. 通过和其他文件比较修改时间查找文件

显示在指定文件之后做出修改的文件。下面的find命令将显示所有的在ordinary_file之后创建修改的文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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. 通过文件大小查找文件

使用-size选项可以通过文件大小查找文件。

查找比指定文件大的文件

1
find ~ -size +100M

查找比指定文件小的文件

1
find ~ -size -100M

查找符合给定大小的文件

1
find ~ -size 100M

注意: – 指比给定尺寸小,+ 指比给定尺寸大。没有符号代表和给定尺寸完全一样大。

14. 给常用find操作取别名

若你发现有些东西很有用,你可以给他取别名。并且在任何你希望的地方执行。

常用的删除a.out文件。

1
2
alias rmao= "find . -iname a.out -exec rm {} \;"
# rmao

删除c程序产生的core文件。

1
2
alias rmc= "find . -iname core -exec rm {} \;"
# rmc

15. 用find命令删除大型打包文件

下面的命令删除大于100M的*.zip文件。

1
find / - type f -name *.zip -size +100M - exec rm -i {} \;"

用别名rm100m删除所有大雨100M的*.tar文件。使用同样的思想可以创建rm1g,rm2g,rm5g的一类别名来删除所有大于1G,2G,5G的文件。


1
2
3
4
5
6
7
8
9
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