Skip to content

Instantly share code, notes, and snippets.

@talkingmoose
Last active May 5, 2023 19:53
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talkingmoose/e7f2a54da90a11b66fac29f68b9a282f to your computer and use it in GitHub Desktop.
Save talkingmoose/e7f2a54da90a11b66fac29f68b9a282f to your computer and use it in GitHub Desktop.
Resources for my Penn State 2022 MacAdmins Campfire presentation on June 2

Speed Dating for Mac Admins

Terminal Login Banner

Last login: Wed Jun  1 23:03:39 on ttys000


                        'c.            Logged in as: bill.smith
                     ,xNMM.            ---------------------------------
                   .0MMMMo             Operating System: macOS 12.4
                   0MMM0,              Computer Name: MooseBook Pro II
         .;loddo:' loolloddol;.        Model Name: MacBook Pro
       cKMMMMMMMMMMNWMMMMMMMMMM0:      Model Identifier: MacBookPro18,3
     .KMMMMMMMMMMMMMMMMMMMMMMMMWd.     Serial Number (system): JP24RE9XNRY
     ;XMMMMMMMMMMMMMMMMMMMMMMMX.       Memory: 16 GB
     ;MMMMMMMMMMMMMMMMMMMMMMMM:        
     :MMMMMMMMMMMMMMMMMMMMMMMM:        FileVault is On.
     .MMMMMMMMMMMMMMMMMMMMMMMMX.       Activation Lock Status: Disabled
      kMMMMMMMMMMMMMMMMMMMMMMMMWd.     Uptime: 1 day, 21:47
      .XMMMMMMMMMMMMMMMMMMMMMMMMMMk    Battery Cycle Count: 35
       .XMMMMMMMMMMMMMMMMMMMMMMMMK.    Shell: /bin/zsh
         kMMMMMMMMMMMMMMMMMMMMMMd.    
          ;KMMMMMMMWXXWMMMMMMMMk.      Today is Thursday, June 02, 2022
            .cooc,.    .,coo:.         On this day: First issue of Computerworld, 1967
            

.zlogin script

Create a .zlogin file in your home folder and paste the contents into the new file. https://gist.github.com/talkingmoose/15f055885b51cc8cb0bc7aad021acead

Oh My Zsh

https://ohmyz.sh

The theme

https://github.com/agnoster/agnoster-zsh-theme

The fonts

https://github.com/powerline/fonts

Instructions I followed for customization

https://dev.to/hannahgooding/how-i-customise-my-terminal-with-oh-my-zsh-macos-427i

Useful information about formatting colors

https://misc.flogisoft.com/bash/tip_colors_and_formatting

Color themes for macOS Terminal

https://github.com/lysyi3m/macos-terminal-themes#vs-code-dark-plus-download

Terminal customizations

Silence the “Last login” message
/usr/bin/touch .hushlogin

Restore the “Last login” message
/bin/rm .hushlogin

cal

View formatted calendars in Terminal
/usr/bin/cal

     June 2022        
Su Mo Tu We Th Fr Sa  
          1  2  3  4  
 5  6  7  8  9 10 11  
12 13 14 15 16 17 18  
19 20 21 22 23 24 25  
26 27 28 29 30       

View a different month
cal sep 2022

View a month in a different year
cal apr 2030

View the full calendar for a different year
cal 2030

Invoke ncal mode
cal -N

ncal

View formatted calendar in Terminal (vertical)
/usr/bin/ncal

    June 2022         
Mo     6 13 20 27   
Tu     7 14 21 28   
We  1  8 15 22 29   
Th  2  9 16 23 30   
Fr  3 10 17 24      
Sa  4 11 18 25      
Su  5 12 19 26     

Get all the Fridays of a month
ncal | grep -E 'Fr'

Get all the Fridays of a year
ncal 2022 | grep -E 'Fr'

Include year and months
ncal 2022 | grep -E '^[[:blank:]]|Fr'

Display week numbers
ncal -w or cal -Nw

Display week numbers for entire year
ncal -w 2022 or cal -Nw 2022

Get all the Fridays of a month
ncal -w | grep -E 'Fr'

Get all the Fridays of a year
ncal -w 2022 | grep -E 'Fr'

Include year and months
ncal -w 2022 | grep -E '^[[:blank:]]|Fr'

calendar

Earliest version of “calendar” command
https://www.freebsd.org/cgi/man.cgi?query=calendar&apropos=0&sektion=1&
manpath=Unix+Seventh+Edition&arch=default&format=html

New commands added to Unix Seventh Edition include Bourne shell, awk and calendar 
https://en.wikipedia.org/wiki/Version_7_Unix

Lord of the Rings first appears in 4.4BSD in .history
https://news.ycombinator.com/item?id=4847821

Lord of the Rings first appears February 15, 2003
https://svnweb.freebsd.org/base?view=revision&revision=110933

Bug 57623 - One LOTR calendar event still in calendar.history
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=57623#c0

View calendar.lotr
https://opensource.apple.com/source/misc_cmds/misc_cmds-23/calendar/calendars/calendar.lotr.auto.html

Note: Calendar relies on the cpp library. Your first run may ask you to install cpp as part of the Xcode command line utilities.

View the Lord of the Rings calendar
/bin/cat /usr/share/calendar/calendar.lotr

List more calendars
ls /usr/share/calendar/

Read a specific calendar for events today and tomorrow

/usr/bin/calendar -f /usr/share/calendar/calendar.music  
Jun  3 	Georges Bizet dies in Bougival, Paris, France, 1875

Creating your own calendar

File ‘calendar’ in your home directory

/*
 * Bill's calendar
 */


*/03	Pay mortgage
Friday	Family game night
05/MonLast	Memorial Day


/* Birthdays and Anniversaries */

01/16	Kirsten's birthday, 1969
02/14	Anniversary, 2000
05/27	Dean's birthday, 1966
06/03	Lori's birthday, 1958


/* Backups */

Monday	Incremental backups, set 1
Tuesday	Incremental backups, set 2
Wednesday	Incremental backups, set 3
Thursday	Incremental backups, set 4
Friday+1	Full backups
Friday+2	Incremental backups, set 5
Friday+3	Incremental backups, set 5
Friday+4	Incremental backups, set 5
Friday+5	Incremental backups, set 5


/* Other calendars */

#include <calendar.music>
#include <calendar.lotr>% 

06/02 + tab + description

Asterisk ( * ) can mean any month

Friday means every Friday of the year

MonLast means last Monday of the month

Friday+3 means third Friday of the month

Include other calendars from /usr/share/calendars
#include <calendar.lotr>

time

Time a command
time + any command (including sudo)

There are two “times”

  • shell built-in
  • /usr/bin/time

View the time man page man time

Print times on separate lines
/usr/bin/time -p + any command

To get just the time

(/usr/bin/time -p sudo jamf recon) 2>&1 | /usr/bin/awk '/real/{print $2}'
Password:
37.39

uptime

How long has your computer been running?
/usr/bin/uptime

Depending on the number of days, minutes and seconds your Mac has been running, uptime may return one of five formats

17:00 up 5 days, 51 mins, 2 users…
17.10 up 5 days, 1:01, 2 users…
17:00 up 51 secs, 2 users…
17:00 up 2 mins, 2 users…
17:00 up 1:01, 2 users…

Parse for a computer’s up time

/usr/bin/uptime | /usr/bin/awk -F 'up |, [0-9]+ users' '{ print $2 }'
1 day, 21:34

strftime

String formatter for time

View the man page for list of conversion characters
man strftime

View current date and time in this format: 2022-06-02 09:55:01
strftime '%Y-%m-%d %H:%M:%S'

View specific date and time in this format: 2014-10-31 07:40:18
strftime '%Y-%m-%d %H:%M:%S' 1414759218

View epoch date and time in this format: 1969-12-31 18:00:00
strftime '%Y-%m-%d %H:%M:%S' 0

View epoch date and tiime (adjust for Central time -06:00) in this format: 1970-01-01 00:00:0
strftime '%Y-%m-%d %H:%M:%S' 21600

Example conversion characters

Target date: 2022-06-02 13:27:01

Full day of the week
%A — Thursday

Abbreviated day of the week
%a — Thu

Full month
%B — June

Abbreviated month
%b — Jun

Minutes not month
%M — 27

Numerical month
%m — 06

Hour as two-digit 24-hour
%H — 13

Hour as two-digit 12-hour
%I — 01

Hour as padded 12-hour
%l — _1

Hour as unpadded 12-hour
%-l — 1

date

Print default view of date and time for right now
/bin/date

Set the clock to a specific date and time
07 = month
01 = day
00 = hour
00 = minute
22 = year
date 0701000022

Set the clock to a specific time (e.g. 5 o'clock p.m.)
17 = hour
00 = minute
date 1700

Resync with time server by turning time sync off and on
sudo /usr/sbin/systemsetup -setusingnetworktime off; sudo /usr/sbin/systemsetup -setusingnetworktime on

Epoch time

Print epoch time for right now
date +'%s' same as strftime %s

Subtract one epoch time from another to display elapsed time
echo $((1654194332-1654194325))
7

Script snippet for a timer

#!/bin/zsh


# start the timer
startTime=$( /bin/date '+%s' )



# do a whole lotta stuff here
# that takes a little while

sleep 5



# stop the timer
stopTime=$( /bin/date '+%s' )

# calculate elapsed time
timeDiff=$((stopTime-startTime))

# report running time
echo "The script took $timeDiff seconds to run."

exit 0

Convert one date format to another
Original format date: 2022-06-02
Reformatted date: Thursday, June, 2, 2022
date -j -f '%Y-%m-%d' 2022-06-02 +'%A, %B, %-d, %Y'

Adjust date and time properties when a specific day or date is unknown
Each adjustment begins with -v

Set a specific property to a specific value
-vMonday = Set the weekday of a date string to Monday
-v2023y = Set the year of a date string to 2023
-v3m = Set the month of a date strig to March -v13h = Set the hour of a date string to 13 or 1 p.m. -v26d = Set the date of a date string to 26

Adjust backward a specific property
-v-Monday = Adjust the weekday to the previous Monday
-v-2y = Adjust the year of a date string to two years ago
-v-3m = Adjust the month of a date strig to three months ago
-v-1h = Adjust the hour of a date string to one hour ago
-v-7d = Adjust the date of a date string to seven days ago

Adjust forward a specific property
-v+Monday = Adjust the weekday to the next Monday
-v+2y = Adjust the year of a date string to two years from now
-v+3m = Adjust the month of a date strig to three months from now
-v+1h = Adjust the hour of a date string to one hour from now
-v+7d = Adjust the date of a date string to seven days from now

Determine whether 2023 is a Leap Year (i.e., includes February 29)

Starting from June 2, 2022
date -v+1y -v3m -v1d -v-1d or
date -v2023y -v3m -v1d -v-1d
Tue Feb 28 10:42:29 CST 2023 is not a Leap Year

Starting from June 2, 2022
date -v+2y -v3m -v1d -v-1d or
date -v2024y -v3m -v1d -v-1d
Thu Feb 29 10:43:00 CST 2024 is a Leap Year

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment