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.
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.
Now find that Terminal icon and click it. Click it or ticket.
I use it so much, I keep the Terminal in my Dock. Terminal Sweet Terminal.
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.
We’ll start with a remarkably handy command I discovered not long, long, long ago.
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.
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.
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.
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 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.
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.
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
cd Downloads. Except
Downloads in not in the
Documents directory we just changed to. Type
cd to return to where we started. Again,
It’s in the home directory at the same level as the
Documents. To see this, again, type
ls. See the directories
Downloads listed, among others. Now type
cd Downloads. To save a step to changing directories, type the tilde-slash(
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,
.. 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
lyndell is a subdirectory of
Documents is a subdirectory of
/Users/lyndell/Documents is the full directory name for the
Documents directory. Directory and folder are synonyms.
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
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;
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.
Now you can automatically download backups with the hypothetical script “download-backup.sh”. With
ls, you can browse the files.
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
-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:
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.
-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.
rm simply removes files. Simply type
rm and the file name. Example:
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.
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:
Once completed, looks like this:
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)
To delete an empty directory, simply type
rm can delete whole directories.
rm normally won’t delete a non-empty directory. Add the switch
-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
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!
To log in with SSH, you don’t need an app for that, this is another handy command already on the Mac, type
ssh firstname.lastname@example.org. 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:
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.
To logout of the remote server, type
logout or press
control+D. Your screen should look something like this.
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.