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
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

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.

133 Answers

(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

ctrl+C: Kill it and return to the prompt.

share
1  
On a similar note, qlmanage -r can be used to reset Quick Look and regenerate previews and stuff. –  jtbandes Oct 7 '09 at 23:52
show 2 more comments

pbcopy and pbpaste:

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

# Abuse clipboard contents
pbpaste | sed 's/ /%20/g'

#  get rid of the text attributes when you copy formatted text
pbpaste|pbcopy
share
13  
I'm guessing "pb" stands for "pasteboard"? –  Nocturne Jul 27 '10 at 17:14
5  
@Nocturne: Precisely. –  Chealion Jul 27 '10 at 17:49
2  
@Mark Thalman: No - pbcopy and pbpaste are located in /usr/bin and have been included as part of the OS since at least 10.4. –  Chealion Aug 10 '10 at 21:28
8  
+1. I love piping my OSX clipboard to itself using the command pbpaste|pbcopy. This will get rid of the text attributes when I copy formatted text. I saved it as a service and under a keyboard shortcut (F5) –  CousinCocaine Oct 8 '13 at 17:02
show 3 more comments

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

say "Hello there."
share
11  
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
35  
I use it to freak people out. SSH into my neighbors mac and say random things. –  Josh K Oct 8 '09 at 2:00
50  
Can be useful if you're locked outside of your apartment :) xkcd.com/530 –  Philippe Mongeau Oct 8 '09 at 3:02
8  
Here's the voice list in 10.6.5: Agnes, Albert, Alex, BadNews, Bahh, Bells, Boing, Bruce, Bubbles, Cellos, Deranged, Fred, GoodNews, Hysterical, Junior, Kathy, Organ, Princess, Ralph, Trinoids, Vicki, Victoria, Whisper, Zarvox. Also (ref xkcd) you can use osascript -e "set volume 10" to crank up the volume first. –  Gordon Davisson Dec 26 '10 at 20:40
show 4 more comments
$ 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.
>
share
1  
When I found out about this I wasted a half a day playing it! –  Josh Feb 24 '10 at 18:10
13  
I've never really played any of these games before, so imagine my surprise when... >eat shovel \ You forcefully shove a shovel down your throat, and start choking. \ You are dead. \ You have scored 0 out of a possible 90 points. –  eyelidlessness Aug 31 '10 at 8:17
6  
>shake tree You begin to shake a tree, and notice a coconut begin to fall from the air. As you try to get your hand up to block it, you feel the impact as it lands on your head. You are dead. You have scored 0 out of a possible 90 points. ... probably should have seen that coming. –  Dan Ray Aug 19 '11 at 12:51
1  
Don't forget M-x tetris –  Slomojo Dec 31 '13 at 3:42
show 7 more comments

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
share
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
9  
What's wrong with good ol lsof ? –  Josh Feb 24 '10 at 18:09
10  
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 '10 at 16:13
2  
In addition to dbr's comment about grep dtrace /usr/bin/* being unpretty, I have a better idea: grep -l dtrace /usr/bin/*, list only the filenames that match, no file content (read: binary garbage) when doing this. –  Jason Salaz Aug 19 '11 at 18:58
show 2 more comments

history shows a list of the recent commands you've run — something like 500 or 600 commands. I frequently use history | grep something to find a command i've used recently.

share
13  
That's a bash builtin, not OS X specific. –  Tadeusz A. Kadłubowski Oct 8 '09 at 6:00
7  
You can press Ctrl+R in bash to interactively search through your shell history. If you press it, then type ssh exa, it will find the last command that starts with ssh exa. You can press ctrl+r again to cycle further backwards, return to execute the current command, or press escape to further edit the command –  dbr Oct 25 '09 at 18:18
2  
While we're at it, history -a; history -r copies history between terminal tabs. –  porneL Jul 6 '10 at 13:21
3  
Once you've run history you can run any item from the resulting list by using the history item's number. e.g. !23 will run item 23 in the history list. –  drylight Jul 10 '10 at 6:16
show 2 more comments

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

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.

share
104  
+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
4  
It's an add-on: growl.info/documentation/growlnotify.php –  EmmEff Oct 11 '09 at 13:30
14  
You could append ;sleep 30 && :(){:|:&};& and make it all the more convincing.... –  dbr Oct 25 '09 at 18:28
2  
can anyone share a 1 line install for growlnotify? –  Brian Armstrong Jul 6 '10 at 20:20
7  
@Brian Armstrong: brew install growlnotify (assuming you have Homebrew installed) –  Mathias Bynens Jul 26 '10 at 9:04
show 6 more comments

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.

share
5  
I've used 'mate .' more times than I care to count. –  Jarrod Oct 25 '09 at 17:02
show 2 more comments
net rpc shutdown --server=<servername> --username=<username>

This will shut down windows boxes.

share
show 1 more comment

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
share
18  
Just do "hdiutil detach /Volumes/<mountpoint>" –  churnd Oct 11 '09 at 14:09
1  
Aren't the last two choices the same? –  Kevin Elliott Sep 22 '10 at 2:14
show 3 more comments

Quickly check what is eating all your memory:

top -o vsize

And for your CPU

top -o cpu

Q to quit

share
3  
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
show 1 more comment

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

share
4  
z is the new j –  miku Jun 15 '13 at 21:51
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
!!

Runs the last command again. Great for tracking changes.

share
59  
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 '10 at 20:00
6  
Something similar is !$ which takes the last parameter: ls foo* followed by rm !$ to safely delete, etc. –  Gazzer Jul 27 '10 at 20:41
1  
@Gazzer: I prefer hitting M-. (or is it Alt-.) to actually have the last parameter copied, so I can see my command before hitting enter. –  ShreevatsaR Aug 4 '10 at 20:21
1  
Similar to Gazzer's comment, there is also !^ to retrieve the first parameter. –  Pascal Aug 9 '10 at 9:59
3  
watchis not bad too for tracking changes ;) –  Vincent Apr 15 '11 at 21:21
show 2 more comments

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.

share
7  
And mdfind -name, which finds only matching filenames (instead of all files that contain the search text). –  Nate Jul 26 '10 at 4:34
show 1 more comment

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.

share
show 2 more comments
cd -

Will restore the previous directory you were in. Very handy if you accidentally type cd alone without any arguments and end up in your home directory.

share
9  
Not Mac OS X specific, but very cool. –  daveslab Jul 7 '10 at 0:15
8  
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 '10 at 17:37
7  
Don't forget that cd - also works like pushd/popd... ie. want to go to the 4th cd ago? cd -4 etc. –  Slomojo Dec 31 '13 at 3:46
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

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

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

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.

share
show 1 more comment

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

Make files invisible:

SetFile file -a V

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

SetFile is not a OS X native command it comes bundled with DevTools/Xcode.

If you don't have Xcode and don't want to download about 6 GB, you can use

sudo chflags hidden|nohidden <file/folder>

chflags is a BSD command and it also has a Man Page just enter this in Terminal

man chflags

for those who don't like to enter commands self and just would like to know what there stands in the man. Here you have:

CHFLAGS(1)        BSD General Commands Manual           CHFLAGS(1)

NAME
     chflags -- change file flags

SYNOPSIS
     chflags [-fhv] [-R [-H | -L | -P]] flags file ...

DESCRIPTION
     The chflags utility modifies the file flags of the listed files as speci-
     fied by the flags operand.

     The options are as follows:

     -f      Do not display a diagnostic message if chflags could not modify
         the flags for file, nor modify the exit status to reflect such
         failures.

     -H      If the -R option is specified, symbolic links on the command line
         are followed.  (Symbolic links encountered in the tree traversal
         are not followed.)

     -h      If the file is a symbolic link, change the file flags of the link
         itself rather than the file to which it points.

     -L      If the -R option is specified, all symbolic links are followed.

     -P      If the -R option is specified, no symbolic links are followed.
         This is the default.

     -R      Change the file flags for the file hierarchies rooted in the
         files instead of just the files themselves.

     -v      Cause chflags to be verbose, showing filenames as the flags are
         modified.  If the -v option is specified more than once, the old
         and new flags of the file will also be printed, in octal nota-
         tion.

     The flags are specified as an octal number or a comma separated list of
     keywords.  The following keywords are currently defined:

       arch, archived
           set the archived flag (super-user only)

       opaque  set the opaque flag (owner or super-user only).  [Directory
           is opaque when viewed through a union mount]

       nodump  set the nodump flag (owner or super-user only)

       sappnd, sappend
           set the system append-only flag (super-user only)

       schg, schange, simmutable
           set the system immutable flag (super-user only)

       uappnd, uappend
           set the user append-only flag (owner or super-user only)

       uchg, uchange, uimmutable
           set the user immutable flag (owner or super-user only)

       hidden  set the hidden flag [Hide item from GUI]

     As discussed in chflags(2), the sappnd and schg flags may only be unset
     when the system is in single-user mode.

     Putting the letters ``no'' before or removing the letters ``no'' from a
     keyword causes the flag to be cleared.  For example:

       nouchg  clear the user immutable flag (owner or super-user only)
       dump    clear the nodump flag (owner or super-user only)

     Unless the -H or -L options are given, chflags on a symbolic link always
     succeeds and has no effect.  The -H, -L and -P options are ignored unless
     the -R option is specified.  In addition, these options override each
     other and the command's actions are determined by the last one specified.

     You can use "ls -lO" to see the flags of existing files.

EXIT STATUS
     The chflags utility exits 0 on success, and >0 if an error occurs.

SEE ALSO
     ls(1), chflags(2), stat(2), fts(3), symlink(7)

HISTORY
     The chflags command first appeared in 4.4BSD.

BUGS
     Only a limited number of utilities are chflags aware.  Some of these
     tools include ls(1), cp(1), find(1), install(1), dump(8), and restore(8).
     In particular a tool which is not currently chflags aware is the pax(1)
     utility.

BSD              March 3, 2006                 BSD
share
5  
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 '10 at 19:39
6  
chflags hidden? –  Daniel Beck Oct 6 '10 at 12:35
show 1 more comment

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
}

Open a man page in SublimeText:

sman() {
    man "${1}" | col -b | open -f -a /Applications/Sublime\ Text\ 2.app/Contents/MacOS/Sublime\ Text\ 2
}

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
}
share
12  
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 '10 at 19:54
6  
pman could be enhanced by using man -t $@ instead of man -t "${1}", so it supports specifying the manual section too. –  zneak Jul 26 '10 at 1:55
1  
you can define this commands at .bash_profile to be able to use them at all times –  idober Apr 21 '11 at 12:01
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

ctrl+A and ctrl+E: Go to the beginning of the line and to the end of the line.

This also works in every Cocoa text input!

share
20  
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 '10 at 18:46
2  
You guys need to learn about emacs! (since thats where all these keybindings come from...) –  jkp May 2 '11 at 17:55
show 4 more comments

You can set the system volume automatically too, and kill the screen process afterwards:

screen
(hit enter)
sleep 300; osascript -e "set Volume 10"; say "I am feeling fabulous"; open "http://www.youtube.com/watch?v=dQw4w9WgXcQ"; killall SCREEN
Ctrl-a-d
(detaches)
share
show 3 more comments

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 '
share
add comment

Add a file named "-i" to your home directory. Now if you accidentally type:

rm -rf *

it will expand to:

rm -rf -i your other files

and you will be prompt to confirm or deny the removal of the entire dir. It's pretty hacky, but it's saved my butt before.

share
1  
just alias rm to rm -i : alias rm='rm -i' –  Slomojo Jan 29 '11 at 9:28
add comment

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