Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1890632
  • 博文数量: 389
  • 博客积分: 7877
  • 博客等级: 少将
  • 技术积分: 4521
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-10 14:02
文章分类

全部博文(389)

文章存档

2024年(1)

2022年(1)

2021年(1)

2020年(1)

2019年(1)

2018年(3)

2017年(6)

2016年(4)

2015年(8)

2014年(15)

2013年(31)

2012年(19)

2011年(47)

2010年(33)

2009年(105)

2008年(109)

2007年(4)

分类:

2009-07-27 22:19:55

先看几个例子:

在当前目录下(包含子目录),查找所有txt文件并找出含有字符串"bin"的行
find ./ -name "*.txt" -exec grep "bin" {} \;

在当前目录下(包含子目录),删除所有txt文件
find ./ -name "*.txt" -exec rm {} \;

在当前目录下,查看有无kevin.txt文件,有的话将其详细信息写入到log文件,没有的话也把错误信息写入到该log文件,需要用一个小脚本实现:

#!/bin/ksh
export LOG=/tmp/test.log
exec >> $LOG 2>&1
ls -l kevin.txt
exit 0


看了上面例子后,还是不清楚exec到底是干什么的吧!首先,这两中用法中exec是不一样的, "-exec"作为find命令的一个option, 其功能是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.

而作为shell内置命令的exec
IBM的Unix Korn Shell Scripting Reference Sheet里面
Miscellaneous built-in commands这一段里面有exec命令的介绍:

exec [arg]
If arg is present, executes arg in place of this shell.
(arg will replace this shell).


阅读(11606) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-11-17 16:48:12

当然作为上面查找指定目录下的文件,并将其删除掉还有以下命令可以用: find ./ -name "*.txt"|xargs rm -rf 需要注意的是凡是跟删除文件有关的,都要特别小心,以免误删文件

chinaunix网友2009-11-17 16:17:58

find时不区分大小写用法: find ./ -name "[k,K]evin.sh"