Terminal Commands

If you are a power user, you probably have some terminal commands that you like to use. Post them here. Follow the format outline below, either one command per reply or grouped by similar function to make browsing easier, and if you find a bug, respond to the message with the specific command. I’ll start with a few examples.

EDIT: Okay, maybe more than a few…

EDIT2: Trust but verify! Read the MAN page for the command to make sure you won’t do something irreparable.

Group Title only if grouping multiple commands (# for Heading 1)

Command Title (## for Heading 2)

Further explanation in necessary (no formatting)

Terminal command. Use > to create block text (forum does not seem to support 4 spaces for code)

2 Likes

Use ExifTool to set EXIF date/time on scanned jpg images

Easier method: use Better Finder Attributes

Adjust date, time and filename below as necessary

exiftool “-AllDates=YYYY:MM:DD HH:MM:SS" filename

Turn off ads in Parallels

defaults write com.parallels.Parallels\ Desktop ProductPromo.ForcePromoOff -bool YES

2 Likes

Create Symlink

(easier method: use pathfinder or Symlink Service)

ln -s Source/Folder/or/File Destination/Folder/

Rebuild LaunchServices to remove outdated apps from Open With…

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user

2 Likes

Shutdown Commands

Shutdown immediately

sudo halt

Restart immediately

sudo reboot

Shutdown in 10 minutes

sudo shutdown -h +10

Shutdown at 8 pm

sudo shutdown -h 20:00

Shutdown immediately

sudo shutdown -h now

Reboot immediately

sudo shutdown -r now

1 Like

Dock Commands

Add Mouse-Over Effect

defaults write com.apple.dock mouse-over-hilte-stack -boolean YES

Remove Mouse-Over Effect

defaults write com.apple.dock mouse-over-hilte-stack -boolean NO

“Single App” Mode

defaults write com.apple.dock single-app -bool TRUE; killall Dock

Add a Recents or Favorites Stack

After adding, right-click to select Recent Apps, Recent Docs, Recent Servers, Favorite Volumes, or Favorite Items.

defaults write com.apple.dock persistent-others -array-add ‘{“tile-data” = {“list-type” = 1;}; “tile-type” = “recents-tile”;}’; killall Dock

Add Spacers on Left Side of Dock

defaults write com.apple.dock persistent-apps -array-add ‘{“tile-type”=“spacer-tile”;}’; killall Dock

Add Spacer on Right Side of Dock

defaults write com.apple.dock persistent -others -array-add ‘{tile-data={}; tile-type=“spacer-tile”;}’; killall Dock

Indicate Hidden Apps on Dock

defaults write com.apple.dock showhidden -bool TRUE; killall Dock

Set Dock Autohide Delay to Zero (or other amount)

defaults write com.apple.Dock autohide-delay -float 0 && killall Dock

Restore dock autohide delay to default

defaults delete com.apple.Dock autohide-delay && killall Dock

Scroll Gestures

defaults write com.apple.dock scroll-to-open -bool TRUE; killall Dock

“Suck” Animation

Valid values: suck, genie, scale

defaults write com.apple.dock mineffect suck; killall Dock

Reset All Dock Settings to Default

defaults delete com.apple.dock; killall Dock

Zip File Manipulation

Create Zip File

cd to folder containing files unless you want the full path embedded.

zip -r -X Filename.zip Files

Remove file from Zip

zip -d Filename.zip Files

Working with Hidden Files and Folders

Show hidden files in Finder

defaults write com.apple.finder AppleShowAllFiles -bool true
killall Finder && open /System/Library/CoreServices/Finder.app

Hide hidden files in Finder

defaults write com.apple.finder AppleShowAllFiles -bool false
killall Finder && open /System/Library/CoreServices/Finder.app

Unhide the user Library folder in OS X

chflags nohidden ~/Library/

Starting in High Sierra, need to use xattr command below to allow the no hidden attribute to be set.

xattr -d com.apple.FinderInfo ~/Library
chflags nohidden ~/Library/## Unlock files

chflags -R nouchg filename

Working with .DS_Store Files

Prevent .DS_Store file creation on network volumes

defaults write com.apple.desktopservices DSDontWriteNetworkStores true

Recursively delete .DS_Store files

find /path/to-folder ( -name ‘.DS_Store’ ) -delete

2 Likes

Working with DNS

Determine current DNS Nameserver IP Address

cat /etc/resolv.conf | grep nameserver

Flush the DNS Cache

sudo discoveryutil mdnsflushcache

Quarantine

DANGER, Will Robinson! DANGER, Will Robinson!

Turn off quarantine systemwide

defaults write com.apple.LaunchServices LSQuarantine -bool NO

Turn off quarantine for a specific application

xattr -d -r com.apple.quarantine /Path/to/application/

Mail.app Terminal Commands

Set the minimum display size of fonts in mail.app

defaults write com.apple.mail MinimumHTMLFontSize 13

Disable inline attachments in mail.app

defaults write com.apple.mail DisableInlineAttachmentViewing -bool yes

Set Save Location for Screenshots

defaults write com.apple.screencapture location ~/Desktop/Screenshots; killall SystemUIServer

Application management

Launch application

open -a ApplicationName /file/to/open

In my self built automation system I use that a lot, however I am looking at moving from belt and suspenders stuff into keyboard maestro which would handle that for me

2 Likes

Compare Lists

I often need to compare two lists of things either to find what is missing or remaining or what is common between them. I use the comm command for that. The following examples all use the same two text files (list1.txt and list2.txt) in the same order.

Find items only in list1

comm -23 list1.txt list2.txt

Find items only in list2

comm -13 list1.txt list2.txt

Find items that only appear in both lists

comm -12 list1.txt list2.txt

1 Like

Getting Around/Searching

I tend to find it faster to search and move through my files from the terminal.

Z learns from your history

z Notes

Z
z

Tracks the most used directories and enables quickly navigating to them using string or regex patterns.

- Go to a directory that contains "foo" in the name:
    z foo

- Go to a directory that contains "foo" and then "bar":
    z foo bar

- Go to the highest-ranked directory matching "foo":
    z -r foo

- Go to the most recently accessed directory matching "foo":
    z -t foo

- List all directories in `z`'s database matching "foo":
    z -l foo

- Remove the current directory from `z`'s database:
    z -x .

FZF fuzzy search with preview

fzf --preview=“pygmentize -g {}” java

fzf
fzf

Command line fuzzy finder.

- Start finder on all files from arbitrary locations:
    find path/to/search -type f | fzf

- Start finder on running processes:
    ps aux | fzf

- Select multiple files with `Shift + Tab` and write toa file:
    find path/to/search_files -type f | fzf -m > filename

- Start finder with a given query:
    fzf -q "query"

- Start finder on entries that start with core and end with either go, rb, or py:
    fzf -q "^core go$ | rb$ | py$"

- Start finder on entries that not match pyc and match exactly travis:
    fzf -q "!pyc 'travis"

Ripgrep super fast regex searcher in files and directories

rg -t py coderJSON

rg
ripgrep

A fast command-line search tool.

- Recursively search the current directory for a regex pattern:
    rg pattern

- Search for pattern including all .gitignored and hidden files:
    rg -uu pattern

- Search for a pattern only in a certain filetype (e.g., html, css, etc.):
    rg -t filetype pattern

- Search for a pattern only in a subset of directories:
    rg pattern set_of_subdirs

- Search for a pattern in files matching a glob (e.g., `README.*`):
    rg pattern -g glob

And the one that’s actually my most used command now is a function I made combining fzf and rg and some borrowed code from the web.

fs
# Modified version where you can press
# - CTRL-O to open with `open` command,
# - CTRL-E or Enter key to open with the $EDITOR
# - CTRL-W to search inside files
# - CTRL-C to copy file path to clipboard
# - CTRL-D to cd to directory of file
# - CTRL-N to make a new markdown file.
fs() {
  local out file key
  IFS=$'\n' out=($(fzf -i --preview="pygmentize -g {}" --query="$1" --exit-0 --expect=ctrl-o,ctrl-e,ctrl-w,ctrl-m,ctrl-c,ctrl-d,ctrl-x,ctrl-n --bind '?:toggle-preview'))
  key=$(head -1 <<< "$out")
  file=$(head -2 <<< "$out" | tail -1)
  esfile=$(printf %q "$file")
  if [ -n "$file" ]; then
    [ "$key" = ctrl-o ] && open "$file" ||
    [ "$key" = ctrl-w ] && infile "$1" ||
    [ "$key" = ctrl-c ] && echo "$file" | pbcopy || 
    [ "$key" = ctrl-d ] && cd $(dirname "$file") ||
    [ "$key" = ctrl-n ] && code "$1.md" ||
    ${EDITOR:-code} "$file"
  fi
}

infile() {
  rg "$1" | fzf --height 40% | sed 's/:.*$//g' | sed 's/ /\\ /g' | sed 's/&/\\&/g' | xargs -0 -I {} /bin/zsh -c 'code {}'
}

Video at the bottom, if it doesn’t load, try here

Edit: Cleaned up my function

1 Like

This looks awesome. Immediately in my mind I think of students who have been added or removed from my classes (I have over 300 students spread out over a couple of sections). I would love to be able to do a compare at the end of the week to see who to follow up with or stop worrying about. If I pull lists out and compare them week to week this is what I would use?

Also consider using diff which will create an intelligent (?) output. If the lists aren’t in the same order you might want to use sort first.

FYI Take Control Books has a useful ebook on this subject: TAKE CONTROL OF
the Mac Command Line with Terminal
. $15 (with free download of pdf and epub versions), though TC Books occasionally offers discount coupons…

You can download a pdf sample from the ebook here.