Linux Find 命令精通指南 --未完待续

查找路径/tmp/gaohaiyang及子目录下所有以t开头的文件,列出文件的详细信息:
find /tmp/gaohaiyang  -type f  -name "t*" -exec ls  -l {} /;

注意:-exec ls  -l {} /;的格式,exec选项后面跟随着所要执行的命令,然后是一对{},一个空格和一个/,最后是一个分号。

可以使用下面两个命令来验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。
先执行这条命令:
find /tmp/gaohaiyang  -type f  -name "t*"
观察列出文件的路径,然后执行:
cd /tmp/gaohaiyang
find . -type f  -name "t*"
会发现只输出从当前路径起的相对路径及文件名。


为了在logs目录中查找更改时间在5日以前的文件并删除它们,可以用:
$ find logs -type f -mtime +5 -exec rm {} /;

在下面的例子中,find命令在当前目录中查找所有文件名以.log结尾、更改时间在150日以上的文件,并删除它们,只不过在删除之前先给出提示。
find . -name "*.log" -mtime +150 -ok rm {} /;
按y键删除文件,按n键不删除。

下一个命令将查找由用户、组或二者共同执行的文件(错误输出丢掉):  
find /tmp/gaohaiyang -type f  -perm -ug=rx -exec ls -l {} /; 2>/dev/null
或者:
find /tmp/gaohaiyang -type f  -perm -110 -exec ls -l {} /; 2>/dev/null

 

一:按照owner和权限查找文件和文件夹。

The following command recursively print all file names whose permission  mode  exactly  matches  read, write, and execute
access for user, and read and execute access for  group  and other:
find . -perm u=rwx,g=rx,o=rx
or use this command:find . -perm a=rwx,g-w,o-w
print them with details: find . -perm a=rwx,g-w,o-w -exec ls  -l {} /;

在当前目录及子目录中查找owner是hg06574,权限是644的所有文件(文件的权限必须是644):
find . -user hg06574 -type f -perm 644 -print
在当前目录及子目录中查找owner是hg06574,权限包括644的所有文件(文件的权限必须包括644,如果文件的权限是755,也可以):
find . -user hg06574 -type f -perm -644 -print

在当前目录及子目录中查找owner是hg06574,权限是755的所有文件夹:
find . -user hg06574 -type d -perm 755
在当前目录及子目录中查找owner是hg06574,权限包括755的所有文件夹(文件夹的权限必须包括755,如果文件的权限是775,也可以):
find . -user hg06574 -type d -perm 755

#(case 1)-perm mode (比如: -perm 775)
解释:775 前面没有横线是代表只要找到百分之百一样的权限才算
好比说一个档案是 -rwxrwxr-x (775)就是100%的match
但是另一个档案是 -rwxrwxrwx (777)不是100%的match,others 多了写的权限就不是100%,所以不match!

# (case 2) -perm -mode (例如: -perm -775)
解释:-775 前面有横线表示只要标示的权限match,其他的无所谓,所以上面的第二个档案(777的)在这个case也会match的。

英文资料:http://unixhelp.ed.ac.uk/CGI/man-cgi?find

-perm mode
File's  permission  bits are  exactly mode (octal or symbolic). Since an exact match is required,
if you want to use  this  form for  symbolic  modes,  you  may have to specify a rather complex mode string. 
For example '-perm g=w'  will  only  match  files which  have  mode 0020 (that is, ones for which group write permission is the only permission set). 
It is more likely that you will want to use the '/' or '-' forms, for example '-perm -g=w', which matches any file with group  write permission.

-perm -mode
All  of the permission bits mode are set for the file.  Symbolic modes are accepted in this form,
and this is usually the way  in which  would want to use them.  You must specify 'u', 'g' or 'o' if you use a symbolic mode.  
See the EXAMPLES section for  some illustrative examples.


二:查找文件并改变文件的权限

1:查找当前目录及子目录下owner是hg06574并且权限是644的文件,然后把这些文件的权限改为664
find . -user hg06574 -type f -perm 644 -exec chmod 664 {} /;

查找当前目录及子目录下owner是hg06574并且权限包含644的文件,然后把这些文件的权限改为664
find . -user hg06574 -type f -perm -644 -exec chmod 664 {} /;

查找/tmp/gaohaiyang及子目录下所有以t开头的文件,然后把这些文件的权限改为600
find . -user hg06574 -type f -name "t*" -exec chmod 600 {} /;

2:查找当前目录及子目录下owner是hg06574并且权限是775的文件夹,然后把这些文件夹的权限改为755
find . -user hg06574 -type d -perm 775 -exec chmod 755 {} /;

三:常用的find命令

find . -name “hello” -print 寻找目前目录及所有的子目录内叫hello的文档.
find . -size +2000m -exec ls -l {} /;找出大小超过2000 bytes的文档
find /tmp -user b1234567 -print 在/tmp下属於b1234567的文档
find . -name '*.c' -exec rm {} /;删除所有的.c档
find . -name test/* -print 显示当前目录及其子目录文件名前4位为test的文件
find . -name "test*" -print

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

查找当前目录下所有的以.txt结尾的文件
find . -name "*.txt"
列出详细信息:
find . -name "*.txt" -exec ls -l {} /;
可以统计有多少个文件,
find . -name "*.txt" -exec ls -l {} /;|wc -l

查找当前目录下所有的以.txt结尾的文件,但是跳过当前目录下文件夹testfolder1及路径中包含“testfolder1”的子目录,
find . -name testfolder1 -prune -o -name "*.txt"
上面的命令不会查找当前目录下文件夹testfolder1及路径中包含“testfolder1”的子目录,但是会打印出这些目录的路径。
加上-exec选项,打印出详细信息时,就不会包括当前目录下文件夹testfolder1及路径中包含“testfolder1”的子目录,
find . -name testfolder1 -prune -o -name "*.txt" -exec ls -l {} /;
可以统计有多少个文件,
find . -name testfolder1 -prune -o -name "*.txt" -exec ls -l {} /;|wc -l

使用-depth后,-prune被忽略:
find . -name testfolder1 -prune -o -name "*.txt" -depth
find . -name testfolder1 -prune -o -name "*.txt" -depth -exec ls -l {} /;|wc -l

五:使用-xargs代替-exec

在使用find命令的-exec选项处理匹配到的文件时,find命令将所有匹配到的文件一起传递给exec执行。不幸的是,有些系统对能够传递给exec的命令长度有限制,这样在find命令运行
几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。find命令把匹配到的文件传递给xargs命
令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。在有些系统中,使用-exec选项会
为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;而使用
xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参
数来确定。
让我们来看看xargs命令是如何同find命令一起使用的,并给出一些例子。

下面的例子在/tmp/gaohaiyang/testfolder1目录下查找所有other用户具有读、写和执行权限的文件,并收回相应的写权限
$ find /tmp/gaohaiyang/testfolder1 -perm -7 -print | xargs chmod o-w

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值