Skip to content

Instantly share code, notes, and snippets.

@binzram
Created April 20, 2017 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binzram/072c2f4c3455510dd79ce90af2501646 to your computer and use it in GitHub Desktop.
Save binzram/072c2f4c3455510dd79ce90af2501646 to your computer and use it in GitHub Desktop.
Find files in month range (2013-11-24)
#!/bin/sh
if [ "$#" -lt 2 ]; then
echo "Usage: `basename $0` MM YYYY [other find parameters]" >&2
exit 1
fi
set -e #exit early on error
month=$1
year=$2
shift; shift
if [ "$month" = "12" ]; then
next_year=`expr $year + 1`
next_year=`printf "%02d" $next_year` #date requires YY not Y
next_month=1
else
next_year=$year
next_month=`expr $month + 1`
fi
now=`date --utc +%s`
start=`date --date="$year-$month-01 UTC" +%s`
end=`date --date="$next_year-$next_month-01 UTC" +%s`
if [ $start -gt $now ]; then
start=$now
end=""
elif [ $end -gt $now ]; then
end=""
fi
start_days_ago=`expr \( $now - $start \) / 86400`
start_days_ago=`expr $start_days_ago + 1`
if [ "$end" ]; then #faster
end_days_ago=`expr \( $now - $end \) / 86400`
find "$@" -daystart -mtime -$start_days_ago -mtime +$end_days_ago -exec ls -l {} \;
else
[ `echo -n $month | wc -c` -eq 1 ] && month="0$month"
[ `echo -n $year | wc -c` -eq 2 ] && year="20$year"
find "$@" -daystart -mtime -$start_days_ago -exec ls -l {} \;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment