linux find 之 执行


/*
 * 排序
 */
    find . -type d | sort

/*
 * 使用exec或ok来执行shell命令
 */
    -exec [other command] {} \;
    {} : 匹配到的每一个文件,此处花括号作为占位符
    \; : ; -exec 选项结束标记, \ 转义;

    -ok 与 -exec 不同之处在于会提示需要用户是否要执行此操作


    find . -type f -exec ls -l {} \;                    # 执行 ls -l
    
    find logs -type f -mtime +5 -exec rm {  } \;        # 执行 rm

    find . -name "*.conf"  -mtime +5 -ok rm {  } \;     # 操作之前提示,    按y键删除文件,按n键不删除。

    find /etc -name "passwd*" -exec grep "sam" {  } \;  # 执行 grep

    find . -type f -perm 644 -exec ls -l {  } \;
    find . -type f -size 0 -exec ls -l {  } \;
    find /var/logs -type f -mtime +7 -ok rm {  } \;
    find . -name "admin.log[0-9][0-9][0-9]" -atime -7  -ok rm {  } \;           # admin.log042

    find . -type f -user root -exec chow ac {} \;
    find . -type f -name "*.txt" -exec cat {} \;> /all.txt
    find . -type f -mtime +30 -name "*.log" -exec mv {} old \;
    find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;

    -exec ./text.sh {} \;               # 脚本文件,可执行多个命令


/*
 * xargs
 */
    find . -type f -print | xargs file
    find . -name "core" -print | xargs echo "" >/tmp/core.log
    find . -perm -7 -print | xargs chmod o-w
    find . -type f -print | xargs grep "hostname"

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值