2

目前我有多个目录

Directory1 Directory2 Directory3 Directory4

这些目录中的每一个都包含文件(这些文件有些神秘)

我想做的是扫描文件夹中的文件以查看是否存在某些文件,如果存在则不理会该文件夹,如果某些文件不存在则删除整个目录。这就是我的意思:

我正在搜索包含单词 .pass 的文件。在文件名中。说目录 4 有我正在寻找的那个文件

Direcotry4:    
file1.temp.pass.exmpl
file1.temp.exmpl
file1.tmp

其余目录没有该特定文件:

file.temp
file.exmp
file.tmp.other

所以我想删除 Directory1,2 和 3 但只保留 Directory 4 ...

到目前为止,我已经想出了这段代码

(arr 是所有目录名称的数组)

for x in ${arr[@]}
do
    find $x -type f ! -name "*pass*" -exec rd {} $x\;
done

我想到的另一种方法是这样的:

for x in ${arr[@]}
do

    cd $x find . -type f ! -name "*Pass*" | xargs -i rd {} $x/
done

到目前为止,这些似乎都不起作用,我害怕我可能会做错什么并删除我所有的文件.....(我已经备份了)

有什么办法可以做到这一点吗?记住我希望目录 4 保持不变,我想保留其中的所有内容

4

2 回答 2

2

要查看您的目录是否包含传递文件:

if [ "" = "$(find directory -iname '*pass*' -type f |  head -n 1)" ]
  then
    echo notfound
  else
    echo found
fi

要在循环中执行此操作:

for x in "${arr[@]}"
  do
      if [ "" = "$(find "$x" -iname '*pass*' -type f |  head -n 1)" ]
        then
          rm -rf "$x"
      fi
  done
于 2011-07-22T10:56:47.177 回答
1

试试这个:

# arr is a array of all the directory names
for x in ${arr[@]}
do
ret=$(find "$x" -type f -name "*pass*" -exec echo "0" \;)
# expect zero length $ret value to remove directory
if [ -z "$ret" ]; then
    # remove dir
    rm -rf "$x"
fi
done
于 2011-07-22T07:25:05.057 回答