`
yuanlanxiaup
  • 浏览: 858485 次
文章分类
社区版块
存档分类
最新评论

find,grep和exec的使用

 
阅读更多

[转贴]find,grep和exec的使用
find是查找文件,而grep是在文件中查找字符串。
1.man find的部分说明:
-exec command ;
Execute command; true if 0 status is returned. All
following
arguments to find are taken to be arguments to the
command until
an argument consisting of `;' is encountered. The
string `{}'
is replaced by the current file name being processed
everywhere
it occurs in the arguments to the command, not just in
arguments
where it is alone, as in some versions of find. Both
of these
constructions might need to be escaped (with a `/') or
quoted to
protect them from expansion by theshell. The command
is exe-
cuted in the starting directory.
find . -name "*.txt"或者find . -name *.txt
在当前目录下查找*.txt文件。结果中当前目录的路径以./表示。
[root@VIAC3-03 root]# find . -name *.txt
./pet.txt
find ~ -name "*.txt"或者find . -name *.txt
在当前目录及其子目录下查找*.txt文件。结果中当前路径会显示。示例:
[root@VIAC3-03 root]# find ~ -name *.txt
/root/pet.txt
find ~ -name *.txt -exec ls -l {} /;
在当前目录及其子目录下查找*.txt文件,并将查找到的文件信息显示出来。注意:{}和/之间有空格;不要少了最后的分号“;”,否则提示find:
missing argument to `-exec',这个意思并不是说没有exec参数,而是exec后面的参数不对。示例:
[root@VIAC3-03 root]# find ~ -name *.txt -exec ls -l {} /;
-rw-r--r-- 1 root root 272 Oct 16 17:47
/root/pet.txt
find ~ -name "*.txt" -exec grep "Gwen" {} -nH /;
在当前目录中查找*.txt文件,并在这些查找到的文件的内容中搜索字符串"Gwen",将含有字符串"Gwen"的文件名(包括路径)和字符串所在行及行号显示出来。示例:
[root@VIAC3-03 root]# find ~ -name "*.txt" -exec grep "Gwen" {} -nH
/;
/root/pet.txt:2:Claws Gwen cat m 1994-03-17 /N
/root/pet.txt:6:Chirpy Gwen bird f 1998-09-11 /N
/root/pet.txt:7:Whistler Gwen bird /N 1997-12-09
/N
2. grep的用法和常用选项
grep "Gwen" *.txt
在当前目录下所有.txt文件中查找字符串"Gwen"。示例:
[root@VIAC3-03 root]# grep "Gwen" *.txt
Claws Gwen cat m 1994-03-17 /N
Chirpy Gwen bird f 1998-09-11 /N
Whistler Gwen bird /N 1997-12-09 /N
grep -iH "gwen" *.txt
在当前目录下所有.txt文件中不区分大小写地查找字符串"gwen",结果中显示文件名和字符串所在行。示例:
[root@VIAC3-03 root]# grep -iH "gwen" *.txt
pet.txt:Claws Gwen cat m 1994-03-17 /N
pet.txt:Chirpy Gwen bird f 1998-09-11 /N
pet.txt:Whistler Gwen bird /N 1997-12-09 /N
常用选项:
-c 只输出匹配行的计数。
-i 不区分大小写(只适用于单字符)。
-h 查询多文件时不显示文件名。
-l 查询多文件时只输出包含匹配字符的文件名。
-n 显示匹配行及行号。
-s 不显示不存在或无匹配文本的错误信息。
-v 显示不包含匹配文本的所有行。

分享到:
评论

相关推荐

    UNIX Handbook

    11.grep匹配带有数字6 后跟至少3个0的文本行 使用 E启用边界特性 22 12.grep匹配含有 abc 的文本 22 13.常用正则表达式举例 22 14.grep精确匹配:在抽取字符串后加 > 23 15.grep消除大小写:加入 i选项 23...

    LINUX与UNIX_Shell编程指南(上)

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    Linux与unix shell编程指南(1-16)

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    shell编程和unix命令

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    LINUX 与 UNIX SHELL编程指南

    第 2 章 使用 find 和 xargs 14 2.1 find 命令选项 14 2.1.1 使用 name 选项 15 2.1.2 使用 perm 选项 16 2.1.3 忽略某个目录 16 2.1.4 使用 user 和 nouser 选项 16 2.1.5 使用 group 和 nogroup 选项 16 ...

    linux shell 编程教程

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    Linux与unix shell编程指南

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    shell 编程指南pdf

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    LINUX与UNIX SHELL编程指南(很全)

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    linux与unix shell 编程(下)

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    LINUX与UNIX SHELL编程指南.rar

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    Linux与unix shell编程指南1.rar

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    shell教程-30章,下了之后会让你大吃一惊,相当好

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

    LINUX与UNIX SHELL编程指南

    第2章 使用find和xargs 14 2.1 find命令选项 14 2.1.1 使用name选项 15 2.1.2 使用perm选项 16 2.1.3 忽略某个目录 16 2.1.4 使用user和nouser选项 16 2.1.5 使用group和nogroup选项 16 2.1.6 按照更改时间查找文件 ...

Global site tag (gtag.js) - Google Analytics