shell day day up --使用find和xargs

《本文整理子网络某书籍》

Find命令

find命令可以便利当前目录甚至整个文件系统来查找某些文件或则目录

find pathname -options [ -print -exec -ok]pathname

find命令所查找的目录路径。如.表示当前目录,/表示系统根目录

-print find命令将匹配的文件输出到标准输出

-exec find命令对匹配的文件执行该参数所给出的shell命令。相应的命令形式为'comm-and' {} \;,注意{}和\;之间空格

-ok 和-exec一样。不过更安全、没执行一个命令前都会询问用户

find的命令选项

-name 按照文件名查找文件

-perm 按照文件权限查找文件

-prune 可以使find命令不在当前的目录中查找,如果同时使用了

-depth则该选项会被忽略-user 按照文件属主来查找文件

-group 按文件所属的组来查找文件

-mtime -n +n 按照更改时间来查找文件 -+n表示距今天数 -+atime和mtime相似

-nogroup 查找无效所属组的文件,即该文件所属不在/etc/groups中不存在

-nouser 查找无效属主文件,即该文件不在/etc/passwd中不存在

-newer file1 !file2 查找更新时间比文件file1新而比file2旧的文件

-type 查找某一类型的文件如:

b 块文件、d目录、c字符设备文件、p管道文件、l符号链接文件、f普通文件

-size n[c]查找文件长度为n块得文件,带有c表示文件长度以字节

-depth 查找文件时,首先查找当前目录,然后再在其子目录中查找

-fstype 查找位于某一类型稳文件系统中的文件,这些文件系统通常配置在/etc/fstab 中找到

-mount 在查找文件上时不跨越系统mount点-follow 如果find命令遇到符号连接符,就跟踪过去

-cpio 对匹配文件使用cpio命令,将这些文件备份到磁带设备中取

使用name选项

~ 表示当前用户$HOME目录
.表示当前目录

可以使用正则

使用perm选项

查找当前目录下权限为755的文件

如果需要全部用户的 则find . -perm -755 -print

忽略某个目录

命令格式如:find /apps -name "/apps/bin" -prune -o -print

使用-prune忽略/apps/bin目录

其他选项示例

find ~ -user dave -print

find /home -nouser -print

find /apps -group -accts -print

find / -nogroup -print

find / -mtime -5 -print

find . -newer age.awk ! -newer belts.awk -exec ls -l {} \;

---touch -t 05042140 dstamp

ls -l dstamp --查找21.40以来文件

find /etc -type d -print

find . ! -type d -print

find . -size +1000000c -print

find . -size +10 -print

find / -name "CON.FILE" -depth -print

find . -name "*.XC" -mount -print

find -type f -exec ls -l {} \;

find logs -type f -mtime +5 -exec rm {} \;

find etc home apps -depth -print | cpio -ivcdC65536 =0 \ /dev/rmt0

xargs

xargs 优化和扩张-exec功能。

find / -type f -print |xargs file /etc/protocols:English txt /etc/securetty:ASCII text

上例表示查找文件系统中每一个普通文件,然后使用xargs命令测试他们分别属于哪类文件

find . -name "core" -print | xargs echo "" > /tmp/core.log

上例查找内存信息存储文件,然后把结果保存到/tmp/core.log

find /apps/audit -perm -7 -print | xargs chmod o-w

查找所有有读写权执行的文件并回收相应写权限

find / -type f -print | xargs grep "device"

用grep在所有普通文件中查找“device”

find . -name *\-type f | xargs grep "DBO"

\用来取消*在shell中特殊含义


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值