Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

One tip or trick per answer.

My favorite is

open .

Opens the folder you're currently browsing in Finder. You can also pass URLs, images, documents or else to open.

If you specify a program name with -a you can pass the URL, image, document or folder to that program instead, e.g. open -a Preview image.png, overriding the default program set for the filetype.

Please don't post duplicates. Search in the question like this: inquestion:this ls -l

Mac OS X specific answers only.

share

migrated from superuser.com Dec 26 '10 at 8:40

This question came from our site for computer enthusiasts and power users.

locked by stuffe Mar 25 at 22:21

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

5  
There is a similar thread on Server Fault as well: serverfault.com/questions/7346/… –  Chealion Oct 7 '09 at 23:07
5  
You can use open for everything: URLs, images, documents. I use it everyday. –  olt Jul 6 '10 at 14:48
5  
As an extension to that: open -a Mail filetosend.ext Creates a new Email with the file attached. –  Skade Jul 6 '10 at 16:11
1  
@Nick Bedford: It's very useful. For example, I use the command line to scp a bunch of files down from the server. Then, I use "open ." to open the current folder up in the finder, where I can easily right-click on a file and say "open in excel". –  khedron Jul 12 '10 at 18:44
1  
@Nick Bedford: If you have the folder open in Terminal, open . opens it Finder. It's useful if you want to do something graphical. –  ShreevatsaR Jul 26 '10 at 4:40
show 9 more comments

133 Answers

Hit and hold esc a few seconds to get a list of every possible terminal command on your system, including built-ins, programs on your path, and aliases.

Or, as Martijn pointed out:

Just use instead, you don't need to hold it for a few seconds even. will also complete partially typed commands for you, as well as filenames and command-specific arguments.

A prompt asking if you really want to display all command possibilities will appear. Just answer y to get the command list.

share
5  
Just use <kbd>TAB</kdb> instead, you don't need to hold it for a few seconds even. <kbd>TAB</kdb> will also complete partially typed commands for you, as well as filenames and command-specific arguments. –  Martijn Pieters Jul 26 '10 at 8:50
2  
If you use Zsh the TAB completion enhancements will rock your world. Try zsh and .oh-my-zsh. –  Slomojo Jan 29 '11 at 8:46
show 1 more comment

bcat is a convenient pipe between my always-open terminal (xterm under XQuartz) and my always-open browser.

it sets up a streaming HTTP server for just one process so things like

tar czvf - . | tee bcat

will just stream until the command exits. Man pages need a bit of cleanup:

man bash | col -b | bcat

or just

export MANPAGER='col -b | bcat'
man bash
share
add comment

Auto-complete a command as an argument. for example start to type:

which pyt (now press ++1)

it will complete to

which python

++1 works like tab completion except that it auto-completes using command names instead of file names.

share
show 1 more comment

This is my absolute favorite. Sharing screen captures via the internet is a hassle. I wrote this to make sharing screenshots across chat a one step process using DropBox. (I have subsequently come across apps and utilities that do this, but I think this is perfect, at least for me.)

It starts the interactive screenshot utility (same as ++4), saves it your Dropbox's public folder, copies the URL to your clipboard and opens it in your browser.

I run it via LaunchBar, but you could run it from the shell or bind it to a keyboard shortcut to make it as easy as ++5.

You could add something random to the filename if you are worried about privacy.

I used to have it scp the screenshot file to my webserver before I switched to Dropbox. You could send the file wherever it would be useful to you. You could even put it in your ~/Sites directory to use it on your local network.

If you want sign up for dropbox, you can use my referral link which will give us both bonus storage. =)

#!/bin/sh

# Integrates Mac OS X's screenshot utility with DropBox for easy sharing.

# - Starts the interactive take-screenshot function, saves it to your public
# Dropbox (if you didn't cancel it) where it will be uploaded automatically.
# Copies the public URL to your clipboard and opens your browser to it.

## Config
dropbox_id="112358132134"  ## this is fibonacci's dropbox id
dropbox_public_folder="$HOME/dropbox/Public/screenshots"
upload_delay_in_second=1.5

## Derivative Variables
filename=$(date '+%F__%H-%M-%S.png')
save_to="$dropbox_public_folder/$filename"
url="http://dl.dropbox.com/u/$dropbox_id/screenshots/$filename"

## start interactive screen capture
screencapture -i "$save_to"

## if the screenshot actually saved to a file (user didn't cancel by pressing escape)
if [[ -e "$save_to" ]]; then
    ## echo some output in case you run this in a shell
    echo "Saved screenshot to:" "$save_to"

    ## copy url to the clipboard
    echo "$url" | pbcopy

    ## wait for Dropbox to upload your screenshot, then open in your browser
    sleep $upload_delay_in_second
    ## The `-g` flag means it won't bring your browser to the foreground, which 
    ## feels less like getting interrupted.
    open -g "$url"
fi
share
7  
+1 for disclosing Fibonacci's dropbox id –  iolsmit Jun 25 '12 at 22:44
show 6 more comments

Use ctrl+R to active reverse history search. Then start typing a command you've already typed and all matching commands will start presenting to you.

To navigate in the reverse history search simply:

  • continue typing to narrow down search
  • ctrl+R: move to the next result
  • : go back to the previous result
  • ctrl+C: cancel your search

eg.

apouche:bin>  echo 'type CTRL+R to start reverse search'
(reverse-i-search)`fin': find . -exec grep "MainMenu.nib" {} \;

See also the accepted answer to "How can I search the bash history and rerun a command?" on Super User.

share
show 1 more comment

The more I use it the more addicted to it I am.

screen

Along with

screen -ls
screen -r [session]

Very useful for having several screens open on an SSH connection. It saves tons of time especially when you don't have to restart your tail everytime you want to check another log file.

share
2  
Sadly it's not part of OS X by default but consider tmux. It's basically a better BSD equivalent of GNU screen. –  Peter Cooper Jul 6 '10 at 14:01
show 1 more comment

diskutil is a very powerful command-line tool for working with disks and disk images. It's gotten me out of some binds. It's not too hard to use.

share
add comment

I’m not sure; this might work in any decent terminal application, not only in OS X’s. However:

Using Terminal.app it is possible to put status information to the actual title bar and not just to the prompt.

In order to do that, you need to change the PS1 variable in bash to the following model:

PS1='\[\033]0;TITLE\007\]PROMPT'

Where TITLE and PROMPT must be substituted to the actual commands which provide the status information. For example, \w lists the current full path; \W the directory name. You can even execute a command by putting it in backticks. (So you could even put the output of arbitrary commands to the title – or to the prompt.)

Git users (with git’s bash completion installed) might find the following useful. Add this to your .bashrc

export GIT_PS1_SHOWDIRTYSTATE=1
PS1='\[\033]0;`__git_ps1` \w\007\]\h:\W \u\$ '

and the title bar of Terminal.app will show the current git branch (and whether it’s clean or not) followed by the current full path. This gives useful information about where you are only when you need it and does not make the actual prompt overly long.

In case you don’t use git very much and only care about the path in the title bar:

PS1='\[\033]0;\w\007\]\h:\W \u\$ '
share
2  
As of Mac OS X Lion 10.7, Terminal also supports setting the tab title separately from the window title. "0" sets both. Use "1" to set the tab title, "2" to set the window title. ("1" technically means "icon title", but Terminal uses it for the tab title, since it doesn't have icon titles in the same sense that xterm and X11 do.) –  Chris Page Aug 19 '11 at 11:42
show 2 more comments

I'm going to have to say watching Star Wars from the command line is the best:

telnet towel.blinkenlights.nl

If you say that isn't a command, which it isn't really, just a trick, then I like this:

defaults write com.apple.dashboard devmode YES

share
show 3 more comments

Print almost any document as a PDF, as long as it has a correctly defined MIME type in OS/X

 cupsfilter $filename > output
share
add comment

Here's a shell function to get the path of the front Finder window. Can be handy. (I started doing this instead of dragging a folder into the Terminal window.)

function fp { osascript -e 'tell application "Finder"'\
 -e "if (${1-1} <= (count Finder windows)) then"\
 -e "get POSIX path of (target of window ${1-1} as alias)"\
 -e 'else' -e 'get POSIX path of (desktop as alias)'\
 -e 'end if' -e 'end tell'; };\

## alias to copy it to the clipboard
alias cfp='fp | pbcopy'

(This has been in my zshrc a while, but I don't know where I got it / parts of it, otherwise I'd cite credit.)

share
add comment

Easily burn an ISO from commmand line (with verify burn at the end):

hdiutil burn /path/to/iso

Without verifying the burn:

hdiutil burn -noverifyburn /path/to/iso
share
add comment

Get a list of airport SSID

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -s

the airport utility has a lot more options to manage the airport configuration. Run without the -s to get a list.

share
1  
if you tire of long pathnames like this, try locate airport - you could run it with $(locate airport | grep 'Priv') –  Slomojo Jan 29 '11 at 9:15
add comment

the most interesting pschotherapist you will ever talk to:

  • Run emacs
  • Press +esc+X
  • type doctor and press enter
  • have fun :D
share
11  
That's just part of emacs, it's not OS X specific –  Josh Feb 24 '10 at 18:10
show 2 more comments

alias to open preview from command line

alias preview='groff -Tps > /tmp/tmp.ps && open -a Preview /tmp/tmp.ps'

So you can do :

echo "toto" | preview
cat /tmp/test.log | preview
cheat git | preview
share
show 1 more comment

Putting a couple of these together, we can get manual pages in a browser with proper markup:

bman () {
    gunzip < `man -w $@` | groff -Thtml -man | bcat
}
share
add comment

None of these are exactly OSX specific, but here's some stuff from my .bash_profile that I find useful:

Colored Prompt:

PS1="\[\e[0;31m\][\[\e[1;31m\]\u\[\e[0;34m\]@\h \[\e[32m\]\w\[\e[0;31m]\]\$\[\e[0m\] ";

example

SSH tab completion of hosts that exist in ~/.ssh/config: (found on MacOSXHints)

complete -o default -o nospace -W "$(/usr/bin/env ruby -ne 'puts $_.split(/[,\s]+/)[1..-1].reject{|host| host.match(/\*|\?/)} if $_.match(/^\s*Host\s+/);' < $HOME/.ssh/config)" scp sftp ssh

Highlighted grep:

alias grep="grep --color=auto"

highlighted grep

Automagically dump your public ssh key to a host for future passwordless auth: (can probably easily tweaked to add said host to ~/.ssh/config)

ssh-setup() { cat ~/.ssh/id_rsa.pub | ssh $1 'cat - >> ~/.ssh/authorized_keys'; }

More OSX specific stuff that I've setup forces the machine to take a picture with the built-in iSight every time the machine's lid is open and dumps that image in a directory.
Requirements:

Create a directory somewhere to hold all your images. Dump this into ~/.wakeup:

date=$(date +%y%m%d_%H_%M_%S).jpg;
/PATH/TO/isightcapture -w 640 -h 480 -n 3 -d -t jpg /PATH/TO/PICTURE/DUMP/$date > /dev/null
unset date

I've been capping a frame every time my MacBook wakes up for the past 3 1/2 years now, it's interesting to see everything compiled into a long video at a high framerate.

share
show 1 more comment

Not installed by default, but MacPorts is great for adding more command line programs. After downloading and installing you can use the port command to find and install more programs, plus much more.

port search convert video
port install ffmpeg
share
2  
And homebrew is much better mxcl.github.com/homebrew –  Steve Jun 25 '12 at 13:32
show 1 more comment

My favorite alias:

alias redo='sudo \!-1'

When you forget to use 'sudo', just do 'redo' to rerun the last command using sudo.

share
10  
You can also use !! to mean the last command, so you can do: sudo !! –  Joe Shaw Jul 26 '10 at 13:54
show 1 more comment

Use !$ to repeat the last parameter in the last command you entered, for example:

~$ mkdir test-dir
~$ cd !$
cd test-dir
test-dir$  

!$ is actually short for !!$ which means "from the most recent command, pull the last parameter"

See the "HISTORY EXPANSION" section of the bash man page for more.

share
3  
Similarly, there is !^ for the first command, which I happen to use often. –  Pascal Aug 9 '10 at 9:58
show 1 more comment
afplay ~/path/to/file.mp3

Let's you play songs from the commandline. You can also append [space]& and let it run in the background. :)

share
show 1 more comment

Not a huge feature, but I noticed it wasn't here.

+ mouse drag on Terminal text let's you make a rectangular selection.

share
show 1 more comment

The OSX installer app has a command line interface too.

sudo installer -pkg /Volumes/Growl-1.2.1/Growl.pkg -target LocalSystem

Is a one line install command for Growl, GrowlNotify is an extra on the same install disk image.

You can find the domains supported by a package file via

installer -pkg  /Volumes/Growl-1.2.1/Growl.pkg -dominfo
share
add comment
xattr -h

allows you to view file attributes. The most handy use for this command is to remove the internet download warning from the finder:

cd /the/directory/where/you/downloaded/all/your/files

xattr -rd com.apple.quarantine .
share
add comment
history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort -r

Gives you a list of some of your most recent commands, numbered by how often you use them.

share
add comment

Create a new directory and enter it:

md() { mkdir -p "$@" && cd "$@"; }

For more, see my dotfiles repository on GitHub, and/or view my .osx file for OS X-specific preferences and settings.

share
add comment

Repeat the previous command with a substring replacement:

Syntax:

^before^after^

Example:

You entered:

git clonr https://unbelievablylongurl.org/projectdirectory/evenmoreprojects/project.git

Use this:

^clonr^clone^

And your command will be re-run with the replaced substring:

git clone http://unbelievablylongurl.org/projectdirectory/evenmoreprojects/project.git
share
add comment

You can transfer a working directory from one Terminal window to another with these two commands added to your .bash_profile file:

alias cwd='pwd | pbcopy'
alias gowd='cd "`pbpaste`"'

cwd copies your working directory from one window, and gowd opens that directory in another window.

share
show 1 more comment

In my bash profile I have these aliases:

# Alias for "." shows current directory
alias -- .='pwd'

# Alias for ".." goes to parent directory
alias -- ..="cd .."
alias -- ...="cd .. ; cd .."
alias -- ....="cd .. ; cd .. ; cd .." 
share
1  
Yeah, me too, working on a shell that doesn't have '..' is strange. :) (FWIW, You could also use alias '..'='cd ../..') –  Pascal Aug 9 '10 at 9:56
add comment

Use Apple’s ASCIIMoviePlayer to play QuickTime movies in the Terminal:

(There are also two great adaptations out there that allow using ANSI colour output).

On a more serious note: CoreImageTool (3rd party; just google for it) is a great way of using CoreImage filters from the command line.

share
show 1 more comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.