SPECIAL! Pre-Order my book Smashing Node.JS on Amazon today!
Show Posts
← Back to homepage

The default shell for Leopard users is Bash. Although many GNU/Linux users are familiar with it, not all Mac users take full advantage of its power. Here are some very useful commands and tips I use routinely.

Users and login

Use the root superuser

sudo su -

Alter login variables (such as $PATH, $EDITOR)

nano ~/.profile

Other available shells

  • /bin/ksh
  • /bin/tcsh
  • /bin/csh
  • /bin/zsh

Files management

Output the contents of a file

cat /some/thing

Get 20 lines from the end of a file

tail -n 20 /some/thing

Get the first 20 lines of a file

head -n 20 /some/thing

Create an empty file

touch /some/thing

Redirect the output of a command to a file (overwrites)

command > /some/thing

Redirect the output of a command to a file (appends)

command >> /some/thing

Append the timestamp to a file

touch /backups/backup_`date +%s`.txt

Change to the last directory you were in

cd /var
cd /etc
cd - # will take you to /var

List file size in human-readable units

ls -lh /dir/or/file

Available editors

  • /usr/bin/vi
  • /usr/bin/vim
  • /usr/bin/nano

Applications and processes

Run a process in the background

command &

List running processes

ps ax

The first column will be the PID

Kill a process by pid

kill -9

Kill a process or processes by name (e.g: the Dock)

killall Dock

Open an application (Mac-only)

open /Applications/iTunes.app/

About Guillermo Rauch:

CTO and co-founder of LearnBoost, developer, open source enthusiast, blogger.