shell命令find例子全解析

本文详细解析了shell命令find的各种用法,包括删除指定文件、处理特殊字符、执行命令、查找具有特定权限的文件、根据时间查找、处理不可读文件、过滤版本控制系统目录等,提供多个实例帮助理解find的复杂操作。
摘要由CSDN通过智能技术生成

前言:本文的例子均来源于man手册第一章节的find,man 1 find查看

e.g.01 find /tmp -name core -type f -print | xargs /bin/rm -f

手册原文:

find /tmp -name core -type f -print | xargs /bin/rm -f

Find files named core in or below the directory /tmp and delete them.  Note that this will work incorrectly if there are any filenames containing newlines, single or double quotes, or spaces.

效果:
删除/tmp路径下面名字为core的普通文件,但是文件不能够有换行符、单引号、双引号和空格符,否则会出错。

e.g.02 find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

手册原文:

find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

Find files named core in or below the directory /tmp and delete them, 
processing filenames in such a way that file or directory names containing single or double quotes, 
spaces or newlines are correctly handled.  
The -name test comes before the -type test in order to avoid having to call stat(2) on every file.

效果:
删除/tmp路径下面名字为core的普通文件,和上面的区别就是文件可以有换行符、单引号、双引号和空格符,因为用字符\0隔开了。

e.g.03 find . -type f -exec file ‘{}’ ;

手册原文:

find . -type f -exec file '{}' \;

Runs `file' on every file in or below the current directory.
Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation.  
The semicolon is similarly protected by the use of a backslash, though single quotes could have been used in that case also.

效果:
对当前目录下每个文件执行file命令。这里的{}在执行命令的时候会被替换成找到的文件名,这里的;是作为exec选项的参数,不然exec命令就不完整了。(注意: ‘{}’ ;加单引号和转移符号都是为了避免shell错误解释)

e.g.04 find / ( -perm -4000 -fprintf /root/suid.txt ‘%#m %u %p\n’ ) , ( -size +100M -fprintf /root/big.txt ‘%-10s %p\n’ )

手册原文:

find / \( -perm -4000 -fprintf /root/suid.txt '%#m %u %p\n' \) , \
 \( -size +100M -fprintf /root/big.txt '%-10s %p\n' \)

Traverse the filesystem just once, 
listing setuid files and directories into /root/suid.txt and large files into /root/big.txt.

效果:
递归查找根目录下具有SUID(Set User ID)权限的文件,并将结果输出到/root/suid.txt文件中;大于100M的文件,将结果输出到/root/big.txt文件中。然后前者的输出格式是这个%#m %u %p\n,后者输出的格式%-10s %p\n是这个。第一行末尾 \只是一行的连接符,别看错了。

e.g.05 find $HOME -mtime 0

手册原文:

find $HOME -mtime 0

Search for files in your home directory which have been modified in the last twenty-four hours.  
This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded.  
That means that to match -mtime 0, a file will have to have a modification in the past
  • 24
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值