for i in $*;do
find "$i" -name "*.c" -exec rm -r -f {} \;
[ "$i/*"=$i/*]&&{
rmdir "$i"
echo "Delete Directory $i"
}
done
应用的时候比如把这个脚本程序命名为a.sh,然后在该程序目录下建立1和2两个目录,再在目录1下面建立11.c和12.c任意两个文件,目录2下面建立21.c和22.txt两个文件。
然后运行 ./a.sh 1 2
结果就只剩下2目录和其下面的22.txt文件,其他的都被删掉了。
这里面我有这么3个问题:
1,find "$i" -name "*.c" -exec rm -r -f {} \;,这后面的{} \是干什么用的?
2,[ "$i/*"=$i/*]判断语句中, "$i/*"和$i/*分别指什么?按我的理解:比如i是目录1的时候,这两者不都是指1/*,也就是目录1下所有文件的意思,但是事实显然不是这样的。所有请指点。
跪求!
wucongdonglai 于 2010-07-17 08:44:04发表:
6# deepwhite
恩,我这边没有,在敲入上面的程序后运行后,它显示“Delete Directory 1”,然后就完了,再去查看的时候,目录1和下面的文件没有了,目录2和22.txt
还在!
从你那种情况分析的话,应该在判断目录2了之后,又运行了rmdir "&i"(此时&i为2),由于目录2非空,所有就有这么个报警!那也就是说前面的判别条件“$i/*"=$i/*恒等,所以在目录2下的判断也为真了。但是在我这,它怎么又成假的了呢?纳闷!
deepwhite 于 2010-07-16 17:42:02发表:
你运行的时候没有报错或者警告信息么?
我这里运行会报错:
[code]
rmdir: 删除 "2" 失败: 目录非空
Delete Directory 2
[/code]
也就是说,目录2之所以没有被删除,是因为其中一个txt文件,该目录不为空,而rmdir只能删除空目录,所以目录2没有被删除。
这里可以推测,第二点不能够用于判断目录是否为空。判断目录是否为空一般可以用 find . -type d -empty来实现。
wucongdonglai 于 2010-07-16 14:06:43发表:
4# deepwhite
恩,第一点解决了,还是自己鸟文没看仔细!
对于第二点,我运行了一下,确实是可以达到他所说的效果:从结果上判断,这应该是想要判断 i/ 下面的目录是否为空,如果为空,就 rmdir "$i" 把该i目录删除。但是我看来看去,怎么 "$i/*" 和 $i/* 是一样的啊!所以很不明白
deepwhite 于 2010-07-16 13:31:36发表:
1. Linux 下面有个工具用来看命令或者函数的manual ---- man。在终端输入[code]man find[/code],仔细阅读,你会找到你想要的答案:[code]
-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 the
shell. See the EXAMPLES section for examples of the
use of the -exec option. The specified command is run
once for each matched file. The command is executed in
the starting directory. There are unavoidable
security problems surrounding use of the -exec action;
you should use the -execdir option instead.
[/code]简而言之,{}将会被替换成find的结果;\;表示参数的结束。
2. "$i/*"和$i/* 我理解应该是一样的,不知道在这里他想用这几话表达什么意思。
s430504 于 2010-07-15 11:12:48发表:
dddddddddddddddddddddddddddddddd
wucongdonglai 于 2010-07-15 09:00:57发表:
哪个高手帮忙解决一下嘛