我有一组我下载的文件包含我要删除的文件。我想创建一个表单的列表,脚本应该支持blobbing所以我可以非常积极地删除文件,而不会在文件列表中使用正则表达式的复杂性。
我也很困惑,因为我在我的脚本循环中放了一个sleep命令,并且在每次迭代后都没有运行,但是在运行结束时只运行一次。
这是脚本
# Get to the place where all the durty work happens
cd /Volumes/Videos
FILES=".DS_Store
*.txt
*.sample
*.sample.*
*.samples"
if [ "$(pwd)" == "/Volumes/Videos" ]; then
echo "You are currently in $(pwd)"
echo "You would not have read the above if this script were operating anywhere else"
# Dekete fikes from list above
for f in "$FILES"
do
echo "Removing $f";
rm -f "$f";
echo "$f has been deleted";
sleep 10;
echo "";
echo "";
done
# See if dir is empty, ask if we want to delete it or keep it
# Iterate evert movie file, see if we want to nuke contents. Maybe use part of last openned to help find those files fast
else
# Not in the correct directory
echo "This script is trying to alter files in a location that it should not be working"
echo "Script is currently trying to work in $(pwd)"
exit 1
fi
完全难倒的主要事情是sleep
命令。它运行一次,而不是每次迭代一次。如果我有100个文件要通过,我会得到10秒的睡眠,而不是100 * 10。
我将添加一些其他功能,例如,如果文件小于x字节,请继续并删除它。这些文件在文件名中将包含空格和其他奇数字符,我是否正确创建变量以使此脚本处理这些场景以及尽可能符合POSIX。我会将shebang更改为sh over bash并尝试添加set -o noun set并设置-o err exit虽然我这样做时会遇到很多麻烦。
我应该使用更好的列表吗?我不反对将模式匹配列表存储在单独的文件中。我可以包含它,或者用任何一些命令读取它。
这些也是嵌套文件,dir,包含文件或包含包含某些文件的目录的目录。像这样:
/Volumes/Videos:
The Great guy in a tree
The Great guy in a tree S01e01
sample.avi
readme.txt
The Great guy in a tree S01e01.mpg
The Great guy in a tree S01e02
The Great guy in a tree S01e02.mpg
The Great guy in a tree S01e03
The Great guy in a tree S01e03.mpg
The Great guy in a tree S01e04
The Great guy in a tree S01e04.mpg
谢谢。
答案 0 :(得分:1)
您的脚本无法正常工作的原因是您的for
循环写错了。此示例显示了正在进行的操作:
$ i=0
$ FILES=".DS_Store
*.txt
*.sample
*.sample.*
*.samples"
$ for f in "$FILES"; do echo $((++i)) "$f"; done
1 .DS_Store
*.txt
*.sample
*.sample.*
*.samples
请注意,只输出一个数字,表示该循环仅绕过一次。此外,没有发生路径名扩展。
为了使您的脚本按预期运行,您可以删除"$FILES"
周围的引号。这意味着字符串中的每个单词都将单独评估,而不是一次性评估。这也意味着您正在使用的通配符的路径名扩展将会发生,因此所有以.txt
结尾的文件都将被删除,我想这就是您的意思。
您可能更喜欢使用数组:
,而不是使用字符串来存储表达式列表FILES=( '.DS_Store' '*.txt' '*.sample' '*.sample.*' '*.samples' )
每个元素周围的引号都会阻止扩展(因此数组只有5个元素,而不是完全展开的列表)。然后,您可以将循环更改为for f in ${FILES[@]}
(同样,在扩展列表的每个元素时,不会出现双引号。)
虽然删除引号会修复您的脚本,但我同意@ hek2mgl建议使用find
。它允许您按名称,大小,修改日期以及在一行中查找更多文件。如果要在删除每个文件之间暂停,可以使用以下内容:
find \( -name "*.sample" -o -name "*.txt" \) -delete -exec sleep 10 \;
答案 1 :(得分:0)
您可以使用find
:
find -type f -name '.DS_Store' -o -name '*.txt' -o -name '*.sample.*' -o -name '*.samples' -delete