如何第归删除子目录下的特定文件?
如题比如说要删除/projects/下面所有子目录里的 *.tr*文件?
多谢。 xargs
在使用find命令的- e x e c选项处理匹配到的文件时, f i nd命令将所有匹配到的文件一起传递给e x e c执行。但有些系统对能够传递给e x e c的命令长度有限制,这样在f i nd命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是x a rg s命令的用处所在,特别是与f i nd命令一起使用。
举例
删除数量比较多的文件
ls | xargs -n 20 rm -fr
ls当然是输出所有的文件名(用空格分割)
xargs就是将ls的输出,每20个为一组(以空格为分隔符),作为rm -rf的参数
也就是说将所有文件名20个为一组,由rm -rf删除,这样就不会超过命令行的长度了 find/etc -type f -name *.con*-exec rm{} \;
先查看一下
find/etc -type f -name *.con*-exec ls -l{} \;
或者用exec的安全模式
find/etc -type f -name *.con*-ok rm{} \;
或者
find/etc -type f -name *.con*-exec rm -i {} \;
总之威力强大删错了别找我,嘿嘿 啊,新学的果然不行
先看见后答出来:? 我曾经自己写过一个脚本:
#!/bin/bash
if [ $# == 1 ]; then
echo -ne "Deleting FILES including [$1] in the CURRENT directory ...\n"
for i in $(tree -a -f -i | grep "$1")
do
rm -f $i
done
elif [ $# == 2 ]; then
echo -ne "Deleting FILES including [$1] in [$2] directory ...\n"
for i in $(tree -a -f -i $2 |grep "$1")
do
rm -f $i
done
else
echo -ne "Arguments Error.\n"
echo -ne "Usage:\n"
echo -ne "\t$0 STRING\n"
echo -ne "\t$0 STRING DIRECTORY\n"
fi
用法:
rm_sth.sh string directory
删除 directory 目录下面文件名包含 string 字串的文件。
如果文件名中有空格就需要再做进一步判断。
[ 本帖最后由 zhy2111314 于 2008-1-25 23:38 编辑 ] 我的比较简单:
#!/bin/bash
function proc()
{
rm *.tr*;
for name in *; do
if cd $name; then
proc;
fi
done
cd ..;
}>/dev/null 2>&1
cd /projects;
proc;
页:
[1]