Tell me more ×
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|improve this question
4  
There is a similar thread on Server Fault as well: serverfault.com/questions/7346/… – Chealion Oct 7 '09 at 23:07
4  
You can use open for everything: URLs, images, documents. I use it everyday. – olt Jul 6 '10 at 14:48
1  
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

118 Answers

1 2 3 4

LAME encode .wav to .mp3

This is the original one-liner I used to eventually craft this handy command.

find ./ -name "*.wav" -execdir lame -V 3 -q 0 {} \;
  • Converts 20Mb .wav (at the highest quality settings) to .mp3 in 3 seconds!
  • Simply install the LAME binary and you're golden.
share|improve this answer

user42053 mentions adding a file -i to every folder. Gets a bit hairy seeing -i in every folder everywhere else. Easier method would be

alias rm="rm -i"
share|improve this answer
2  
This is a baaaaad idea since you'll get too used to this, what if you're on a system without it and you do a careless rm -rf? – Mark Szymanski Aug 3 '11 at 19:34

In your ~/.bash_profile

export PS1="\[\e]2;\h - \w\a\e[32;1m\]%\[\e[0m\] "

This puts your machine name and current directory in the terminal title bar, so you can keep track of where you are. This also shows the data in the Window directory.

share|improve this answer

You can browse and search the history by using the cursor keys after adding

bind '"\e[A": history-search-backward'

bind '"\e[B": history-search-forward'

to your .profile.

share|improve this answer

ctrl-R will allow you to perform a reverse search within your bash shell. It's like an interactive form of history.

share|improve this answer

As of Mac OS X Lion 10.7, Terminal supports a few "less"-compatible pager commands when there are no processes running in a terminal. This is useful for paging through and reading text after commands have completed/exited. Supported keys are:

space: Page Down
+Space: Page Up
: Scroll down one line
/: Scroll up/down one line
F: Page down ("forward")
B: Page up ("back")
<: Home (scroll to top)
>: End (scroll to end)

Terminal has commands that will lookup and display man pages, which these keys are indispensable for viewing. See the Help menu and contextual menus. It also supports Services for opening man pages from other applications (enable them in
System Preferences > Keyboard > Keyboard Shortcuts > Services).

share|improve this answer

A relevant command for Terminal.app on Mac OS X is to launch Software Update from the CLI:

sudo softwareupdate -i -a

The bonus is you do not get any nagging from having to click on windows. I run this as part of a update script that is run every week approximately (so that I do not miss the feedback as it may happen when doing this automatically).

share|improve this answer
show 3 more comments

Change directory to the directory shown in the top-most Finder window:

cdf () {
   currFolderPath=$( /usr/bin/osascript <<-EOT
       tell application "Finder"
           try
               set currFolder to (folder of the front window as alias)
           on error
               set currFolder to (path to desktop folder as alias)
           end try
           POSIX path of currFolder
       end tell
       EOT
   )
   echo "cd to \"$currFolderPath\""
   cd "$currFolderPath"
}

Another version:

f() {
    cd "$(osascript -e 'try
tell app "Finder" to (target of Finder window 1) as text
POSIX path of result
on error
    (system attribute "HOME") & "/Desktop"
end')"
}
share|improve this answer
show 1 more 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|improve this answer
show 1 more comment

BBEdit version for viewing man pages:

bbman () {
  MANWIDTH=160 MANPAGER='col -bx' man $@ | bbedit --clean
}
share|improve this answer

Automatically update the Terminal.app window title to display your username, host and current directory.

If I do the following:

cd ~/Developer

I want the Terminal window title to be updated to:

jason@rocksteady:~/Developer

To achieve this, make sure that the PROMPT_COMMAND variable is set in your ~/.bash_profile:

PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'

Of course, you can substitute whatever suits your fancy.

share|improve this answer
show 1 more comment

Set bash to exit a script immediately on any error.

set -o errexit

Always a good idea when developing bash scripts, especially destructive ones.

share|improve this answer

An easier way to open and close DMGs is:

open <My disk image>.dmg

Then to close it:

umount /Volumes/<My disk image>
share|improve this answer
show 1 more comment

To connect to a network volume, you could use mkdir;mount and umount;rmdir etc, etc... however that's long-winded and there's a better Mac specific alternative method...

You can open a share:

open afp://user:pass@server/sharepointname

and eject it with:

diskutil eject /Volumes/sharepointname

By the way, you can also open a dialog to select from a list of all the sharepoints on a server by doing..

open afp://user:pass@server/

Omit the user/password to prompt for credentials in the GUI.

share|improve this answer

List all directories alone in a directory - er - folders in a folder.

ls -la | grep '^d'

Find sizes of given directory - again - er - folder.

du -s dirname
share|improve this answer

Drag the proxy icon of a Finder window to get an escaped path; especially useful after typing cd

share|improve this answer
show 2 more comments

I made an alias called dirstat, named for a similar utility. It helps determine where all the hard drive space is being used. Add it to your /etc/bashrc or as a bash script.

du -s ./* | sort -n| cut -f 2-|xargs -i du -sh {}
share|improve this answer

I've added the lines below to my ~/.bash_profile file.

With the you will see the effect by starting a command/path and hitting a few times.

Change function of to scroll through autocomplete options:

bind '"\t":menu-complete'

SSH as root to...:

alias shroot="ssh -l root"

Force eject volumes:

alias forceeject="hdiutil detach -force"

Force quit an application:

alias forcequit="killall -HUP"

Open man pages as PDFs:

pdfman() { man $1 -t | open -f -a Preview; };

Alias pingburst:

alias ping2="ping -c2"
share|improve this answer
show 1 more comment

You can use esc key as replacement for alt. >ou have to tap it first and then enter the other instead of holding it. It's a standard feature, but more important as the key on macs works different as on "windows/Linux" keyboards.

You can set the behavior of alt key in preferences to behave like on "windows" keyboards. Though you then will be unable to type important characters as @, \, {, ...

Very important if you use emacs in terminal. But suppose there are many commands that require it - eg you can also copy-paste in bash with emacs bindings.

share|improve this answer
show 1 more comment

I find it useful to copy text to the clipboard from Terminal.app without using the mouse to make a selection.

This seems to only works with the older Terminal.app from Tiger. I just renamed it to Tiger Terminal.app, and it still runs fine on Leopard. Haven't tried it on Snow Leopard.

So, with Tiger Terminal, you can do mouse-free copy by typing ++, then using the arrow keys to move to the start of the area you want to copy. Next, type ++ again to anchor the selection point. Use the arrow keys (some emacs-like commands also work for navigation like ctrl+E) to move to the end of the region you want to copy. Finally, type ++ again to copy selection to the clipboard.

share|improve this answer

Useless, but fun. Go and download the ASCII players from Google Docs. Install them in /usr/bin. Now you can have a movie player in Terminal. There is ASCIIbw and ASCIIcolor. I don't think I need to explain that one is color and one is black and white. To open a movie file type: ASCIIbw ~/Desktop/Test.mov

share|improve this answer
show 4 more comments

Easy handling of tarring/compressing directories:

# lsZ -- list contents of compressed tar archive
function lsZ() {
    tar tvzf "$1"
}

# deZ -- silently extract contents of compressed tar archive
function deZ() {
    # extract bzip2 compressed tars as well
    if [[ $(file "$1") == *bzip2* ]]; then
        bunzip2 -c "$1" | tar xf -
    else
        tar xzf "$1"
    fi
}

# enZ -- build compressed tar archive
function enZ() {
    tar cZf "${2:-$1.tar.Z}" "$1"
}

# enG -- build compressed tar archive (with gzip)
function enG() {
    tar czf "${2:-$1.tar.gz}" "$1"
}

# enB -- build compressed tar archive (with bzip2)
function enB() {
    tar cf - "$1" | bzip2 > "${2:-$1.tar.bz2}"
}

# lsB -- list contents of bzip2 compressed tar archive
function lsB() {
    bunzip2 -c "$1" | tar tvf -
}

# deB -- silently extract contents of bzip2 compressed tar archive
function deB() {
    bunzip2 -c "$1" | tar xf -
}
share|improve this answer

When I'm in terminal I expect every action to be given in written form, because there is no GUI.

Many of the terminal based apps have some kind of quit command to bring you back to shell. This is what I'm used to when in terminal.

To leave a terminal window or tab I aliased following command:

alias q="osascript -e 'tell application \"System Events\" to tell process \"Terminal\" to keystroke \"w\" using command down'"

EDIT:

better even is to do as Jason commented:

configure Terminal.app to close the window if the shell exited cleanly

alias q="logout"
share|improve this answer
show 3 more comments
alias ka="killall"

Probably one of my most used commands. I put this in my .bash_profile for easy access.

share|improve this answer
drutil eject cd

Opens the CD tray or ejects a CD (for a laptop)

drutil tray close

Closes the CD tray

These are very useful when you are SSHing into another computer.

share|improve this answer
1  
So how is this useful? Can you ssh the CD to the tray? You do still have to get up and go to the machine to get a CD or place something in the now open tray. – bmike Nov 4 '11 at 19:40
show 1 more comment

One I actually use quite a lot is uptime. Simple but nice :) Currently mine returns up 32 days, 14:30.

share|improve this answer

If you have XCode installed, running the command purge at the terminal is really helpful. It frees up all of your active and inactive RAM. It's useful for people like me who do a lot of audio production (or any kind of media editing for that matter) when you only have 4 GB RAM. You would be surprised how fast 4 GB gets used up.

share|improve this answer
alias alias_open="mate ~/.oh-my-zsh/lib/aliases.zsh"
alias alias_reload="source ~/.oh-my-zsh/lib/aliases.zsh"
alias lsa='ls -lahG'
alias l='ls -la'
alias ll='ls -l'
alias sl=ls # often screw this up
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'

#######
# GIT #
#######
alias gd="git diff"
alias gl="git log"
alias gu="git up"
alias gs="git status"
alias gf="git fetch"
alias gr="git remote -v"
alias gp="git push"
alias gph="git push heroku master"
alias gps="git push staging staging:master"
alias gpg="git push github master"
alias gpo="git push origin master"
alias gplh="git pull heroku master"
alias gpls="git pull staging staging:master"
alias gplg="git pull github master"
alias gplo="git pull origin master"
alias gpl="git pull"
alias gc="git commit -am"
alias gco="git checkout"
alias ga="git add ."

##########
# SYSTEM #
##########
alias cwd='pwd | pbcopy' #copy the working directory into the clipboard
alias grep="grep --color=auto"

####################################################
# Create box of '#' characters around given string #
####################################################

function box() { t="$1xxxx";c=${2:-#}; echo ${t//?/$c}; echo "$c $1 $c";echo ${t//?/$c}; }

########################
# Rip audio from video #
########################

# ("$1" for output file & "$2" for input file)
function audioextract()
{
mplayer -ao pcm -vo null -vc dummy -dumpaudio -dumpfile "$1" "$2"
}
# extract audio from DVD VOB files
# USAGE:  audioextractdvd input_file.vob output_file.ac3
function audioextract_dvd()
{
mplayer "$1" -aid 128 -dumpaudio -dumpfile "$2"
}

#######################
# Backup .bash* files #
#######################

function backup_bashfiles()
{
  ARCHIVE="$HOME/bash_dotfiles_$(date +%Y%m%d_%H%M%S).tar.gz";
  cd ~
  tar -czvf $ARCHIVE .bash_profile .bashrc .bash_functions .bash_aliases .bash_prompt
  echo "All backed up in $ARCHIVE";
}
share|improve this answer
show 1 more comment
1 2 3 4

protected by Community Aug 29 '12 at 21:39

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

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