ISOの週番号からファイルを探す

ISOの週番号からファイルを探す

その年のISO週番号に生成されたファイルを見つけたいです。これら2つの値は、ユーザーがパラメータとして提供します。

たとえば、ユーザーは次の2つの値を提供します。

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

スクリプトは、find2020-03-02から2020-03-08までのファイルを一覧表示するコマンドを実行しています。

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

これを行う簡単な方法はありますか(オプションのfindパラメータまたは同様のもの)。

ベストアンサー1

些細なことではありませんが、日付演算を管理するGNUなどのツールがある限り、dateかなり可能です。

#!/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" "$@"

一般的な使用法は次のとおりです。

./iso-year-week.sh 2020 04

おすすめ記事