Friday, November 30, 2007

Work arround to handle files in a range of dates

This bash script works pretty well in Linux, i would like to know if it also works in other Unix Flavors, please write a comment if you have the possibility to try it in AIX or HPux.
This little script handles files that are between a range of dates. Since the "find" command only permits to manage files older than a date, not in a date range tought.
The script takes the files from between 3 and 5 days ago and prints its names, as academical purpose.

#!/bin/bash
if [ ${#} -ne 1 ] then
echo "Usage: $0 "
echo "E.g.: $0 /tmp /fcon"
exit 1
fi
sourcePath=${1//%\//}
today=$( date +%s )
lowDate=$( date --date="5 days ago" +%s )
topDate=$( date --date="3 days ago" +%s )

echo "Start Date: $startDate";
for arch in "$sourcePath"/*; do
filename="${arch//#${sourcePath}\//}"
if [ -f "$arch" ] && [ $( date -r "$arch" +%s ) -gt $lowDate ] && [ $( date -r "$arch" +%s ) -lt $topDate ]; then
echo -e "$arch\n"
fi
done

0 comentarios: