Skip to content

Instantly share code, notes, and snippets.

@dive
Created July 8, 2012 12:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dive/3070807 to your computer and use it in GitHub Desktop.
Save dive/3070807 to your computer and use it in GitHub Desktop.
Terminal.app tips and tricks
# navigation
# 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
# alt + <-/->
# 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 + Y - paste previously deleted text
# ctrl + P - up in command line history
# ctrl + N - down in command line history
# cmd + K - clear screen
# hold option and click a position in the current line to move your cursor to that position
# shows a list of the recent commands you've run
# press Ctrl + R in bash to interactively search through your shell history
history
# copies history between terminal tabs
history -a; history -r
# make inactive memory as free again
purge
# launch Software Update from the CLI
sudo softwareupdate -i -a
# shows who has logged into a system, how long they used it, and when the system has been restarted
last
# displays the currently installed version and build of Mac OS X
sw_vers
# give you the same results as you get when you choose About This Mac -> More Info, only on the command line.
system_profiler
# looking at the man page will give you tips on how to use it
system_profiler -usage
# runs the last command again
!!
# open current foler in Finder
open .
# open file in specified app (just use 'open filename' for mime apps)
open -a Preview image.png
# launch quicklook on a file from the command line
qlmanage -p 2>/dev/null "filename"
# open a man page in Preview (replace "ls" with your own)
man -t "ls" | open -f -a /Applications/Preview.app
# open a man page in TextMate (replace "ls" with your own)
MANWIDTH=160 MANPAGER='col -bx' man "ls" | mate
# say command invokes the system text-to-speech capabilities
say "Hello there"
# say the contents of a text file
cat somefile.txt | say
# take your text file, convert it to .aiff
cat someFile.txt | say -o someAudioFile
# if you want to know what sort of line endings a file has, just run
file /path/to/your-file
# ex:
# file imports/sample-students.txt
# imports/sample-students.txt: ASCII text, with CR line terminators
# Copy output of command to clipboard
grep 'search term' file.txt | pbcopy
ls -lah | pbcopy
# Abuse clipboard contents
find ~/Documents/ -name "`pbpaste`"
pbpaste | sed 's/ /%20/g'
# print almost any document as a PDF, as long as it has a correctly defined MIME type in OS/X
cupsfilter $filename > output
# opensnoop uses DTrace to show you all of the files that are being accessed on your system
sudo opensnoop
# what a particular process opens by passing in the PID
sudo opensnoop -p PID
# watch a particular file to see who's opening it
sudo opensnoop -f /etc/passwd
# monitor file system activity
sudo fs_usage
# quick start webserver from any directory
# -python
python -m SimpleHTTPServer 8000
# -ruby
ruby -rwebrick -e "s = WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start"
# check whether a host is running an HTTP server (ie: check that a site is available)
curl -I justsitandgrin.net | head -n 1
# drag a folder from the Finder to the terminal and it will paste the full path to that file
cd <drag folder to terminal>
# restore the previous directory you were in
cd -
# mount a disk image
hdiutil mount ~/Desktop/lastest_webkit.dmg
# dismounting
hdiutil detach /Volumes/<mountpoint>
# to use spotlight from the command line
mdfind "search term"
# updates in real time
mdfind -live "search term"
# show you all metadata of the file that Spotlight knows about
mdls filename
# allows you to switch indexing on or off on certain volumes, and allows you to reset the index etc
mdutil
# check how many disk space I currently have on all the disk
df -h
# displays disk usage statistics for the current directory in human readable form
du -d 1 -h
# BSD specific settings tool (check help for more)
systemsetup
# get overview of current Power Management Settings
pmset -g
# put display to sleep after 15 minutes of inactivity
sudo pmset displaysleep 15
# put computer to sleep after 30 minutes of inactivity
sudo pmset sleep 30
# change volume: mute
osascript -e 'set volume output muted true'
# max
osascript -e 'set volume 10'
# handy tool that can cross convert text between HTML, RTF(D), Word (including XML), OpenOffice.org Writer, and the webarchive format
textutil -convert html filename.ext
textutil -convert html -title "Here we are" -author "Unknown" filename.ext
# 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 '
# check what is eating all your memory
top -o vsize
# for your CPU
top -o cpu
# wait for network to be ready in a script
/usr/sbin/networksetup -detectnewhardware
# list of network IPs
ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'
# inet IP
dig +short myip.opendns.com @resolver1.opendns.com
# Flush DNS cache
dscacheutil -flushcache
# will display the order for DNS resolution
scutil --dns
# resample image so height and width aren't greater than specified size, e.g. 100x100:
# sips supports other operations such as: flip, rotate, crop, image properties query, colour profile query and modification. Check man sips for usage.
sips -Z 100x100 image.jpg
# automagically creates icon previews for all images
sips -i *
# grab whatever from the screen
screencapture -i -c
# let's you play songs from the commandline
afplay ~/path/to/file.mp3
# shows you the cdtext info (if any) on the CD currently in the drive
drutil cdtext
# shows you the capability of your optical drive(s)
drutil info
# eject cd
drutil eject
# For each dir, dot_clean recursively merges all ._* files with their corresponding native files according to the rules specified with the given arguments. By default, if there is an attribute on the native file that is also present in the ._ file, the most recent attribute will be used.
dot_clean .
# ============================================ .bash_profile ============================================
# Add tab completion for SSH hostnames based on ~/.ssh/config, ignoring wildcards
[ -e "$HOME/.ssh/config" ] && complete -o "default" -o "nospace" -W "$(grep "^Host" ~/.ssh/config | grep -v "[?*]" | cut -d " " -f2)" scp sftp ssh
# colored Prompt
PS1="\[\e[0;31m\][\[\e[1;31m\]\u\[\e[0;34m\]@\h \[\e[32m\]\w\[\e[0;31m]\]\$\[\e[0m\] ";
# Case-insensitive globbing (used in pathname expansion)
shopt -s nocaseglob
# Append to the Bash history file, rather than overwriting it
shopt -s histappend
# Autocorrect typos in path names when using `cd`
shopt -s cdspell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment