Here are a bunch of Mac OS X terminal commands sorted into general categories. I have intentionally omitted long bash scripts and AppleScripts and focussed instead on small useful commands that can be plugged into bigger scripts or used on their own… enjoy!
System
Restart Mac OS X:
shutdown - r now
Shutdown Mac OS X:
shutdown now
Power Management / Energy Saving
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
…Also see my post about hibernate mode and Safe Sleep on the Mac
OS X Look and Feel
Permanently disable Dock icon bouncing
Disable Dashboard (don’t forget to drag the Dashboard Dock icon off the Dock too):
defaults write com.apple.dashboard mcx-disabled -boolean YES killall Dock
Enable Dashboard:
defaults write com.apple.dashboard mcx-disabled -boolean NO killall Dock
Force the Finder to show hidden files (very useful for Web Developers who need to edit .htaccess files, for example):
defaults write com.apple.finder AppleShowAllFiles TRUE
Force the Finder to hide hidden files (ie: back to the default setting):
defaults write com.apple.finder AppleShowAllFiles FALSE
Networking
Ping a host to see whether it’s available:
ping -o leftcolumn.net
Troubleshoot routing problems to a host using traceroute:
traceroute leftcolumn.net
Check whether a host is running an HTTP server (ie: check that a Web Site is available):
curl -I www.leftcolumn.net | head -n 1
Automatically enable Internet Sharing at startup
Manage Windows networks (a drop-in for the NET command on Windows). Too many options to list here, so run this for details:
man net
Use dig to discover Domain information:
dig www.leftcolumn.net A dig www.leftcolumn.net MX
Who is logged in to your Mac?
w
Show routing table:
netstat -r
Show active network connections:
netstat -an
Show network statistics:
netstat -s
Troubleshooting
List all open files (this will take a few seconds to complete on most Macs):
lsof
Restart Bonjour – handy when a Mac ‘disappears’ from the Network:
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist sudo launchctl load /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
Eject a CD… it’s never happened to me but you can eject a stuck cd with the following. Note that it won’t always be ‘disk1′:
diskutil eject disk1
Text Manipulation commands
Sometimes you need to take some text from the clipboard or a file, transform it somehow and then use it. Here are a bunch of commands that do text manipulation. I’ve assumed you want to transform text from the clipboard and back again, see the notes at the end of the article for info on how to write to and from files instead.
Count number of lines in the text in the Clipboard:
pbpaste | wc -l
Count number of words in the text in the Clipboard:
pbpaste | wc -w
Sort lines of text in the Clipboard and copy them back to the Clipboard:
pbpaste | sort | pbcopy
Reverse each line of text in the Clipboard (ie: make each line appear backwards) and copy them back to the Clipboard:
pbpaste | rev | pbcopy
Strip duplicate lines from lines of text in the Clipboard and copy only one instance of each duplicate line back to the Clipboard (output is sorted):
pbpaste | sort | uniq | pbcopy
Find duplicate lines from lines of text in the Clipboard and copy only one instance of each duplicate line (stripping non-duplicates) back to the Clipboard (output is sorted):
pbpaste | sort | uniq -d | pbcopy
Strip duplicate lines from lines of text in the Clipboard and copy only one instance of each line (stripping duplicates entirely) back to the Clipboard (output is sorted):
pbpaste | sort | uniq -u | pbcopy
Tidy up HTML in the Clipboard and copy it back to the Clipboard:
pbpaste | tidy | pbcopy
Display the first 5 lines from the Clipboard:
pbpaste | head -n 5
Display the last 5 lines from the Clipboard:
pbpaste | tail -n 5
Convert tabs to spaces for the lines in the Clipboard:
pbpaste | expand | pbcopy
Other useful commands
Password protect your web site! Create a CRYPTed user/password for using in a .htpasswd file. Save the outputted results of A below to a file called .htpasswd in the directory you want to secure. Then save the contents of B to a file called .htaccess in the same folder.
A:
htpasswd -nb username password
B:
AuthType Basic AuthName "restricted area" AuthUserFile /path/to/your/site/.htpasswd require valid-user
Display a history of commands used in the terminal by the current user:
history
Convert a file to HTML. Support formats are Text, .RTF, .DOC.
textutil -convert html file.extension
Nano is a very easy-to-use text editor for quick changes to text files. It is less powerful than VIM but has the advantage of clearly showing you the common editing commands:
nano [file_to_edit]
…In nano, use ctrl+o to Save and ctrl+x to quit.
Greg shared a way of tidying the terminal window. Essentially this command scrolls down a page, clearing the current view:
clear
iTunes
Change iTunes link behaviour to point at local iTunes Library instead of iTunes Store:
defaults write com.apple.iTunes invertStoreLinks -bool YES
Change iTunes link behaviour to point at iTunes Store instead of local iTunes Library (ie: back to the default):
defaults write com.apple.iTunes invertStoreLinks -bool NO
Other Mac OS X Terminal Resources
Mac OS X Hacking Tools (old but detailed list for the obsessive only).
Note: For commands where I’ve used pbcopy to get the contents of the Clipboard as input, you can use the contents of a file as input instead. Swap pbpaste for:
cat [/path/to/filename]
And to put the results into a file on your desktop, just swap | pbcopy for:
> ~/Desktop/filename.txt
… hope you find them useful!















33 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
Great resource – thank you! I’m used to looking up a command but I can never remember the syntax etc a month or so later. So bookmarking this will be very useful. Lots of things in your list I hadn’t heard of, too – will be fun to experiment with these.
Minor correction:
Your code to enable Dashboard is the same as the disable code.
Should be:
defaults write com.apple.dashboard mcx-disabled -boolean NO
killall Dock
Thanks JS, fixed that typo.
Some of these (In fact… MOST of these.) aren’t Mac exclusive.
Thanks for your comment… Good point, I was really focussed on Mac OS X because there are obviously many Linux/Unix resources on the Web already. Off the top of my head, the ‘defaults’ command will not work, nor will ‘pmset’. Also, there are no ‘pbcopy’ or ‘pbpaste’ commands, at least not on Debian.
Awesome! The less than .05% of Mac users that will find this useful thank you!
Awesome! The less than .05% of Mac users that will find this useful thank you!
when i’m nano’ing as I’ve come to call it, i can’t seem to paste new strings in like i could in any text editor. i’m using terminal. is there any reason why my pastes are going to the top of the terminal every time, regardless of where i highlight or paste in the terminal?
You need to make sure you use the keyboard to navigate in the terminal. If you click with the mouse it does not set the cursor where you click, so you’ll paste at the default place, somewhere at the top. Use the arrow keys to navigate around.
Here’s the man page, worth a look:
http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/nano.1.html
nice tips! thanks…
“Clear cmdwin” is a good one if your terminal window is full and you want it to Helios you just opened it, it scrolls down 6 or 12 lines and gives you a “fresh command window again essentially.
Thanks Greg, I’ve added it to the list.
It’s me again, I made a mistake, it’s just clear, not clear cmdwin.
You’re right… anything following ‘clear’ is ignored. Cheers!
Re: using clear function
You can achieve same result via [cmd]-[k], although not in single-user mode (only supports clear).
Note: this can become a compulsion, so careful.
Nice, that’s handy. Note that cmd+k actually clears all the scrollback lines, whereas clear actually just pushes them out of the current view. Cheers!
Is there a terminal command that will set the label-color of a folder to blue?
This should work:
osascript -e "tell application \"Finder\" to set label index of item (\"~/Desktop/foo.txt\") to 5"
…but Adobe Photoshop CS3 seems to have installed some bad script extension son my mac so osascript is broken for me right now. You’ll need to play with the number, it should be 4 or 5 I think. Good Luck and let us know how you get on…
trying that script again:
osascript -e “tell application \”Finder\” to set label index of item (\”~/Desktop/foo.txt\”) to 5″
How can I turn off web sharing in the system preferences on a 10.3 via single user mode. What do I type? The system 9 has unmoutable errors and I suspect system 10 wont start up because I was silly enough to turn it on to see if that would fix something else. System wont start up it stalls on the last few start up things.
Hmm, well I have actually never used single-user mode, sorry, but if you can get terminal access then from the terminal you can get the pid of Apache with ‘top’ (look for ‘httpd’), then ‘kill -9 [pid].’ There may be several processes for httpd depending on your configuration.
Hope that helps…
I’m having some issues with the one to password protect a folder. Granted, I’m brand new to using terminal so i’m probably making a really obvious mistake that people will laugh at me for, but what does it mean that your instructions have the commands on separate lines? Are they to be entered individually, or just in a string on one line? I tried the latter but it said ‘-bash: AuthType: command not found’
You need to run command in A in the terminal on your mac, and select and copy the line of text it returns. This is a username/password combo, which you paste into a new file named ‘.htpasswd’ in the folder you want to protect. The contents of B are not typed in the terminal, they are pasted into another text file named ‘.htaccess’ – make sure you update the ‘AuthUserFile’ line to reflect the actual location of the .htpasswd file you created above. Usually its something like /home/example.com/public_html/.htpasswd.
Note that this is a command for protecting folders on apache web servers, not on your Mac. I suspect you might actually be after a tip to do the latter? If so, just select the folder you want to control in the finder, hit cmd I, and then under ‘Sharing & Permissions’ make sure your own login has ‘Read and Write’ access then set everyone else to ‘No Access.’
You can also protect a folder by making an encrypted disk image, which seems to work well. Instructions here: http://support.apple.com/kb/ht1578
How to disable sleep mode permanently command?
Please suggest.
Depends. To disable sleep in any condition:
pmset -a sleep 0
To disable sleep only when AC power (charger) is present:
pmset -c sleep 0
…you will probably need to ‘sudo’ these.
…Also just note that I got this info from the pmset man page on my Mac. It should work as documented, but I haven’t tested it. Finally, I would urge caution when disabling sleep in the absence of AC power.
Have fun with your new MacBook Server (or whatever)!
is there a command to disable ssid broadcast when making your own adhoc network….yes yes i know it doesnt make it more secure i just really want to know because we are not allowed to use wifi in afghanistan and i want to share my macs ethernet internet to my ipad but i dont want them to know im doing this….and no they do not have sniffing software.
If you’re using an airport device with the Airport Utility, it’s easy (Wireless Options -> Create a Closed Network), but if you’re talking about internet sharing then sorry, the best information I can find is this, which seems to indicate that it can’t be done. I’d pick up a cheap airport device and use that… good luck in any case…
How to i enable “Adobe QT 32 Server” on my Firewall?
Sorry for the late approval, I’ve been away from my mac.
If you’re using little snitch or something similar, see this:
http://kb2.adobe.com/cps/857/cpsid_85711.html
…If not, then your mac won’t be firewalled. People have reported problems with this software. If you have freezing issues you should try upgrading all codecs and plugins first. Good Luck!
thanks! great scripts
very useful for me
Continuing the Discussion