linux shell 编程笔记 - 常用的find和xargs

find命令工具用于在目录下(甚至是整个文件系统),遍历地查找文件;
find path_name -option [-print -exec -ok]    #find的一般格式;
1、find命令的选项(-option)
1.1、根据名称查找文件和目录(-name):
    cb@Standalone14:~/Documents$ find . -name "cb*" -print    #在当前目录下,查找cb开头的文件及目录; 
1.2、根据文件修改时间查找文件和目录(-mtime):
    cb@Standalone14:~/Documents$ find . -mtime -2 -print    #在当前目录下,查找2天内修改的文件及目录;
    cb@Standalone14:~/Documents$ find . -mtime +5 -print    #在当前目录下,查找5天以前修改的文件及目录;
1.3、根据文件类型查找文件或目录(-type):
     find . -type d -print    #在当前目录下,查找所有的目录;
1.4、在find中使用exec和ok来执行shell命令:
    cb@Standalone14:~/Documents$ find . -type f  -exec ls -l {} \;     #在当前目录下,查找所有文件,然后执行 ls -l命令;
    cb@Standalone14:~/Documents$ find . -name "myfile2" -ok rm {} \;    #用安全模式ok,查找文件并删除文件;
    < rm ... ./shellT/myfile2 > ? yes
    root@Standalone14:/home/cb/Documents# find /etc -name "passwd*" -type f  -exec grep "cb" {} \;    #查找文件,并通过grep查看文件中是否有包含cb的行。
2、xargs
    xargs把find的结果分批传给shell命令,避免过程占用机器的资源。
    cb@Standalone14:~/Documents$ find . -type f -print | xargs file    #查找当前目录下,所有文件,然后查看这些文件的文件类型。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值