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

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

share
3  
Nice, never knew about this one. Very handy. –  scottishwildcat Mar 23 '12 at 14:09
4  
This also allows you to select and copy rectangular sections from Terminal. –  Sergio Acosta Mar 29 '12 at 5:58
2  
:D I voted this up and forgot about it, so I was thinking "Wow! This is cool, I need to upvote this!" and then I saw that I already had. Thanks again! –  CoffeeRain Apr 23 '12 at 18:53
show 7 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

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

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

Start a quick webserver from any directory:

python -m SimpleHTTPServer 8000
share
2  
this is awesome, thanks –  mcotton Jul 20 '10 at 4:52
14  
I made a bash function for this, with a default port value: serve() { python -m SimpleHTTPServer ${1:-8080} } –  Ed Brannin Jul 26 '10 at 16:47
3  
This is the greatest thing in the world. –  Joseph Silvashy Jul 26 '10 at 18:43
1  
I think this is cool. But how do I let others use it? I have to open up my fire wall, and then point them to my IP? How do I do these? –  John Berryman Mar 1 '11 at 12:09
3  
I'm pretty sure this is not OSX specific .. –  hasenj Jan 14 at 15:49
show 4 more comments

When you're editing a particularly long and gnarly command line:
ctrl+X, ctrl+E will pop you into your editor and let you work on it there.

share
3  
How to abort if you have typed in something that you don't want to run? –  neoneye Jul 25 '10 at 18:03
3  
@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 '10 at 19:27
3  
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 '10 at 19:36
1  
This is awesome, thanks for posting this. This is a bash feature, so it'll work anywhere you have bash. (Also explains why it doesn't work in zsh.) –  oyvindio Sep 12 '10 at 15:07
2  
@slomojo: use something like this in your zsh config # Bash-like command editing autoload -U edit-command-line; zle -N edit-command-line; bindkey '^X^e' edit-command-line; –  jkp May 2 '11 at 17:51
show 8 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
!!

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

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

ctrl+W: delete from the cursor to the beginning of the current word

share
14  
set -o vi, then hit escape as usual to switch to command mode. bash uses readline, which has a vi mode. –  mjs Jul 26 '10 at 19:48
1  
These are "emacs" keybindings used by libreadline, and they're almost all available in any text box widget in OSX –  jtimberman May 19 '11 at 3:39
5  
Note that Esc, b and Esc, f (back/forward one word) are bound to Opt-b/f (when you set the terminal to recognize Opt as Meta) –  Slomojo Aug 28 '11 at 23:32
show 6 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

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

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

share
3  
D&D is also very usefull for files to be used as parameters –  Arne Burmeister Feb 22 '11 at 9:27
show 3 more comments

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.

This does not affect normal operation of the screensaver, but will end after normal screensaver has been activated, either by timeout or by moving the mouse to a predefined hot corner.

Alternatively, you can use:

killall ScreenSaverEngine
share
3  
To disable this, press Ctrl + C or just close the Terminal window. –  Mathias Bynens Jul 26 '10 at 10:08
1  
This is great for Quartz Composer animations! –  CoffeeRain Apr 23 '12 at 18:54
show 3 more comments

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

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

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

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

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.

share
show 2 more comments

Resample image so height and width aren't greater than specified size, e.g. 100x100:

sips -Z 100x100 image.jpg

sips supports other operations such as: flip, rotate, crop, image properties query, colour profile query and modification. Check man sips for usage.

share
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

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

Some useful aliases:

alias ..="cd .."
alias ...="cd .. ; cd .."

alias ls="ls -G" # list
alias la="ls -Ga" # list all, includes dot files
alias ll="ls -Gl" # long list, excludes dot files
alias lla="ls -Gla" # long list all, includes dot files

alias stfu="osascript -e 'set volume output muted true'"
alias pumpitup="sudo osascript -e 'set volume 10'"

# Get readable list of network IPs
alias ips="ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'"
alias myip="dig +short myip.opendns.com @resolver1.opendns.com"
alias flush="dscacheutil -flushcache" # Flush DNS cache

alias gzip="gzip -9n" # set strongest compression level as ‘default’ for gzip
alias ping="ping -c 5" # ping 5 times ‘by default’
alias ql="qlmanage -p 2>/dev/null" # preview a file using QuickLook

# Upload image to Imgur and return its URL. Get API key at http://imgur.com/register/api_anon
imgur() { curl -F "image=@$1" -F "key=ANONYMOUS_IMGUR_API_KEY" https://api.imgur.com/2/upload | egrep -o "<original>.+?</original>" | egrep -o "http://imgur\.com/[^<]+" | sed "s/imgur.com/i.imgur.com/" | tee >(pbcopy); }

All of these are in my ~/.bash_profile so I can use them in every Terminal window.

P.S.

alias chpwn="chown"

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

share
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

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

This one isn't an every day usage - but it was a big time saver once - I had a SMB fileserver (Avid Unity) that was displaying lots of .filename files for mac users as well as PC users.

This cleaning command totally fixed the problem (after running twice)

share
3  
its great to finally know theres a good way to do this. –  jkp May 2 '11 at 18:13
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

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

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

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