当目录不再包含(任何目录)任何 .mp3 或 .ogg 文件时,我有以下脚本递归清理目录:
set -u
find -L $1 -depth -type d | while read dir
do
songList=`find -L "$dir" -type f \( -iname '*.ogg' -o -iname '*.mp3' \)` && {
if [[ -z "$songList" ]]
then
echo removing "$dir"
rm -rf "$dir"
fi
}
done
这很好用,除了在目录名称的最后一个字符有空格的情况下它会失败,在这种情况下find
,如果脚本被调用,第二个会失败,并带有以下反馈。作为其唯一参数,并且存在具有路径的目录'./FOO/BAR BAZ '
(注意末尾的空格):
find: `./FOO/BAR BAZ': No such file or directory
(注意最后缺少的空格,尽管其他空格保持不变。)
我很确定这是引用的事情,但是我尝试过的所有其他引用方式都会使行为变得更糟(即更多目录失败)。