Shell aliases

Shell aliases

Shell aliases are woefully underused. Painfully, I frequently see people do the following:

  • Type a shell command with the same 4 arguments cd into the same folders
  • ssh into the same machines or devices
  • Run the same sequence of commands

Aliases are super easy. Just open your ~/.bashrc file (or equivalent) and start adding them. I’ll now give a bunch of examples I use.

“ls” command

I have many variations I use on the “ls” command. I don’t even remember what the arguments mean anymore because I’ve been using these shortcuts for so long.

# default arguments I like
alias l='ls -AG'

# Long listing
alias ll='ls -aAlGh'

# Do a grep afterwards (My most often used)
alias lg='ls -AlGOh@ | grep -i '

# Newest files at bottom (My second most often used)
alias lt='ls -AltrGhu'

# Largest files at bottom
alias lz='ls -AlGrhS'

# Extended attributes
alias lx='ls -aAlGh@O'

# kitchen sink
alias le='ls -aAlGh@eOR'

Common sets of flags

When I run “du” or “top” I always use the same flags, so I have this set up:

alias ds='du -chs'
alias t='top -stats pid,command,cpu,time,threads,ports,mem,state,username -o cpu'

Note that you could redefine the meaning of a command (e.g. alias ls = ls -l) but note that if you want to use different arguments, you’re stuck unless you define it as a function (see below).

Paths

If you type the same path at least once a day, just make an alias for it.

alias cr='cd ~/Library/Logs/CrashReporter/MobileDevice/<device name>'
alias dl='cd ~/Library/Logs/CrashReporter/MobileDevice/<device name>/DiagnosticLogs'
alias sd='cd ~/Library/Logs/CrashReporter/MobileDevice/<device name>/DiagnosticLogs/sysdiagnose'

Shortcuts to machines or devices you ssh to often.

If you ssh to other machines often, it’s tedious to type out the command every time, so set up an alias.

alias sandfly='ssh admin@sandfly'
alias trilobite='ssh admin@trilobite'
alias weta='ssh admin@weta'

Complicated commands

For verbose or complicated commands, you’ll quickly tire of typing them over and over.

alias lc='launchctl unload /Library/LaunchAgents/org.cricket.install-xcode; launchctl load /Library/LaunchAgents/org.cricket.inst

Functions

I’ll mostly leave this as an exercise to the reader, but you can create functions in your .bashrc (or equivalent) file that allow you to pass in arguments. For example:

function installbuild () {
sudo asr -source $1 -target $2 -erase -noverify
}

This is one way you could redefine a command so that it always uses your desired set of flags, but also allows you to pass in new flags and override what’s in your function. You could even define a function that always adds flags to the default set when you add the flags on the command line.

Reserve functions for times when the time investment will pay off. It’s easy to get addicted to improving your productivity but make sure it’s worth the investment.