find and grep

本文介绍了Linux系统中find和xargs命令的使用方法,包括根据文件名、类型、权限、所有者和更改时间进行搜索,并展示了如何结合xargs进行文件删除、内容查找等操作。通过实例演示了find与xargs的组合在日常文件管理和自动化任务中的强大功能。
摘要由CSDN通过智能技术生成

find命令常用来搜索目录树中的文件或者目录,并可对搜索出来的目录或文件执行某些操作。 
命令格式:find pathname -options [-print -exec -ok…],其中pathname表示在pathname这个目录下寻找。 
-options表示find命令的常用选项,主要有 
(1)、-name 表明按文件或目录名来进行查找; 
(2)、-type 表明将按文件类型来查找; 
(3)、-perm 表明将按文件或目录权限来查找; 
(4)、-user 表明将文件或目录的所有者查找; 
(5)、-group 表明将按文件或目录所属的组来查找; 
(6)、-mtime -n (或+n) 表明按照文件的更改时间来查找文件, 时间都是距离此刻的时间,- n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。
2、-print表示将find命令匹配的文件输出到标准输出,

-> find . -name "*ptp*" -exec ls -l {} \;
-rw-r--r-- 1 huimingf ccase 139178 Dec 28 16:48 ./ptpdriver.cc
-rw-r--r-- 1 huimingf ccase 19047 Dec 28 16:48 ./ptpdriver.hh
-rw-r--r-- 1 huimingf ccase 7705 Dec 28 16:48 ./ptpevent.cc
-rw-r--r-- 1 huimingf ccase 1503 Dec 28 16:48 ./ptpevent.hh
-rw-r--r-- 1 huimingf ccase 2178 Dec 28 16:48 ./ptpmain.c
-rw-r--r-- 1 huimingf ccase 15278 Dec 28 16:48 ./ptptcdriver.cc
-rw-r--r-- 1 huimingf ccase 56432 Dec 28 16:48 ./ptputil.cc

-> find . -name "*ptp*" -ok rm {} \;
< rm ... ./ptpdriver.cc > ? y
-> find ptp/src -name "*ptp*" -ok grep -nr "string.h" {} \;
< grep ... ptp/src/ptpdriver.cc > ? y
14:#include <string.h>
18:#include <string.h>
< grep ... ptp/src/ptpdriver.hh > ? y
< grep ... ptp/src/ptpevent.cc > ? y
13:#include <string.h>
< grep ... ptp/src/ptpevent.hh > ? y
< grep ... ptp/src/ptpmain.c > ? y
< grep ... ptp/src/ptptcdriver.cc > ? y
3:#include <string.h>
7:#include <string.h>
< grep ... ptp/src/ptputil.cc > ? y
1:#include <string.h>

xargs命令是给其他命令传递参数的一个过滤器,常作为组合多个命令的一个工具。它主要用于将标准输入数据转换成命令行参数,xargs能够处理管道或者标准输入并将其转换成特定命令的命令参数

/home/huimingf/GitWorkSpace/test/app/ptp/src
-> find . -name "ptp*"| xargs grep  "ptpdriver.hh" -ls
./ptpdriver.cc
./ptptcdriver.cc
./ptputil.cc
/home/huimingf/GitWorkSpace/test/app/ptp/src
-> find . -name "ptp*"| xargs grep  "ptpdriver.hh"
./ptpdriver.cc:#include "ptpdriver.hh"
./ptptcdriver.cc:#include "ptpdriver.hh"
./ptputil.cc:#include "ptpdriver.hh"
-> find ./ -type f|xargs grep pattern -ls
./.git/info/exclude
./tps/siftester/tc_selection_ctags.py
./tps/siftester/tc_selection_nm.py
./clearcase/old/commitcheck
./rc.d/msys_bashrc
./ptplogger/ptpvisualizer.py
./ptplogger/old/ptpvisualizer_realtime.py
./ptplogger/old/ptp_plot_old.py
./ptplogger/old/ptp_plot_file.py
./pss/parseual.py

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值