Did you know that future updates to Minecraft are always previewed in weekly snapshots? Big parts of the ‘pretty scary update’ were available for weeks before it was officially released! Here’s how you can get access to the cool stuff in upcoming versions of Minecraft on your Mac… (more…)
Archives For mac osx
The /etc/hosts file on the Mac can be hard to find. Here are some tips for finding and editing it, and what you can do with it. (more…)
I’ve covered the ‘history’ command before, but here are two more useful Mac OS X Terminal commands…
I’ve been using the Ctrl+ R trick (Hit Ctrl + R, start typing part of the command you want. If you don’t like the first suggestion hit Ctrl + R again to show the next one). This is really handy and fast and it will return matches from the middle of the string.
And I just found another great Terminal command here: Discover the last used command beginning with anything without executing it… this one is a bit harder to use:
…where [needle] is the first character(s) of the command you’re looking for. It’s not as flexible because it only shows the most recent match instead of cycling through the results, but could still come in handy.
Web Developers, Administrators of Mac Networks, and anyone who likes to noodle with OS X will probably eventually need to add hosts file entries. I use them when I’m testing a moved and/or changed version of a website at new hosting before I move the DNS.
I used to add hosts file entries manually then wonder why they didn’t seem to work. I tried rebooting, logging out, etc with varying success, until I learned this trick. On the Mac you must flush the DNS cache for the changes to take effect. Mac OS X used to have a friendly utility called NetInfo Manager, but that seems to have been removed in Leopard, so we’re using terminal commands.
Open a terminal sesion and type:
sudo nano /etc/hosts
Hit the down arrow until you get to the end of the file, and add your hosts file entry, for example:
… I put in an extra line break at the end. I don’t think it’s really necessary but it’s a habit from having to use Windows, which would ignore the last line in a hosts file.
then flush the cache:
sudo dscacheutil -flushcache
…the name should now resolve! Test it:
Update: As of OS X 10.5 (aka Leopard) you don’t need to flush the DNS: changes to the hosts file take effect immediately. There are other cases where it is necessary to flush DNS; be aware that as of 10.7 (aka Lion) you should use:
sudo killall -HUP mDNSResponder
One thing that Windows, for all its annoyances, did get right is that changes to the hosts file (in ‘c:\Windows\system32\drivers\etc’) are reflected immediately. I’d like to see Apple do something about this in OS X, perhaps just flushing the cache automatically when /etc/hosts changes. Update: as of OS X 10.6 this works; changes to the hosts file take effect immediately.
Another handy use for /etc/hosts is to set up mapping of virtual server addresses. For example, I use MAMP on my development Mac, and create new virtual hosts entries in Apache for each site I’m working on. Then for each I just add a new entry in the hosts file, e.g.:
… then I just use devclientsite7.leftcolumn.net:8080 in Safari or Chrome to access the development site. It means my development sites don’t have to be in subdirectories, instead I just make sure the sites use a config file to figure out what their address is, and I make sure that config file is updated when the site is moved to a live system.
On OS X Tiger (10.4) and probably earlier, there was some strange behaviour with hosts file entries. You could add an entry and it would take effect, but if you had no network available, the name would not resolve and could not be used, even if it resolved to 127.0.0.1! So you have the bizarre situation of putting in addresses for virtual hosts as described in the previous paragraph, all of which resolve to 127.0.0.1, but still not being able to use them for testing and development when not connected to the Internet. If anyone can enlighten me as to how this was anything other than a bug in OS X I’d be interested…
You can also use hosts file entries to block ads (by resolving common ad server addresses to e.g.: 127.0.0.1). You can download huge lists of ad servers for this purpose. This is a good idea when you’re stuck using Internet Explorer on Windows, especially on dial-up.
If you like this page and want to know more about the Mac Terminal, check out my ebook. It contains an improved version of the hosts file info here, plus a reference to commonly-used Terminal commands. Get to grips with the Mac Terminal and scripting tools now. More Info >
One day my new MacBook Pro 17″ wouldn’t wake up. When I opened the lid machine was running but the Desktop wouldn’t load (just a default blue Desktop background colour) and there was no response from the Mouse or Keyboard. I eventually had to hard reset the Mac.
This annoying issue can apparently be caused a few different ways, but if your Mac is new and has all System Updates applied, chances are that the issue is caused when you close the lid and then move the mac before the contents of RAM are written out. There’s a feature called Safe Sleep, enabled by default, which can preserve and restore the state of a sleeping Mac even when the battery is removed. This sounds great, but the Safe Sleep procedure can take up to 30 seconds, which is too long for me.
How to fix this annoying problem? Here’s how I switched it off:
sudo pmset -a hibernatemode 0 sudo rm /var/vm/sleepimage
…you need to supply the super user password. After making the change, reboot the Mac. The Mac should now Sleep within a couple of seconds. The second line removes the file that Sleep Safe uses to store the contents of RAM. Since you’ve switched off Safe Sleep, the space can be recovered.
Personally I don’t see the need for Safe Sleep on a Mac with a non-removable battery, but be aware that if the machine is asleep and runs out of battery power you wil lose your logged in session! I haven’t tried it, and most OS X apps will deal pretty well with this anyway, but it’s not wise to let this happen…
Here are a bunch of Mac 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!
Terminal & Shell Basics
cmd+n – Open a new Shell in a new window
cmd+t – Open a new Shell in a new tab of the current window
control+d – Logout the Shell in the current tab / window
cmd+d – Split pane. this is not a new shell, just a way of displaying the current Shell.
Restart Mac OS X:
sudo shutdown -r now
Shutdown Mac OS X:
sudo shutdown now
Power Management / Energy Saving
Get overview of current Power Management Settings:
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
If you don’t like the way Mountain Lion now makes the User ‘Library’ folder invisible, you can disable this.
chflags nohidden ~/Library
…you don’t need to relaunch the Finder.
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
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
Ping a host to see whether it’s available:
ping -o leftcolumn.net
Troubleshoot routing problems to a host using traceroute:
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
Manage Windows networks (a drop-in for the NET command on Windows). Too many options to list here, so run this for details:
Use dig to discover Domain information:
dig www.leftcolumn.net A dig www.leftcolumn.net MX
…and you can also retrieve all available stuff in the DNS Zone for a domain with eg:
dig -t ANY google.co.nz
Who is logged in to your Mac?
What’s my user name? This is really useful for bash scripts etc.
Show routing table:
Show active network connections:
Show network statistics:
List all open files (this will take a few seconds to complete on most Macs):
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 terminal 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 mac terminal 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.
htpasswd -nb username password
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:
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:
…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:
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:
And to put the results into a file on your desktop, just swap | pbcopy for:
2016: Updated for El Capitan!
Hey if you find this page useful, check out the 2016 version of my Mac Terminal Commands ebook. It contains all the commands here, plus many new ones, and other useful stuff to do with the OS X Terminal, bash, AppleScript, and more. Get started with Mac scripting now >