Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save porg/06fdfb3adc0d9532615e2a7773d7e253 to your computer and use it in GitHub Desktop.
Save porg/06fdfb3adc0d9532615e2a7773d7e253 to your computer and use it in GitHub Desktop.
On macOS touch a file modification date or creation date or both

Touch a file modification date or creation date or both

Syntax

  • touch <file> → Changes the modification date of file to NOW.
  • touch -a <file> → Changes the access date of file to NOW.
  • touch-c <file> → Changes the creation date of file to NOW.
  • touch-cm <file> → Changes both the creation date and the modification date of file to NOW.

Setup

  • To your shell config file ~/.zshrc
  • or your dedicated alias config file like ~/.oh-my-zsh/custom/aliases.zsh
  • add the following lines:
alias touch-c="dateNow=\$(date '+%m/%d/%y %H:%M:%S') ; SetFile -d \"\$dateNow\" \$1"
alias touch-cm="dateNow=\$(date '+%m/%d/%y %H:%M:%S') ; SetFile -d \"\$dateNow\" -m \"\$dateNow\" \$1"

Remarks

  • The original touch command stays as is.
    • The aliases touch-c and touch-cm get added and they use SetFile which is deprecated for ages but still works as of macOS 11 Big Sur.
  • The dollar symbol is escaped.
    • So the commands do not run when the login shell is initialized but only when the alias is executed.
    • date really outputs now.
    • $1 is the file you supply to the alias command.

Demo

➜  ~ cd ~/Desktop 
➜  Desktop GetFileInfo test.txt 
created: 05/30/2022 12:01:01
modified: 05/30/2022 11:47:29
➜  Desktop touch-c test.txt 
➜  Desktop GetFileInfo test.txt
created: 05/30/2022 12:03:38
modified: 05/30/2022 11:47:29
➜  Desktop touch test.txt 
➜  Desktop GetFileInfo test.txt
created: 05/30/2022 12:03:38
modified: 05/30/2022 12:03:51
➜  Desktop touch-cm test.txt   
➜  Desktop GetFileInfo test.txt
created: 05/30/2022 12:04:02
modified: 05/30/2022 12:04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment