Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged mac os x from sorted by
Terminal - Commands tagged mac os x - 25 results
finger $(whoami) | egrep -o 'Name: [a-zA-Z0-9 ]{1,}' | cut -d ':' -f 2 | xargs echo
2014-09-24 01:22:07
User: swebber
Functions: cut egrep finger xargs
0

Its possible to user a simple regex to extract de username from the finger command.

The final echo its optional, just for remove the initial space

mount_smbfs '//user:p%40ss@server/share' /Volumes/share
2014-07-27 00:52:19
User: bupsy
0

If the password for the share your trying to mount contains special characters you can use URL escape characters.

The above command uses an example as follows:

username: user

password: p@ss

URL Encoded password: p%40ss

All credit goes to Richard York:

http://www.smilingsouls.net/Blog/20110526100731.html

Also check out this URL Decoder/Encoder to convert your passwords.

http://meyerweb.com/eric/tools/dencoder/

cat /dev/urandom | env LC_CTYPE=C tr -dc a-zA-Z0-9 | head -c 16; echo
2014-02-05 15:04:07
User: yakovlev
Functions: cat env head tr
0

Feel free to put this in your ~/.profile:

random(){ cat /dev/urandom | env LC_CTYPE=C tr -dc $1 | head -c $2; echo; }

Then use it to generate passwords:

random [:alnum:] 16

Or DNA sequences:

random ACGT 256
brew install ssh-copy-id; ssh-copy-id user@host
rvm --default use ruby-1.9.3
/usr/bin/java -Xmx256M -XstartOnFirstThread -Dcom.android.sdkmanager.toolsdir=android-sdk/tools -classpath android-sdk/tools/lib/sdkmanager.jar:android-sdk/tools/lib/swtmenubar.jar:android-sdk/tools/lib/x86_64/swt.jar com.android.sdkmanager.Main avd
/Applications/SuperCollider/SuperCollider.app/Contents/Resources/sclang ~/path/to/your/scfile.scd
echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
2013-09-19 03:25:14
Functions: echo
0

If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment character.

pbpaste | coffee -bcsp | tail -n +2
2013-09-13 04:50:27
User: roryokane
Functions: tail
-1

This particular combination of flags mimics Try CoffeeScript (on http://coffeescript.org/#try:) as closely as possible. And the `tail` call removes the comment `// Generated by CoffeeScript 1.6.3`.

See `coffee -h` for explanation of `coffee`'s flags.

function garg () { tail -n 1 ${HISTFILE} | awk "{ print \$$1 }" }
2013-09-10 04:07:46
User: plasticphyte
Functions: awk tail
0

This gets the Nth argument in the last line of your history file. This is useful where history is being written after each command, and you want to use arguments from the previous command in the current command, such as when doing copies/moving directories etc.

I wrote this after getting irritated with having to continually type in long paths/arguments.

You could also use $_ if all you want is the last argument.

sysctl kern.sleeptime
sysctl kern.sleeptime
sysctl -a | grep sleeptime
2013-07-29 07:57:01
User: themiurgo
Functions: grep sysctl
Tags: sleep mac os x
1

Similarly for last wake time:

sysctl -a | grep waketime
while i=1; do /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep CtlRSSI; sleep 0.5; done
pbpaste | tr '\r\n' '\n' | tr '\r' '\n' | pbcopy
2012-07-31 10:50:14
User: adambrenecki
Functions: tr
0

Copying and pasting from Office documents open in Office:mac can dirty your files with Windows CRLF and (inexplicably) Classic Mac OS LF newlines, which can break some tools. This snippet replaces them with good ol' Unix LF newlines.

sudo killall -HUP mDNSResponder
2012-05-15 18:33:51
User: Xeno
Functions: killall sudo
0

Purges DNS cache of OS X.

dscacheutil -flushcache does not work since OS X 10.7.

pmset -g batt | egrep "([0-9]+\%).*" -o --colour=auto | cut -f1 -d';'
2012-03-23 23:23:46
User: deshawnbw
Functions: cut egrep
2

adjusting the field "f1" will give you additional information such as

f1 = 98%

f2 = discharging

f3 = 2:02 remaining

sudo curl "http://hg.mindrot.org/openssh/raw-file/c746d1a70cfa/contrib/ssh-copy-id" -o /usr/bin/ssh-copy-id && sudo chmod 755 /usr/bin/ssh-copy-id
2012-02-09 20:29:24
User: misterich
Functions: chmod sudo
-1

Mac install ssh-copy-id

From there on out, you would upload keys to a server like this:

(make sure to double quote the full path to your key)

ssh-copy-id -i "/PATH/TO/YOUR/PRIVATE/KEY" username@server

or, if your SSH server uses a different port (often, they will require that the port be '2222' or some other nonsense:

(note the double quotes on *both* the "/path/to/key" and "user@server -pXXXX"):

ssh-copy-id -i "/PATH/TO/YOUR/PRIVATE/KEY" "username@server -pXXXX"

...where XXXX is the ssh port on that server

ssh user@server.com sudo tcpdump -i eth0 -w - 'port 80'| /Applications/Wireshark.app/Contents/Resources/bin/wireshark -k -i -
wget -nd -nH -r -A pdf -I library/mac/documentation/ http://developer.apple.com/library/mac/navigation/#section=Resource%20Types&topic=Reference
2011-10-02 04:56:25
User: GinoMan2440
Functions: wget
Tags: mac os x
-2

download every last pdf reference Doc from the Apple Developer Connection, takes a half hour on a fast connection. enjoy!

defaults write com.apple.terminal FocusFollowsMouse -string YES
2011-07-26 02:15:04
User: ratonovitch
Functions: write
0

In Mac OS X, by default, you have to click the mouse on a Terminal window before you can type in it. You can change this behavior to mimic the X11 behavior of focusing on the window on mouseover.

defaults write com.apple.dock persistent-others -array-add '{tile-data={}; tile-type="spacer-tile";}'; killall Dock
2011-07-26 02:05:17
User: ratonovitch
Functions: killall write
0

Insert an additional (moveable) spacer on the right side of the Dock in Mac OS X

defaults write com.apple.dock persistent-apps -array-add '{tile-data={}; tile-type="spacer-tile";}'; killall Dock
2011-07-26 02:03:29
User: ratonovitch
Functions: killall write
0

Insert an additional (moveable) spacer on the left side of the Dock in Mac OS X

sudo scutil --set HostName MY_NEW_HOSTNAME
env LC_CTYPE=C tr -dc "a-zA-Z0-9-_\$\?" < /dev/urandom | head -c 10
2011-02-22 17:09:44
User: aerickson
Functions: env head tr
0

Prepending

env LC_CTYPE=C

fixes a problem with bad bytes in /dev/urandom on Mac OS X