现在的位置: 首页 > 综合 > 正文

【Linux命令】–(2)文件查找命令4条

2016年02月01日 ⁄ 综合 ⁄ 共 4677字 ⁄ 字号 评论关闭
文件查找命令
+++++++++++++++++++++++++++++++
which,whereis,locate
find概览,find命令之exec
find命令之xargs,find 命令的参数详解

+++++++++++++++++++++++++++++++

=============which==============

which 可执行文件名称 PATH变量指定的路径中,搜索某个系统命令的位置

参考:http://www.cnblogs.com/peida/archive/2012/11/08/2759805.html
-------------------------------
which pwd
------------------------------

=============whereis==============

whereis 文件名 搜素程序名

参考:http://www.cnblogs.com/peida/archive/2012/11/09/2761928.html
-------------------------------
whereis 如ssh
whereis -m 如svn 查出二进制文件路径
whereis -m 如svn 查出说明文档路径
whereis -s 如svn 找source源文件

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

=============locate==============

locate 搜寻数据库时快速找到档案

参考:http://www.cnblogs.com/peida/archive/2012/11/08/2759805.html
-------------------------------
查找和pwd相关的所有文件
which pwd

搜索etc目录下所有以sh开头的文件 
locate /etc/sh
------------------------------

=============find==============

find pathname -options [-print -exec -ok ...] 目录结构中搜索文件,并执行指定的操作

参考:http://www.cnblogs.com/peida/archive/2012/11/13/2767374.html
-------------------------------
find -atime -2                  找48小时内修改过的文件 
find . -name "*.log"            在当前目录查找 以.log结尾的文件 
find /opt/soft/test/ -perm 777  查找/opt/soft/test/目录下 权限为 777的文件
find . -type f -name "*.log"    查找当目录,以.log结尾的普通文件 
find . -type d | sort           查找当前所有目录并排序
find . -size +1000c -print      查找当前目录大于1K的文件
------------------------------
 

=============find -exec==============
find -exec 执行的shell命令的形式为'command' {} \;,注意{   }和\;之间的空格。
{}   花括号代表前面find查找出来的文件名
参考:http://www.cnblogs.com/peida/archive/2012/11/14/2769248.html
------------------------------
find . -type f -exec ls -l {} \; ls -l             命令放在find命令的-exec选项中 
find . -type f -mtime +14 -exec rm {} \;           在目录中查找更改时间在n日以前的文件并删除它们
find . -name "*.log" -mtime +5 -ok rm {} \;        在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示
find /etc -name "passwd*" -exec grep "root" {} \;  find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户。
find . -name "*.log" -exec mv {} .. \;   查找文件移动到指定目录 
find . -name "*.log" -exec cp {} test3 \;          查找文件复制到指定目录 
-----------------------------

=============find xargs==============
find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部
参考:http://www.cnblogs.com/peida/archive/2012/11/15/2770888.html
-----------------------------
find . -type f -print | xargs file                         查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件 
find / -name "core" -print | xargs echo "" >/tmp/core.log  在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中
find . -perm -7 -print | xargs chmod o-w                   在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限
find . -type f -print | xargs grep "hostname"              用grep命令在所有的普通文件中搜索hostname这个词
find . -name "*.log" | xargs -i mv {} test4                使用xargs执行mv 
-----------------------------

=============find 命令详细==============
参考:http://www.cnblogs.com/peida/archive/2012/11/16/2773289.html

-name选项,按文件名称来查找文件
-perm选项,按文件权限模式来查找文件
-prune选项,指出需要忽略的目录
-user和-nouser选项,按文件属主查找文件
-group和-nogroup选项,按文件所属于的用户组查找文件
-mtime选项,按照更改时间来查找文件
-newer选项,查找比某个文件新或旧的文件
-type选项,按照文件类型来查找文件,! -type 除了某类型之外
-size选项,按照文件长度来查找文件

--------------find -name----------------
find后接要查找的路径例如 .(当前)~(home)/(根)/filepath(具体路径)
-name子参数指定的值是文件名称的正则表达式,用""引起来,例如"*.log"(后缀是log的文件) "[A-z]*[4-9].log"(字母开头数字结尾的后缀是log的文件)
-----------------------------
find ~ -name "*.log" -print     根目录$HOME中查找文件名符合*.log的文件。 
find . -name "[A-Z]*" -print    当前目录及子目录中查找文件名以一个大写字母开头的文件 
find /etc -name "host*" -print  在/etc目录中查找文件名以host开头的文件   
find / -name "*" -print         要想让系统高负荷运行,就从根目录开始查找所有的文件。 

--------------find -perm----------------
文件属性可以读、写、执行,可以用字母和数字表示,还可以用-007这样的形式表示
-----------------------------
find . -perm 755 -print

--------------find -perm----------------
prune意为修剪,同时使用了-depth选项,那么-prune选项就会被find命令忽略
-----------------------------
find test -path "test/test3" -prune -o -print 
find test \( -path test/test4 -o -path test/test3 \) -prune -o -print               避开多个文件夹
find test \(-path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print  查找某一确定文件
----------------------------- 
    -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  
圆括号表示表达式的结合。  \ 表示引用,即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。  

--------------find -user----------------
find ~ -user peida -print  

--------------find -nouser----------------
find ~ -nouser -print  

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

--------------find -newer----------------
find -newer log2012.log ! -newer log2017.log  查找更改时间比文件log2012.log新但比文件log2017.log旧的文件

--------------find -size----------------
文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计量文件长度的表达形式为N c;以块计量文件长度只用数字表示即可。  
在按照文件长度查找文件时,一般使用这种以字节表示的文件长度,在查看文件系统的大小,因为这时使用块来计量更容易转换。  
--------------------------------
find . -size +1000000c -print  当前目录下查找文件长度大于1 M字节的文件 

-print  

抱歉!评论已关闭.