当目录不再包含任何带有.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
(请注意,尽管其他空格保留不变,但最后缺少的空格)。
我很确定这是一个引号,但是我尝试过的其他所有引号方式都会使行为变得更糟(即,更多目录失败)。
read
遇到空格时将输入拆分。报价help read
:
Read a line from the standard input and split it into fields.
Reads a single line from the standard input, or from file descriptor FD
if the -u option is supplied. The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME. Only the characters found in $IFS are recognized as word
delimiters.
您可以设置IFS
并避免单词分裂。说:
find -L "$1" -depth -type d | while IFS='' read dir
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句