0

I would like to find files created in the ISO week number of the year. Those two values will be provided by the user as an arguments.

For example user provides the two values:

Please specify the year: 2020 
Please specify the ISO week number: 10

And the script is executing the find command which is listing the files between 2020-03-02 and 2020-03-08

find . -type f -newermt 2020-03-02 ! -newermt 2020-03-08

Is there a simple way to do it (find optional argument or something like this)?

1 Answer 1

0

It's not straightforward, but provided you have a tool such as GNU date to manage the date arithmetic it's quite possible.

#!/bin/bash
#
# Find the date range for an ISO year and week number
#######################################################################

isoYear=$1
isoWeek=$2
shift 2
[[ $# -gt 0 ]] && fDir=$1 && shift           # Starting directory (optional)

firstJan="1 Jan $isoYear"

fjDoW=$(date --date "$firstJan" +%u)         # Day of week for 1st January

fjThu=$(date --date "$firstJan" +%F)         # Week number for Thursday that week
[[ $fjDoW -ne 4 ]] && fjThu=$(date --date "$firstJan -$fjDoW days +4 days" +%F)

fjMon=$(date --date "$fjThu -3 days" +%F)    # Start of ISO week
fjSun=$(date --date "$fjThu +3 days" +%F)    # End of ISO week

echo "Searching ${fDir-.} for files in the range $fjMon .. $fjSun inclusive" >&2
find "${fDir-.}" -newermt "$(date --date "$fjMon -1 day" +%F)" \! -newermt "$fjSun" "$@"

Typical usage could be like this

./iso-year-week.sh 2020 04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .