0

This works on Ubuntu but not on Mac.

$ seq -f %1.0f 20210101 20211231 | date -f - 2>/dev/null | grep 'Sun\|Sat'
...
Sat Dec 18 00:00:00 JST 2021
Sun Dec 19 00:00:00 JST 2021
Sat Dec 25 00:00:00 JST 2021
Sun Dec 26 00:00:00 JST 2021

How to find all Sat and Sun from command line?

2

The macOS/BSD version of date can't read from standard input so you need to loop manually.

for d in $(seq -f %1.0f 20210101 20211231); do 
    date -jf "%Y%m%d" $d 2>/dev/null | grep 'Sat\|Sun'
done

You can avoid most of the impossible dates created by seq (and get the result faster) by running

for m in {01..12}; do
    for d in {01..31}; do
        date -jf "%Y%m%d" "2021${m}${d}" 2>/dev/null
    done
done | grep 'Sat\|Sun'
3
  • Thanks. As you mentioned the second one is much faster. I didn't know that macOS/BSD version of date can't read from standard input. – shin Feb 13 at 8:58
  • 1
    @shin I find Man pages (e.g. man date) to be extremely helpful when porting shell scripts from Linux. – nohillside Feb 13 at 9:11
  • Comments are not for extended discussion; this conversation has been moved to chat. – nohillside Feb 13 at 15:32

You must log in to answer this question.

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