前段时间我写了一个脚本,如果文件和目录超过 3 天,则将它们移动到,并在 30 天后从该目录中删除它们Downloads
。.Downloads
它工作得很好,但只适用于没有空格的文件。
我调查并发现find
我在脚本中使用的命令对于名称中带有空格的任何文件或目录都没有按预期工作。
这是做什么find
的:
我希望看到该find
命令也能找到带有空格的文件。
这是脚本:
#! /bin/bash
# set -x
export DISPLAY=:0.0
# true - delete, else - move
function process(){
if [ "$2" = "DELETE" ]; then
rm -r "$1" && notify-send "$3 $1 deleted!"
else
mv "$1" "../.Downloads/$1" && notify-send "$3 $1 moved to ~/.Downloads/$1!"
fi
}
# remove empty directories
for emptyDir in `find ~/Desktop/ ~/Downloads/ -empty -type d`; do
notify-send "Directoy $emptyDir was deleted, because was empty!"
done
find ~/Desktop/ ~/Downloads/ -empty -type d -delete
# remove / move old files / directorie
if [ -z "$1" ] || [ "${1,,}" != "delete" ] && [ "${1,,}" != "move" ]; then
echo "Give as parameter mode ( delete / move )"
exit
fi
if [ "${1,,}" == "delete" ]; then
day=30
path=".Downloads"
mode="DELETE"
else
day=2
path="Downloads"
mode="MOVE"
cr
if [ ! -d "~/.Downloads" ]; then
mkdir -p ~/.Downloads
fi
fi
cd ~/$path
for element in *
do
if [ -d "$element" ]; then
if [ $(find "$element" -type f -mtime -$day | wc -l) -eq 0 ]; then
process "$element" "$mode" "Directory"
fi
else
if [ $(find `pwd` -name "$element" -mtime +$day | wc -l) -gt 0 ]; then
process "$element" "$mode" "File"
fi
fi
done
我会请你告诉我我可能做错了什么。
提前致谢!