【每天一个Linux命令】15. 搜索文件命令find
来源:互联网 发布:德州金蝶软件 编辑:程序博客网 时间:2024/06/06 04:12
命令用途
find命令用于:在一个目录(及子目录)中搜索文件,你可以指定一些匹配条件,如按文件名、文件类型、用户甚至是时间戳查找文件。
命令实例
0. 帮助命令:
bixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ find -help用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]默认路径为当前目录;默认表达式为 -print表达式可能由下列成份组成:操作符、选项、测试表达式以及动作:操作符 (优先级递减;未做任何指定时默认使用 -and):( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2位置选项 (总是真): -daystart -follow -regextype普通选项 (总是真,在其它表达式前指定):-depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf--version -xdev -ignore_readdir_race -noignore_readdir_race测试(N可以是 +N 或-N 或 N):-amin N -anewer FILE -atime N -cmin -cnewer 文件 -ctime N -empty -false -fstype 类型 -gid N -group 名称-ilname 匹配模式 -iname 匹配模式 -inum N -ipath 匹配模式 -iregex 匹配模式-links N -lname 匹配模式 -mmin N -mtime N -name 匹配模式 -newer 文件-nouser -nogroup -path 匹配模式 -perm [+-]访问模式 -regex 匹配模式-readable -writable -executable-wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N-used N -user NAME -xtype [bcdpfls]动作: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit-exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;-execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;
1. 按照文件名查找文件 -name
说明:find /dir -name filename 在/dir目录及其子目录下面查找名字为filename的文件
find . -name "*.c" 在当前目录及其子目录(用“.”表示)中查找任何扩展名为“c”的文件
例子:
#查找当前目录下所有的sed.txt文件
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ find . -name sed.txt./sed.txt
2. 按照文件权限来查找文件 -perm
说明:find . -perm 755 –print 在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件
#例子:查找当前目录下文件权限为755的文件
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find . perm 755 -print../dir1./dir1/cpf1.txt./dir1/cpf12.txt
3.不在当前指定的目录中查找 -prune
注意:如果同时使用-depth选项,那么-prune将被find命令忽略说明:
find /wirelessqa -path "/wirelessqa/bxp" -prune -o –print 在/apps目录下查找文件,但不希望在/apps/bin目录下查找
#例子:在当前目录下查找文件,将dir3排除在查找范围之外
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -altotal 0drwxr-xr-x 8 bixiaopeng staff 272 Sep 18 17:00 .drwxr-xr-x@ 7 bixiaopeng staff 238 Sep 18 15:43 ..drwxr-xr-x 7 bixiaopeng staff 238 Sep 18 17:27 dir1drwxr-xr-x 5 bixiaopeng staff 170 Sep 18 16:50 dir11drwxr-xr-x 7 bixiaopeng staff 238 Sep 18 17:31 dir2drwxr-xr-x 5 bixiaopeng staff 170 Sep 18 16:53 dir3-rw-r--r-- 1 bixiaopeng staff 0 Sep 18 16:15 f2.txt-rw-r--r-- 1 bixiaopeng staff 0 Sep 18 17:00 f3.txtbixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find . -path "./dir3" -prune -o -print../dir1./dir1/cpf1.txt./dir1/cpf12.txt./dir1/ddddir./dir1/f1.txt./dir1/f2.txt./dir11./dir11/cpf1.txt./dir11/cpf12.txt./dir11/f1.txt./dir2./dir2/cpf1.txt./dir2/cpf12.txt./dir2/ddddir./dir2/f1.txt./dir2/f2.txt./f2.txt./f3.txt
4. 按照文件属主来查找文件 -user
find ~ -user bixiaopeng –print 在$HOME目录中查找文件属主为bixiaopeng的文件例子:#在$HOME目录中查找文件属主为bixiaopeng的文件
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find ~ -user bixiaopeng -print /Users/bixiaopeng/source-code/workshop/Wireless-Test-Workshop/wtc/web/report/src/main/webapp/WEB-INF/web.xml/Users/bixiaopeng/testfun1.sh/Users/bixiaopeng/testfun2.sh/Users/bixiaopeng/testfunc.sh/Users/bixiaopeng/testgetopts.sh…...
5. 按照文件所属的组来查找文件 -group
说明:find /apps -group gem –print 在/apps目录下查找属于gem用户组的文件
#例子:在当前目录下查找属于staff用户组的文件
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find . -group staff -print../dir1./dir1/cpf1.txt./dir1/cpf12.txt./dir1/ddddir./dir1/f1.txt….
6. 按照文件的更改时间来查找文件 -mtime -n +n
注意:- n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前说明:
find / -mtime -5 –print 在系统根目录下查找更改时间在5日以内的文件
find /var/adm -mtime +3 –print 在/var/adm目录下查找更改时间在3日以前的文件
#例子:查找当前目录下更改时间在1日以内的文件
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find . -mtime -1 -print../dir1./dir1/cpf1.txt./dir1/cpf12.txt./dir1/ddddir./dir1/f1.txt./dir1/f2.txt./dir11...
7. 查找无有效所属组的文件
即该文件所属的组在/etc/groups中不存在 -nogroup说明:find / –nogroup -print
8. 查找无有效属主的文件
即该文件的属主在/etc/passwd中不存在 -nouser说明:find /home -nouser –print
9. 查找更改时间比文件file1新但比文件file2旧的文件 -newer file1 ! file2
说明: find -newer file1 !file210. 查找某一类型的文件 -type
说明:类型文件:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
find /etc -type d –print 在/etc目录下查找所有的目录
find . ! -type d –print 在当前目录下查找除目录以外的所有类型的文件
find /etc -type l –print 在/etc目录下查找所有的符号链接文件
#例子:在当前目录下查找所有的目录
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find . -type d -print../dir1./dir1/ddddir./dir11./dir2./dir2/ddddir./dir3./dir3/dir1#例子:在当前目录下查找除目录以外的所有文件
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find . ! -type d -print./dir1/cpf1.txt./dir1/cpf12.txt./dir1/f1.txt./dir1/f2.txt./dir11/cpf1.txt./dir11/cpf12.txt./dir11/f1.txt./dir2/cpf1.txt./dir2/cpf12.txt./dir2/f1.txt./dir2/f2.txt./dir3/cpf1.txt./dir3/cpf12.txt./dir3/dir1/cpf1.txt./dir3/dir1/cpf12.txt./dir3/dir1/f1.txt./f2.txt./f3.txt#例子:在当前目录下查找其它文件类型
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find . -type l -printbixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find . -type c -printbixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find . -type p -printbixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find . -type f -print./dir1/cpf1.txt./dir1/cpf12.txt./dir1/f1.txt./dir1/f2.txt./dir11/cpf1.txt./dir11/cpf12.txt./dir11/f1.txt./dir2/cpf1.txt./dir2/cpf12.txt./dir2/f1.txt./dir2/f2.txt./dir3/cpf1.txt./dir3/cpf12.txt./dir3/dir1/cpf1.txt./dir3/dir1/cpf12.txt./dir3/dir1/f1.txt./f2.txt./f3.txt
11. 以文件大小来查找 -size n
-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计说明:
find . -size +1000000c –print 在当前目录下查找文件长度大于1 M字节的文件
find . -size 100c –print 在当前目录下查找文件长度恰好为100字节的文件
find . -size +10 –print 在当前目录下查找长度超过10块的文件(一块等于512字节)
#例子:查找当前目录下为300字节的文件
bixiaopeng@bixiaopengtekiMacBook-Pro Projects$ find . size 300c -print ./workspaces/婵???????/.settings./workspaces/婵???????/.settings/org.eclipse.wst.server.core.prefs./workspaces/婵???????/Tomcat v7.0 Server @ localhost-config./workspaces/婵???????/Tomcat v7.0 Server @ localhost-config/catalina.policy./workspaces/婵???????/Tomcat v7.0 Server @ localhost-config/catalina.properties./workspaces/婵???????/Tomcat v7.0 Server @ localhost-config/context.xml./workspaces/婵???????/Tomcat v7.0 Server @ localhost-config/server.xml….
12. 查找文件时首先从当前目录查找,然后再查找子目录
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找说明:
find / -name "bixiaopeng" -depth –print 它将首先匹配所有的文件然后再进入子目录中查找
13. 在查找文件时不跨越文件系统mount点 -mount
说明:find . -name "*.sh" -mount –print 从当前目录开始查找位于本文件系统中文件名以sh结尾的文件(不进入其他文件系统)
#例子:在当前目录查找位于本文件系统以.zip结尾的文件
bixiaopeng@bixiaopengtekiMacBook-Pro Projects$ find . -name "*.zip" -mount -print./workspaces/.metadata/.mylyn/.tasks.xml.zip./workspaces/.metadata/.mylyn/repositories.xml.zip./workspaces/.metadata/.mylyn/tasks.xml.zip./workspaces/Android Context璇﹁В蹇?????????????.zip./workspaces/WirelessQa-EditText.zip./workspaces/WirelessQA-Location.zip./workspaces/WirelessQA-Provider.zip
14. 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件 -follow
15.在当前目录及所有子目录中查找filename(忽略大小写)
# find -iname "filename"16.在根目录分级查找
查找根目录和根目录的和只展开一级的子目录中查找$find -maxdepth 2 -name passwd
在根目录和根目录下展开两级查找passwd文件
$find / -maxdepth 3 -name passwd
在根目录的第二级和第四级之间查找
$ find -mindepth 3 -maxdepth 5 -name passwd
find与exec和ok一起使用
当匹配到一些文件以后,可能希望对其进行某些操作,这时就可以使用-exec选项,一旦find命令匹配到了相应的文件,就可以用-exec选项中的命令对其进行操作.格式: -exec command {} \;
exec选项后面跟随着所要执行的命令,然后是一对儿{},一个空格和一个\,最后是一个分号
#例子:在当前目录下查找文件,并使用exec执行ls -l命令
bixiaopeng@bixiaopengtekiMacBook-Pro androidshell$ find . -type f -exec ls -l {} \;-rw-r--r-- 1 bixiaopeng staff 24576 Jul 3 10:03 ./.1.log.swp-rw-r--r--@ 1 bixiaopeng staff 6148 Sep 18 15:43 ./.DS_Store-rw-r--r-- 1 bixiaopeng staff 683295 Jun 5 10:39 ./1.log-rw-r--r-- 1 bixiaopeng staff 171 Jun 5 10:32 ./logcat.sh-rw-r--r-- 1 bixiaopeng staff 120 Jun 4 17:34 ./monkey.sh
#例子:查找并删除dir1目录下更改时间小于一天的文件
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find dir1 -type f -mtime -1 -exec rm {} \;bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls dir1 -alls: -al: No such file or directorydir1:ddddirbixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir1total 0drwxr-xr-x 3 bixiaopeng staff 102 Sep 19 13:30 .drwxr-xr-x 8 bixiaopeng staff 272 Sep 18 17:00 ..drwxr-xr-x 2 bixiaopeng staff 68 Sep 18 17:27 ddddir#例子:查找并删除dir1目录下更改时间小于一天的文件,删除时提示是否删除 y 删除 n不删除
# 使用-okbixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find dir2 -name "*.txt" -mtime -1 -ok rm {} \;"rm dir2/cpf1.txt"? y"rm dir2/cpf12.txt"? y"rm dir2/f1.txt"? y"rm dir2/f2.txt"? n#使用-execbixiaopeng@bixiaopengtekiMacBook-Pro testshell$ find dir2 -name "*.txt" -mtime -1 -exec rm -i {} \;remove dir2/f2.txt? n
find与xargs一起使用
find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。#查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件:find . -type f -print | xargs file
bixiaopeng@bixiaopengtekiMacBook-Pro androidshell$ find . -type f -print |xargs file./.1.log.swp: Vim swap file, version 7.3./.DS_Store: data./1.log: UTF-8 Unicode text, with very long lines, with CRLF, CR line terminators./logcat.sh: Bourne-Again shell script text executable./monkey.sh: Bourne-Again shell script text executable
#在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:
find / -name "core" -print | xargs echo "" >/tmp/core.log
#用grep命令在所有的普通文件中搜索wirelessqa这个词
find . -type f -print | xargs grep "wirelessqa"#在/bixiaopeng/apps目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限:
$ find /bixiaopeng/apps -perm -7 -print | xargs chmod o -w#在当前目录下的所有普通文件中搜索ILOVEU这个词
$ find . -name *\ -type f -print | xargs grep "ILOVEU"#删除3天以前的所有东西 (find . -ctime +3 -exec rm -rf {} \;)
find ./ -mtime +3 -print|xargs rm -f –r#删除文件大小为零的文件
find . -size 0 | xargs rm -f &
本文参考众多网上资料,感谢乐于分享的人们。
- 【每天一个Linux命令】15. 搜索文件命令find
- 每天一个linux命令-find
- 每天一个linux命令--find
- 每天一个Linux命令-find
- 每天一个linux命令:find
- 每天一个linux命令:find命令
- 每天一个linux命令:find 命令概览
- Linux搜索文件命令find
- Linux 文件搜索命令 find
- Linux文件搜索find命令
- Linux搜索文件命令find
- linux 文件搜索命令 find
- 每天一个 linux 命令(19):文件查找命令 find 命令---学习笔记
- 每天一个 linux 命令(20):文件查找命令 find命令之exec---学习笔记
- 每天一个linux命令(21):文件查找命令 find命令之xargs
- 每天一个linux命令(22):文件查找命令 find 命令的参数详解---学习笔记
- 每天一个Linux命令(2)- find
- 每天一个linux命令——find
- Hibernate4新特性
- #pragma pack() effect
- 随机数生成问题
- JS实现投票
- VS2010环境下MFC,剪贴板“复制”程序。
- 【每天一个Linux命令】15. 搜索文件命令find
- 利用JS显示与隐藏层
- Letter Combinations of a Phone Number (电话号码字母组合) 【leetcode】
- 【MySQL数据库开发之一】Mac下配置安装数据库-MySQL
- C++中虚函数功能的实现机制
- 用!important解决IE和Mozilla的布局差别
- MD5算法原理
- Linux中的内存管理模型
- 黑马程序员------IO流(No.4)(打印流、序列流、管道流、RandomAccessFile、常用数据类型的操作流)