列出不包含某些文件的目录?

时间:2012-10-13 14:02:40

标签: bash shell testing wildcard

我使用此命令查找当前目录中包含.mp3的所有目录,并仅过滤掉目录名称:

find . -iname "*.mp3" | sed -e 's!/[^/]*$!!' -e 's!^\./!!' | sort -u

我现在想要相反,但我发现它有点困难。我不能只添加'!'查找命令,因为它只会在打印时排除.mp3,而不能找到不包含.mp3的目录。

我用Google搜索并搜索了stackoverflow和unix.stackexchange.com。 到目前为止我已经尝试过这个脚本并返回此错误:

#!/bin/bash

find . -type d | while read dir
do
if [[! -f $dir/*.mp3 ]]
then
    echo $dir
fi
done

/home/user/bin/try.sh:line 5:[[!: command not found

#!/bin/bash

find . -type d | while read dir
do
if [! -f $dir/*.mp3 ]
then
    echo $dir
fi
done

/home/user/bin/try.sh:line 5:[!:找不到命令

#!/bin/bash

find . -type d | while read dir
do
if [[! -f "$dir/*.mp3" ]]
then
    echo $dir
fi
done

/home/user/bin/try.sh:line 5:[!:找不到命令

我认为它与测试命令的多个参数有关。

由于我正在测试变量将要更改的所有目录,并且我使用通配符作为文件名。

非常感谢任何帮助。谢谢。

3 个答案:

答案 0 :(得分:2)

[ "$(echo $dir/*.mp3)" = "$dir/*.mp3" ]

应该有用。

或者只是在'['和'!'

之间添加一个空格

可能明显更快的方法是

if find "$dir" -name '*.mp3' -quit ; then
  : # there are mp3-files in there.
else
  ; # no mp3:s
fi

答案 1 :(得分:1)

好的,我用一个柜台解决了我自己的答案。

我不知道它有多高效,但它确实有效。我知道它可以变得更好。请随意批评。

find . -type d | while read dir
do
count=`ls -1 "$dir"/*.mp3 2>/dev/null | wc -l`
    if [ $count = 0 ]
    then
        echo $dir
    fi
done

这将打印所有不包含MP3的目录。由于find命令打印目录递归,它还显示了子目录。

答案 2 :(得分:0)

我运行了一个脚本来自动下载我的mp3收藏封面。它放了一个名为" cover.jpg"在每个专辑的目录中,它可以检索艺术品。我需要检查脚本失败的哪些专辑 - 即哪些CD(目录)不包含名为cover.jpg的文件。这是我的努力:

find . -maxdepth 1 -mindepth 1 -type d | while read dir; do [[ ! -f $dir/cover.jpg ]] && echo "$dir has no cover art"; done

maxdepth 1阻止find命令进入我的WD My Cloud NAS服务器为每个专辑创建的隐藏目录,并放置默认的通用光盘映像。 (这在下一次扫描中被清除。)

编辑:cd到MP3目录并从那里运行,或更改。在上面的命令中指向它的路径。