Terminal

Bash

Chaining commands


Background commands

Substitution

Function parameters

Control flow & comparisons

for i in *; do echo "$i"; done
if [[ condition ]]; then
	echo "condition 1 passed"
elif [[ condition2 ]]; then
	echo "condition 2 passed"
else
	echo "both conditions failed"
fi

Wildcards

Hotkeys

Fish

Basics

History recall

String matching

Snippets

Get name of frontmost app (macOS)

set result (string match -r '"LSDisplayName"="(.+)"' (lsappinfo info -only name (lsappinfo front)))
echo $result[2]

Commands

Text search and manipulation

grep

Returns lines that match a word or pattern

Output configuration

awk

Searches files for pattern matches and performs an action based on them

sed

wc (word count)

cat/less

head/tail

sort

uniq

nano

Files & folders

ls

file

stat

find

fd

lsof (list open files)

Find process using port

cp

rsync

ln

Processes

ps

top

top -stats "command,cpu" -l 2 | grep WindowServer | tail -1

pkill

pkill -9 "node"

Network

Simple web server

npx serve@latest out -l 3000
python3 -m http.server 3000

ipconfig

Get list of network adapters

ipconfig getiflist

Get network adapter details

ipconfig getsummary en0

Get IP address

ipconfig getifaddr en0

curl

Send a POST request with a JSON body:

curl -X POST -H "Content-Type: application/json" -d '{"name": "Oscar", "species": "cat"}' https://example.com/api/pets

tldr

xargs

tmux

Keyboard shortcuts

crontab

fzf

SSH Agent

node

npm/yarn

NPM and Yarn

ffmpeg

Note

The build on Homebrew doesn't support webm. Use one of the downloads here instead.

Trim video

Convert video to audio

Extract frames

youtube-dl/yt-dlp

ImageMagick

Input/Output

qlmanage -c public.image -p (magick convert image.png ... - | psub)
magick 'image.gif[0]' image.png
magick 'image.gif[0-3]' image-%d.png # outputs image-0.png thru image-3.png
magick 'image.gif[0,3]' image-%d.png # only frames 0 and 3

Resizing and scaling

magick convert image.png -resize 100x100 resized.png

jq

echo '' | fzf --preview 'jq {q} < filename.json'

Flags

JSON for examples:

{
    "foo": {
        "bar": [
            { "name": "Apple", "color": "Red" },
            { "name": "Orange", "color": "Orange" },
            { "name": "Banana", "color": "Yellow" }
        ]
    }
}

List all values for key in array of objects

.foo.bar | .[].name

pandoc

Important

Pandoc can convert to PDF, but not from PDF.

pandoc file.docx -o file.html

macOS-specific

caffeinate

chflags

Hide/unhide files & folders

See [[#stat]] to check if a file/folder is hidden

chflags hidden file.txt
chflags nohidden file.txt

defaults

Dim Dock icons of hidden apps

defaults write com.apple.Dock showhidden -boolean yes; killall Dock

tccutil

Reset app permissions

osascript -e 'id of app "Name of App"'
sudo tccutil reset All bundle_id

Homebrew

Moom

defaults read com.manytricks.Moom "Grid Spacing: Apply To Edges: Gaps" {0,0,0,75}

PowerShell

Environment Variables

Add or append to environment variable

$Env:VariableName = 'value'
$Env:Path += ';C:\Tools'

Get environment variable

Get-Item Env:VariableName

Snippets

Delete hidden macOS files

$theSource = "E:\"       # <<<<< insert drive here
Get-Childitem $theSource -Include @("._*", ".DS_Store", ".fseventsd", ".TemporaryItems", ".Trashes", ".Spotlight-V100") -Recurse -Force -ErrorAction SilentlyContinue | Remove-Item -Force -Recurse