11 easy Mac OS Terminal Tips and Tricks

One of the most powerful features of the Macintosh Operating System is the command line interface.  The Terminal is a native application that gives you access to the command line interface and direct access to the UNIX based operating system (called Darwin) that MacOSX was built in.  Not to go into details here (perhaps in another post), but for those who do not know what UNIX is, just know it’s a very powerful way to maximize your computer’s full potential. 

As user there is a great deal that you can do with the terminal: quickly move files on a network, remotely access your computer at home, change prefrences that don’t appear in the graphical interface, and much more!  To give you a hint here are some Terminal tricks that you may not know about…in future posts we’ll dive more into Terminal.

Getting Started

The Terminal gives you a direct connection to the very fabric of your Mac.  These tips are perfectly safe.  If you don’t know what you are doing, don’t experiment or you could find yourself with an unstable computer.  Click here to learn more about terminal and check our blog for future posts on using Terminal to get it’s full potential.

Starting Terminal

The Terminal app is located in the Utilities folder (Applications -> Utilities).  Or you can use spotlight to quickly launch it (command+space, Terminal).  You’ll get a screen similar to this:

screenshot_20
The bourne-again shell (BASH) Terminal Window

You enter commands at the prompt following the ‘$’ sign. By default the prompt will tell you what computer you are on and the current directory.  Enter commands following the ‘$’ in the Terminal and press ‘enter‘. Go ahead try them out:

Navigate to a folder using the ‘cd’ command:

cd /Documents
cd /Documents/My\ Stuff /
cd "/Documents/My Stuff/"

cd

Notice there are two ways to enter a space if the directory has a space in the name. Surround it in Quotes or use the backslash.

The TAB key has a magic auto-complete feature. If the pathname is long, press the TAB key after the first or second letter. Use TAB to step through the directories until you get to the one you are looking for.

The final example jumps you back to your Home Folder. Do this now before continuing.

List the files in a directory

The ls command is a useful tool to tell you what content is inside of a directory. The simplest form of ls is simply ls:

screenshot_24
ls’ command output

Looks a little like the finder view in text. Here are examples of how more information Terminal can provide with very little change to the command:

ls -l
ls -la

screenshot_25
The ‘ls -l‘ command output
screenshot_26
The ‘ls -la‘ command output

What changed? The list view (ls -l) provides a sorted listed that tells you file owners, read&write access permissions, file size and date modified. The list all view (ls -la) provides the same sorted list but includes the hidden files in the directory. Bet you didn’t know those were there did you? Did you know you can use Terminal to activate this in Finder?

Change defaults to show Hidden Files

Using terminal we are going to issue a command that forces the graphical interface to also show us the hidden files and folders. Enter these two commands (one at a time, not at once) to enable it:

defaults write com.apple.finder AppleShowAllFiles -bool TRUE
killall Finder

The “killall” command tells the Finder to reboot with with new settings. Take a look at the difference:

screenshot_27
The default Finder view of a user’s Home folder
screenshot_28
The modified view of the Home folder showing hidden files

To revert back use these two commands:

defaults write com.apple.finder AppleShowAllFiles -bool FALSE
killall Finder

Telling Terminal to Cancel a Command

Sometimes a command will seems give you an endless stream of text or seem to be non-responsive to your command. This is usually a sign that you entered a command wrong or are experimenting. A simple way out of this is to press CTL-C.

Read Any File

I literally have files that are 20 years old…but the applications I used to make them simply don’t exist or run on modern computers. cat is a powerful command line tool to view the contents of any file. Give it a try:

cat .bash_history

The result is a simple list of the commands that you have entered into Terminal. cat will list the contents of any file…even videos (but unless you live the Matrix video files are just a string of digital code).

BONUS TIP: instead of using cat to view your history…just type history.

Learn More About Command Options

Did I mention this is UNIX? A handy feature set built into UNIX is in-depth help for commands. Nearly all commands have a MAN page that will detail what the commnd does and what options you can use to bring out it’s power. Look at the MAN page for ls:

man ls

screenshot_30
the MAN page for listing directory contents (‘ls’)

To scroll down press the spacebar. To exit press ‘q’.
Try it with the other commands we have used so far and experiment with some of the command options. See if you can find the option to colorize the directory list.

Detailed System Processes

The command top will give you very detailed information in real time on what your Mac is doing.  It’s useful to see the process that are using up your resources in real time.  It’s similtary to the Activity Viewr app but I find it to be easier to use at times.  Another useful command uptime tells you how long your computer has been running between restarts.

screenshot_31
The top command output

Start a Simple HTTP Server

Apple removed the ability to create a Web Server from general users a few OS versions ago. But there is a way to use terminal to create a simple one “on the fly” to transfer files to your Windows friends or test some HTML code.

Navigate to the folder with your data or HTML code and enter the command below:

python -m SimpleHTTPServer 8000

The number 8000 is the web port to use, you can change it to whatever you need. Then in your browser visit http://localhost:8000. Voila! instant webserver.

screenshot_32
Using pyhton to create a simple webserver

To exit use CTL-C inside the Terminal widow.

Shutdown Your Mac

This tip is most usfeful when you remote into other Macs or are on a server. However, using the Shutdown command to shutdown or restart your Mac can be faster and force a stubborn Mac to restart.

To shutdown immediately:

sudo shutdown -h now

The sudo command is short for “Super User Do” and grants you elevated privledges to the system kernal (a topic for another blog). You’ll be asked for your password when you enter commands with sudo.

To restart your mac immediately:

sudo shutdown - r now

The are many other command line options to use that can add messages to logged in users and countdown timers to allow users to save their work. Rember the man pages? Yep, use ‘man shutdown‘ for more information.

Find Your IP Address

There are a few easy ways to do this in the graphical interface. In terminal use the following to see your IP Address

ipconfig getifaddr en0

screenshot_33
Your local IP address using ipconfig

Have Your Mac Read a Book and Build an Audiobook

Your Mac can speak and has built in some great functionality to convert files to spoken text. I used this quite frequently in grad school to convert adobe .pdf files into audio books. You can start with simple commands like:

say "Four score and seven years ago..."

Or try speak an entire text file:

say -f /Documents/files.txt

In an upcoming post I’ll show you how to use automator and the say command to create an MP3 to play in iTunes. Bonus: If you don’t like the default voice, open the Accessiblity control panel, select Speech and change the voice:

screenshot_34
The Accessiblity control panel for speech options.

Keep Your Laptop Awake

I use my laptop much more than I do a desktop computer. Sometimes there is a need to keep the laptop from automatically falling asleep and closing the display or forcing you to log in.  The caffeinate command solves this problem and will keep your Mac laptop awake until you cancel it with a CTL-C:

caffeinate

Optionally, you can give it some options (remember to use MAN) to turn off in, oh say 360 seconds (5 minutes):

caffeinate -u -t 350

Have some Nerdy Fun

Now take a break and have some fun…by watching Star Wars espisode 4 in ASCII art!

telnet towel.blinkenlights.nl

screenshot_36
R2-D2 takes one for the team

Telnet is an older executable command that establishs a terminal window into another computer (in this case one in the netherlands). This telenet session launches into the ASCI version of Star Wars in true 1977 fashion!

That’s all for now

This is just an introduction to Terminal. In future posts we’ll dive deeper into the utility of it to include modificaiton of preferences and incoproting Apple’s Automator application to do some useuful things to improve your experience.

If you want to explore more on your own…head over to SS64 and the extensive list of default MacOSX commands listed there.  We’ll also talk about adding more UNIX tools through services like Homebrew in future posts.

Have fun and safe computing! What Terminal Tips do you? Share in the comments below.

Leave a Comment Here, Contribute!

This site uses Akismet to reduce spam. Learn how your comment data is processed.