I have a daily backups named like this:
yyyymmddhhmm.zip // pattern
201503200100.zip // backup from 20. 3. 2015 1:00
I'm trying to create a script that deletes all backups older than 3 days. The script should be also able to delete all other files in the folder not matching the pattern (but there would be a switch for that in the script to disable this).
To determine the file age I don't want to use backups timestamps as other programs also manipulate with the files and it can be tampered.
With the help of: Remove files older than 5 days in UNIX (date in file name, not timestamp) I got:
#!/bin/bash
DELETE_OTHERS=yes
BACKUPS_PATH=/mnt/\!ARCHIVE/\!backups/
THRESHOLD=$(date -d "3 days ago" +%Y%m%d%H%M)
ls -1 ${BACKUPS_PATH}????????????.zip |
while read A DATE B FILE
do
[[ $DATE -le $THRESHOLD ]] && rm -v $BACKUPS_PATH$FILE
done
if [ $DELETE_OTHERS == "yes" ]; then
rm ${BACKUPS_PATH}*.* // but I don't know how to not-delete the files matching pattern
fi
But it keeps saying:
rm: missing operand
Where is the problem and how to complete the script?
!
at the beginning of yourARCHIVE
andbackups
directory names? If so, why in the world would you want to complicate your life in such a way?!
to keep the one or two key directories on top of the file listings when there is a lot of other files/folders in that directory and it is sorted by name. I am all ears if there is a better technique for that :)aa
or similar to the name, for exampleaabackups
. That way, you don't need to escape special characters. It's a matter of personal preference though.