Nuclear Powered Brain Fueled by chili and cooled by beer.

Intro to Command-Line

The purpose of this document is to teach users with no experience how to use, and get comfortable with the command line on a Mac. At the end of this article, you will be able open and delete files.

Even though you live a productive life without the command line, it is immensely useful. Mac users have it made in the shade. Terminal is readily available with a great multitude of useful commands and interpreters. First, let’s open it up.

Open the command line

On the Mac [OS X], in Finder, open the Utilities folder under Applications. You can quickly open the folder from Finder’s Go menu, or just press shift+command+U.

Finder Go Menu

Now find that Terminal icon and click it. Click it or ticket.

Finder Go Menu

I use it so much, I keep the Terminal in my Dock. Terminal Sweet Terminal.

Terminal in Dock

Since this is an Introduction to Introductory Command Line, we’ll start with the simplest one word commands. The only prerequisites are a Mac, you already have that presumably, and a cup of coffee. Coffee optional. We’ll start with the absolutely simplest commands. While each of these do offer more complexity, it’s not necessary to get started and gain the usefulness of the command line.

Single word commands

We’ll start with a remarkably handy command I discovered not long, long, long ago.

caffeinate

Ever have a moment you want your Mac to not sleep. You could change the power settings, or download an applet. But caffeinate is included, free. caffeinate is like coffee. It will keep your Mac awake. For instant caffeination, just type caffeinate and press Enter. Enter is how we execute the command typed in the Terminal app. When it’s time for a nap, press the Control and “C” keys together (control+C) and the command will end letting the Mac conserve battery power like normal. No re-changing Power Settings, nor clicking a menu bar applet icon. Just one word typed and you already have something useful on the command line.

caffeinate

date

Now, let’s type date. It shows the current date, time and timezone. Yes, you may have that displayed in the Menu Bar, but this is convenient, when you are in the command line, here, or on your remote server.

date

cal

Similarly, type cal to display the current month’s calendar. Faster than waiting for the Calendar app to open. Type cal to a glance at the monthly calendar in an instant.

cal

top

We won’t go into detail regarding top. You may have seen this mentioned in support tickets for your Linux or FreeBSD server. Your Mac has this too. Just type top, press Enter and you’ll see the diagnostic detail for your Mac. To quit this command, just press q.

top

control+C and q are the two most common ways of quitting commands on the command line.

Wasn’t that easy and harmless? With one word commands, you are already useful on the command line.

Everything else will require a little directory navigation. That’s easy in Finder, just click on folders with the mouse, or trackpad. You might be a power user if you can navigate the file system in Finder with the keyboard. Bonus points if you press command key and the up arrow key in a Finder window now. Double points if you press shift+command+O.

Folder/Directory navigation

To navigate the directory structure in the command line, there are a couple two-letter commands. cd changes directory and ls list contents of a directory.

ls & pwd

pwd just shows where you presently are working in the directory structure, present working directory.

We’ve only issued single-word commands, so far. Add a directory or filename to be useful. To actually change directories, type cd and a directory name like Document. Type cd Documents to change to the Documents directory. You can do the same for Downloads, type cd Downloads. Except Downloads in not in the Documents directory we just changed to. Type cd to return to where we started. Again, cd Downloads.

cd

It’s in the home directory at the same level as the Documents. To see this, again, type cd, then ls. See the directories Documents and Downloads listed, among others. Now type cd Downloads. To save a step to changing directories, type the tilde-slash(~/) before Pictures and we can change to the Pictures directory in one step, regardless of the present working directory. ~/ is shorthand for the home directory, in my example, /Users/lyndell. .. is shorthand for the parent directory. cd .. will take you up a directory. The forward slash (/) separates directories. At the beginning of the directory name, the / means “root”, the very top of the directory structure. Thus, Users is a subdirectory of / (“root”), lyndell is a subdirectory of Users. Documents is a subdirectory of lyndell. Further, /Users/lyndell/Documents is the full directory name for the Documents directory. Directory and folder are synonyms.

cd & ls

open

open is a handy little command I like to use. First, lets create an empty file in the Documents folder by typing touch server_change_log.txt. (Again, the command filename action.) Now open the file in TextEdit by typing open server_change_log.txt.

open TextEdit

You can open videos, or any file Finder knows how to open with this command. If you have a video named “how_to_backup.mov”, open how_to_backup.mov will open the movie “how_to_backup.mov” in QuickTime Player. You can even open web addresses; open http://www.softlayer.com/services/storagelayer/.

Does this seem useless and redundant to the great Mac user interface of Finder? If your helpful administrator gave you a shell script named “download-backup.sh” to save your own copy of the server backup, you can run it without your Mac falling asleep in the middle of the script, leaving the backup unfinished. Type the file name “download-backup.sh” after the command caffeinate in Terminal and the backup download should complete without interruption.

caffeinate download-backup.sh

Now you can automatically download backups with the hypothetical script “download-backup.sh”. With cd and ls, you can browse the files.

Options

Now we can take this up a level and add options to the commands, sometimes called switches, arguments or parameters. You saw ls a minute ago, add either or both -l and -h to list the directory in long human readable format. -l give the extra information, like user (lyndell), size and date. -h makes the size units more convenient. These single letter options can be combined together: -lh

These options really add power to the command line. Add the -t option to sort the directory list by time. The -r just reverses the sort order.

ls sort time

Replace -t with -S to sort by size. Again, with the -r switch. The large files are at the bottom of the list, convenient if on a search-and-destroy large files mission.

ls sort size

rm

rm simply removes files. Simply type rm and the file name. Example:

rm Kevin_Hazard_dances.mov

srm

But the OS doesn’t actually wipe the drive space used by the delete files. Obviously, any file left in the Trash can be pulled right out. Sure, you can just use Secure Empty Trash in the Finder menu, but we’re here to use the command line.

Secure Empty Trash

srm securely removes files with options of zero or overwrite the file. Be careful! You’re not getting your files back after this, unless you have backup to restore from. The point of these commands it to delete with near impossible chance of recovering the files.

You may combine the following options. Adding -z will quickly zero wipe the file. -v gives a little progress indicator. Here is srm -vz Kevin_Hazard_dances.mp4 demonstrated:

srm verbose

Once completed, looks like this:

srm done

Two other option to more thoroughly overwrite the file, as explained by the man (manual) page.

   -s, --simple
          only overwrite with a single pass of random data

   -m, --medium
          overwrite the file with 7 US DoD compliant passes  (0xF6,  0x00,
          0xFF, random, 0x00, 0xFF, random)

Delete Directory

To delete an empty directory, simply type rmdir directory_name.

rm can delete whole directories. rm normally won’t delete a non-empty directory. Add the switch -r and -f, run together as -rf. to delete everything in a directory. Be very careful! This will delete everything in a directory without asking if you’re sure. To be safe, list (ls) the folder with the directory to delete, then spell out exactly what to delete. For example:

rm -rf directory_to_delete

You can delete a lot with this. Be careful. srm also has the same -r and -f.

And that, is how to delete files, individually, whole directories, on the command line in Terminal.

SSH is the door to a whole new world. Here is where the real value of the command line comes in. With the right app, you can even use this on your phone to connect to the server. Just the thing to delete unneeded files, backup databases, move files, while sipping Mai Tais. Don’t rm while drunk!

Log-in

To log in with SSH, you don’t need an app for that, this is another handy command already on the Mac, type ssh username@www.example.com. Replace “username” with your username or “root” if logging in as the root user. Replace “www.example.com” with the IP or domain name of your server. If this is your first time SSH’ing to the server, type yes to accept the key when asked, then enter the password to log into the server. Once logged in, it should look like this:

login

Which server?

Looks like the Terminal we’ve been in this whole time. You can get lost, but typing hostname will help you remember, whether you are working on your Mac, or logged into a remote server.

hostname

Log-out

To logout of the remote server, type logout or press control+D. Your screen should look something like this.

logout

That was an ultra short introduction to SSH. Now intro to SSH should be an easy read. Afterward, try Boot Camp!

Sure, you can use SFTP, but with the command line, you can use the some of the clever tricks taught in this three part series. You can delete unwanted files filling a partition that’s keeping the websites or databases offline.