Issue
I would like some help for a bash script to read all the files in a ftp directory and delete the ones after the date range stated on its name.
Each file would have a name such as 'title %m%d%Y %m%d%Y' (e.g.: Events 02172015 02192015.png). Is it possible to read all files in the directory comparing the names with the server date and delete the ones which date range is older than the current date?
Solution
First -- change your filename format. Using %Y%m%d
will greatly decrease the amount of pain and suffering involved (and, as a side effect, will make tools that sort by filename put your files in order by date automatically).
Answering your question as originally asked, however...
today=$(date +%Y%m%d)
# filter for filenames with at least two spaces and a period
for f in *" "*" "*.*; do
without_extension=${f%.*}
end_date=${without_extension##* }
## now, if you were using YYYYMMDD, you could just do this:
# [[ $end_date < $today ]] && rm -f -- "$f"
## ...however, since you aren't, you need all this mess:
mon=${end_date:0:2}
day=${end_date:2:2}
year=${end_date:4:4}
end_date_sane=${year}${mon}${day}
[[ $end_date_sane < $today ]] && rm -f -- "$f"
done
Answered By - Charles Duffy Answer Checked By - Mildred Charles (WPSolving Admin)