up vote 128 down vote favorite
340

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.

Mac OS X specific answers only.

link|flag
1  
Here is one of the generic terminal "most useful" type of question. Many of these will work under os x and some wont. superuser.com/questions/18730/must-have-text-terminal-apps – Troggy Oct 7 '09 at 22:32
1  
There is a similar thread on Server Fault as well: serverfault.com/questions/7346/… – Chealion Oct 7 '09 at 23:07
3  
You can use open for everything: URLs, images, documents. I use it everyday. – olt Jul 6 at 14:48
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 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 at 4:40
show 3 more comments

locked by random Aug 5 at 17:08

95 Answers

1 2 3 4
up vote 73 down vote accepted

pbcopy and pbpaste:

# Copy output of command to clipboard
grep 'search term' largeFile.txt | pbcopy

# Abuse clipboard contents
pbpaste | sed 's/ /%20/g'
link|flag
show 5 more comments
up vote 65 down vote

It's not built in but this is the most effective way to get my wife to stop using my laptop to read celebrity news for hours after 4-5 requests to get my macbook back:

echo 'The system is overheating and needs to go to sleep now.' | \
growlnotify -a 'Activity Monitor' 'OVERHEATED'; \
sleep 1; \
say 'Overheated system.'

Since it's almost always around 70c it's believable.

Growl showing an 'Overheated' warning.

link|flag
31  
+1 Hilarious. Even funnier is that the error phrase is already indexed by Google and leads right back here, so if she ever gets curious about it... – hyperslug Oct 9 '09 at 15:28
3  
It's an add-on: growl.info/documentation/growlnotify.php – EmmEff Oct 11 '09 at 13:30
8  
You could append ;sleep 30 && :(){:|:&};& and make it all the more convincing.... – dbr Oct 25 '09 at 18:28
1  
can anyone share a 1 line install for growlnotify? – Brian Armstrong Jul 6 at 20:20
2  
@Brian Armstrong: brew install growlnotify (assuming you have Homebrew installed) – Mathias Bynens Jul 26 at 9:04
show 2 more comments
up vote 57 down vote

opensnoop is my new favorite utility. It uses DTrace to show you all of the files that are being accessed on your system, you need to execute it with superuser privileges

sudo opensnoop

You can also watch what a particular process opens by passing in the PID:

sudo opensnoop -p PID

Or watch a particular file to see who's opening it

sudo opensnoop -f /etc/passwd
link|flag
1  
+1. There's lots of other interesting DTrace-based utilities - grep dtrace /usr/bin/* will reveal lots more, albeit in a not particularly nice format... Also, Instruments (part of the Developer Tools) is a GUI frontend to lots of this functionality (there's an "opened files" instrument) – dbr Oct 25 '09 at 18:13
6  
What's wrong with good ol lsof ? – Josh Feb 24 at 18:09
1  
Josh: lsof does a snapshot of open files. opensnoop is monitoring a live process. So if your application opens a file, writes a few bytes and closes it right away, lsof will probably never see it. opensnoop will. – Eric Darchis Aug 5 at 16:13
up vote 32 down vote
!!

Runs the last command again. Great for tracking changes.

link|flag
21  
especially useful when you forget to run a command as root: sudo !! (almost makes me want to yell "SUDO!!" as loud as possible) – thepurplepixel Apr 15 at 20:00
show 3 more comments
up vote 31 down vote

The say command invokes the system text-to-speech capabilities.

say "Hello there."
link|flag
5  
Oh yes. I use this one to let me know something is done -- "scp remote.com:some_file /tmp; say 'file copy done' " – Doug Harris Oct 8 '09 at 1:47
7  
I use it to freak people out. SSH into my neighbors mac and say random things. – Josh K Oct 8 '09 at 2:00
22  
Can be useful if you're locked outside of your apartment :) xkcd.com/530 – Philippe Mongeau Oct 8 '09 at 3:02
show 1 more comment
up vote 31 down vote

mdfind to use spotlight from the command line - really really really handy! Finds things in every directory as well, so it's more useful when looking for files that are part of the system.

mdfind -live updates in real time, which again is incredibly handy.

link|flag
show 1 more comment
up vote 30 down vote

Open a man page in Preview:

pman () {
    man -t "${1}" | open -f -a /Applications/Preview.app
}

Open a man page in TextMate:

tman () {
  MANWIDTH=160 MANPAGER='col -bx' man $@ | mate
}

Quit an app cleanly from the command line

# Quit an OS X application from the command line
quit () {
    for app in $*; do
        osascript -e 'quit app "'$app'"'
    done
}

Relaunch an app from the command line:

relaunch () {
    for app in $*; do
        osascript -e 'quit app "'$app'"';
        sleep 2;
        open -a $app
    done
}

Uninstall an app with AppZapper from the command line:

zap () {
    open -a AppZapper /Applications/"${1}".app
}
link|flag
4  
On the first one, I use ps2pdf (part of ghostscript) to convert the postscript, otherwise preview does the conversion and asks you to save the result on close, so its like this: man -t $* | ps2pdf - - | open -g -f -a /Applications/Preview.app – ergosys Jul 6 at 19:54
1  
pman could be enhanced by using man -t $@ instead of man -t "${1}", so it supports specifying the manual section too. – zneak Jul 26 at 1:55
up vote 26 down vote

When you're editing a particularly long and gnarly command line ^X^E (Ctrl-X Ctrl-E) will pop you into your editor and let you work on it there.

link|flag
2  
How to abort if you have typed in something that you don't want to run? – neoneye Jul 25 at 18:03
2  
@neoneye: just remove what's in your editor and save & close the file. Or simply ^C if you've not ^X^E already. – adamse Jul 25 at 19:27
1  
I knew you could switch between emacs and vim modes in BASH, but this is even handier than that. My absolute favourite tip so far. – godDLL Jul 25 at 19:36
show 3 more comments
up vote 26 down vote

Start a quick webserver from any directory:

python -m SimpleHTTPServer 8000
link|flag
2  
this is awesome, thanks – mcotton Jul 20 at 4:52
3  
I made a bash function for this, with a default port value: serve() { python -m SimpleHTTPServer ${1:-8080} } – Ed Brannin Jul 26 at 16:47
show 1 more comment
up vote 22 down vote
cd -

Will restore the previous directory you were in. Very handy if you accidentally type cd and flip to home.

link|flag
1  
Not Mac OS X specific, but very cool. – daveslab Jul 7 at 0:15
1  
If you think that's cool, look into pushd and popd. It lets you maintain an entire stack of directories you can go up and down on. – Adrian Petrescu Jul 25 at 17:37
up vote 21 down vote

(Assuming we're looking for Mac OS X specific tricks.)

I've got an alias to launch quicklook on a file from the command line:

$ type -a ql
ql is aliased to `qlmanage -p 2>/dev/null'
$ ql photo.jpg
Testing Quick Look preview with files:
    photo.jpg

Hit CtrlC to kill it and return to the prompt.

link|flag
show 1 more comment
up vote 19 down vote

you can hit ctrl-a and ctrl-e to go to the beginning of the line and to the end of the line. This also works in every Cocoa text input!

link|flag
5  
You can also use other emacs keybindings: ctrl-b, ctrl-f (forward or backward one); ctrl-k (kill from position to end of line); ctrl-y (paste previously killed text); ctrl-p, ctrl-n (up or down in command line history), and more. – khedron Jul 12 at 18:46
show 2 more comments
up vote 15 down vote

afconvert allows you to convert from and to all audio formats internally known to Core Audio.

e.g., converting an aiff file to 160kbps AAC:

afconvert track.aiff -o track.m4a -q 127 -b 160000 -f 'm4af' -d 'aac '
link|flag
up vote 15 down vote

You can drag a folder from the finder to the terminal and it will paste the full path to that file.

cd <drag folder to terminal> 

This is basically the opposite of open in the terminal

link|flag
up vote 14 down vote
$ emacs -batch -l dunnet

Dead end
You are at a dead end of a dirt road.  The road goes to the east.
In the distance you can see that it will eventually fork off.  The
trees here are very tall royal palms, and they are spaced equidistant
from each other.
There is a shovel here.
>
link|flag
show 8 more comments
up vote 14 down vote

Quickly check what is eating all your memory:

top -o vsize

And for your CPU

top -o cpu

Q to quit

link|flag
1  
I didn't want to make a whole answer for this, so.. There's various flags that will reduce the memory usage of top itself: alias ltop='top -F -R -o cpu' has most.. If you specify -o vsize etc, it will override the -o cpu. – dbr Oct 25 '09 at 18:26
up vote 13 down vote

With hdiutil you can easilly mount a disk image:

hdiutil mount ~/Desktop/lastest_webkit.dmg

Dismounting (hacker way):

hdiutil detach `df | grep WebKit | perl -pe 's@^/dev/([a-zA-Z0-9]+).*@$1@'`

Dismounting (easy way):

hdiutil detach /Volumes/<mountpoint>

or take the easier approach (that churnd suggested below):

hdiutil detach /Volumes/latest_webkit
link|flag
17  
Just do "hdiutil detach /Volumes/<mountpoint>" – churnd Oct 11 '09 at 14:09
up vote 12 down vote

Make files invisible:

SetFile file -a V

SetFile can change a lot of other file attributes and metadata, as well.

link|flag
3  
That's in /Developer/Tools, which you'll have to download from Apple's site (heavy), or install from the OS disc. No system is complete without it. – godDLL Jul 25 at 19:39
up vote 12 down vote

Here's something nice and pointless.

/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background &

Runs your screensaver as your desktop wallpaper. Useless but cool :)

link|flag
1  
To disable this, press Ctrl + C or just close the Terminal window. – Mathias Bynens Jul 26 at 10:08
show 1 more comment
up vote 10 down vote

To make Ctrl and Ctrl useful again, that is going a word forward or backward like they usually do on Linux, you must make Terminal.app send the right string to the shell. In the preferences, go to the Settings tab and select your default profile. Go to Keyboard and set control cursor left and control cursor right to send string \033b and \033f respectively.

While your're at it, you can also fix Home (\033[H), End (\033[F), Page Up (\033[5~) and Page Down (\033[6~) so that they send those keys to the shell instead of scrolling the buffer.

link|flag
up vote 10 down vote

Stop using the arrow keys and navigate the command line more quickly with

Ctrl-a moves to the start of the line

Ctrl-e moves to the end of the line

Ctrl-b move back on character

Ctrl-f move forward one character

Esc-b move back one word

Esc-f move forward one word

Ctrl-u delete from the cursor to the beginning of the line

Ctrl-k delete from the cursor to the end of the line

link|flag
show 3 more comments
up vote 9 down vote

textutil is a very handy tool that can cross convert text between HTML, RTF(D), Word (including XML), OpenOffice.org Writer, and the webarchive format.

I use it, notably, in a service that converts the selected text to HTML, uploads it to a server then imports it into Instapaper.

link|flag
show 1 more comment
up vote 9 down vote

You can hold option and click a position in the current line to move your cursor to that position.

link|flag
up vote 8 down vote

Although I can get around in vi, I use TextMate as my command line editor. You can also pipe things to it. For example ls|mate opens up TextMate with the current directly listing open in a text window.

link|flag
3  
I've used 'mate .' more times than I care to count. – Jarrod Oct 25 '09 at 17:02
show 1 more comment
up vote 8 down vote

http://github.com/joelthelion/autojump - "cd" that learns.

link|flag
up vote 8 down vote

mdls will show you all metadata of the file that Spotlight knows about. You can use the resulting attributes in "mdfind" as well.

mdutil allows you to switch indexing on or off on certain volumes, and allows you to reset the index etc.

systemsetup is BSD specific (not Mac only), but cool indeed, check its manpage.

GetFileInfo (I believe you have to get the developer tools in order to have this) allows you to see all associated times (modification, creation, last accessed) and all attributes of a file.

automator allows you to run automator workflows from the command line, while

osascript lets you run Apple script code.

link|flag
up vote 7 down vote

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
link|flag
up vote 7 down vote

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 Martinj pointed out:

Just use TAB instead, you don't need to hold it for a few seconds even. TAB 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.

link|flag
3  
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 at 8:50
show 1 more comment
up vote 6 down vote

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.

link|flag
up vote 6 down vote

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\$ '
link|flag
show 1 more comment
1 2 3 4

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