|
![]() |
You, the Terminal, and Your MacBy Francine Schwieder |
No doubt you have heard stories about the Terminal, if you have had a Mac and been running OS X for awhile. After all, at bottom OS X is a UNIX system, running on FreeBSD, and UNIX systems can all be run with nothing but the command line instructions (CLI). They were raised that way. If you are like many Mac users the Terminal, perhaps the mere mention of it, gives you the willies. With such a lovely graphical user interface (GUI) who would want to use anything else? Especially something that sounds a lot like DOS, the putative ancestor of Windows? Yuck. You may also fear the Terminal, you may have heard that you can do horrible damage to your Mac using it, so why take the risk? All of those things are true, and there really isn't any need to use the Terminal, usually. But occasionally there really isn't any better way to do something, and when you run into those occasions you'll discover that the Terminal can actually be pretty handy. And it really isn't that scary, unless you are in the habit of running as root, in which case you've probably already destroyed some interesting things. I haven't even enabled root on my machine, and have no intention of ever doing so. If you run into a situation where someone tells you it is easy to fix whatever it is, just enable root and then do this.... Well, nod politely and then do some research on using the Terminal. You can do whatever it is with far less risk.
Let's start with a brief look at the UNIX file system. It is a kind of hierarchical shrub, and you can get from any point to any other point by following the right path. The various points on the path are separated by a slash. Here's a sample path:
/Applications/Adobe Photoshop CS/New Feature Highlights.pdf
To make it clearer, here is the same thing, as you see it in a Finder window in column view:

OK, now go to your Utilities folder and launch the Terminal. That's here: /Applications/Utilities/Terminal.app, in UNIX speak. You'll get a nice little welcome to Darwin message and you'll have a spot marked at the prompt where you will type things. If you are using Panther then the shell will be bash. A shell is the name for a sort of program that translates what you want into a form that the UNIX operating system understands. By default the bash shell opens with the metaphoric you sitting in your home folder. To see that this is true type the list command (ls, that's ell ess) and hit the return key. Here's what mine looks like:
Panther:~ francine$ ls
Applications IDE Movies Public
Desktop Icon? Music Scripts
Documents Library Pictures Sites
Panther:~ francine$
Next try typing list with the "all" option. Options are preceeded by a "-" and are generally abbreviated. Also try list with both the "all" and "long" options. You'll get something like this:
Panther:~ francine$ ls -al
total 384
drwxrwxr-x 35 francine francine 1190 21 Jan 09:51 .
drwxrwxr-t 6 root admin 204 23 Sep 12:31 ..
-rw-r--r-- 1 francine francine 3 11 Jul 2003 .CFUserTextEncoding
-rwxr-xr-x 1 francine francine 15364 4 Feb 15:33 .DS_Store
drwx------ 45 francine francine 1530 4 Feb 23:08 .Trash
-rw------- 1 francine francine 0 1 Aug 2004 .Xauthority
-rw-r--r-- 1 francine francine 29 7 Dec 2003 .Xdefaults
drwxr-xr-x 5 francine francine 170 17 Nov 2003 .axyftp
-rw------- 1 francine francine 14269 4 Feb 15:20 .bash_history
-rw-r--r-- 1 francine francine 109 20 Mar 2004 .bash_profile
-rw-r--r-- 1 francine francine 72 20 Mar 2004 .bashrc
-rw-r--r-- 1 francine francine 24 3 Nov 2003 .cshrc
-rw------- 1 francine francine 55 4 Nov 2003 .cvspass
-rw-r--r-- 1 francine francine 55576 31 Jul 2004 .fonts.cache-1
drwxr-xr-x 3 francine francine 102 5 Feb 2004 .java
drwxr-xr-x 4 francine francine 136 17 Nov 2003 .jpi_cache
-rw-r--r-- 1 francine francine 14 11 Oct 2003 .lpoptions
drwxr-xr-x 4 francine francine 136 26 Dec 2003 .mozilla
drwx------ 5 francine francine 170 20 Nov 2003 .yafc
drwxr-xr-x 5 francine francine 170 7 Dec 19:56 Applications
drwxr-xr-x 9 francine francine 306 4 Feb 23:08 Desktop
drwxrwx--- 61 francine francine 2074 3 Feb 11:58 Documents
-rw-r--r-- 1 francine francine 0 17 Jan 20:14 IDE
-rw-r--r-- 1 francine francine 0 11 Mar 2004 Icon?
drwxrwx--- 46 francine francine 1564 30 Oct 12:53 Library
drwxrwx--- 9 francine francine 306 24 Jun 2004 Movies
drwxrwx--- 27 francine francine 918 14 Nov 01:08 Music
drwxrwx--- 29 francine francine 986 12 Jan 20:43 Pictures
drwxr-xr-x 4 francine francine 136 11 Jul 2003 Public
drwxr-xr-x 12 francine francine 408 30 Sep 01:19 Scripts
drwxrwxr-x 12 francine francine 408 6 Jan 15:23 Sites
I have a lot of dot files because of the things I do, but you are bound to have at least one, the .DS_Store file (where the settings for the view of your home folder are stored by the Finder), as well as the "." and ".." things, and you may have more, depending on the sorts of things you have installed. That's right, using the Terminal you can see all the invisible files you have that the finder hides from you, without them cluttering up your normal Finder window, as they would if you installed one of the utilities that let's you unhide them. The bit at the front of each file or folder, for instance "drwxr-xr-x" in front of the Documents folder, tells you that the item in question is a folder (the beginning "d" is for directory, a UNIX folder, a plain "-" is a file), and this is followed by the permissions. The owner and group are given, as well as the size and date modified.
If you want to list something else you can enter the path to it after the list command. So if you wanted to know everything that is really at the root level of your drive you would type this:
Panther:~ francine$ ls -al /
The slash all by itself signifies the root of the UNIX tree. Everything springs from here. You will be amazed by all the stuff you've never seen before. This stuff is hidden for a reason--it is crucial to the UNIX operating system and you aren't supposed to mess with it unless you know what you are doing. One of those mysterious items is a folder called bin. Inside of it are some UNIX commands (there are more in other places). Go on, list them!
Panther:~ francine$ ls /bin
[ dd kill pwd sync
bash df ln rcp tcsh
cat domainname ls rm test
chmod echo mkdir rmdir zsh
cp ed mv sh zsh-4.1.1
csh expr pax sleep
date hostname ps stty
If you want to know what a command does, you ask for the manual page. These pages were written by geeks for geeks and are less than informative for the rest of us. But take a look at a couple of man pages anyway.
Panther:~ francine$ man cp
This shows you all about the "copy files" command. There's lots of information about it, to see more hit the spacebar on your keyboard and it will move down a page. When you finish, or get bored and/or frustrated, hit the "q" key to return to the prompt.

One of the commands in that list is pwd, for print working directory:
Panther:~ francine$ pwd
/Users/francine
This shows you the full UNIX path to where you are on the directory tree (or shub or bush or whatever image works best for you).
BTW, the tilde (~) stands for your home folder. You can change your location by issuing the change directory command. You can return to your home by just typing cd and hit return, and to a specific folder in your home by typing cd ~/Nameoffolder and return. Here's an example where I moved myself to the root level, then into my home Pictures folder (and verified where I was each time with the pwd command):
Panther:~ francine$ cd /
Panther:/ francine$ pwd
/
Panther:/ francine$ cd ~/Pictures
Panther:~/Pictures francine$ pwd
/Users/francine/Pictures
Now, let's say you've gotten something in your trash and the trash won't empty whatever it is, and you have tried holding down the option key when you attempt to empty the trash (this will usually get rid of locked files inside of some application you want to trash that refuses to go away), and you still can't get the trash emptied. Time for the remove command.
You can remove anything that belongs to you quite easily. Issue a cd command to put yourself in a folder with items you want gone (the easy way to do this is to type "cd" then a space, then drag the folder in question into the Terminal). After you've changed directories, list the folder contents to make sure you're in the right place. Lets say I want to delete one of the files in a folder with some images in it:
Panther:~ francine$ cd /Users/francine/Desktop/folder
Panther:~/Desktop/folder francine$ ls
486innards.jpg 486side.jpg DSCN0282.JPG
Panther:~/Desktop/folder francine$ rm 486side.jpg
Panther:~/Desktop/folder francine$ ls
486innards.jpg DSCN0282.JPG
Yep, typing "r" and "m" and a space and the name of the file (you can copy and paste from the list), hit enter and it is gone. There's no changing your mind with this command. One thing you can do is have it ask you if you are sure, for each item, that you want to remove it. In other words, you'll be running the remove command interactively. Type a "y" for yes, hit enter, and that item is gone. If you don't want it gone, just hit enter to move on to the next item.
Panther:~/Desktop/folder francine$ rm -i *
remove 486innards.jpg?
remove 486side.jpg? y
remove DSCN0282.JPG?
Panther:~/Desktop/folder francine$ ls
486innards.jpg DSCN0282.JPG
The asterisk after the interactive option is a wildcard, and it stands for "anything whatsoever." Thus the remove command will go through every single file in the folder, but not sub-folders. If you had another folder in there you would get this message:
rm: fold2: is a directory
and nothing would happen to that second folder. To get rid of an empty folder issue a remove directory command (rmdir) and the name of the folder. If there is something in the folder you'll be informed the directory is not empty, and nothing will happen. You can remove things recursively, and the directories too, and force remove items regardless of ownership, but that always makes me nervous. Read all about it on the man page. And you can do all of that as a temporary, but fully powered, super user (the equivalent of root). If you do that in the wrong spot and the wrong way you can mess up your computer so badly that the only recourse is to put in the Install Disk and install a brand new system. So I use remove one file at a time.
So to get rid of a really stubborn file in the trash I would navigate there and issue a remove command as super user. That's the "super user do" or "sudo" command, and it makes you all-powerful for a short period of time, so be careful with it. You'll be asked for your password (and the first time you do it you'll be given a sort-of-stern warning about misuse), type it carefully, as it is not echoed to the screen, and then hit the return key.
Panther:~ francine$ cd .Trash
Panther:~/.Trash francine$ ls
badfile fold2
Panther:~/.Trash francine$ cd badfile
Panther:~/.Trash/badfile francine$ ls
486innards.jpg DSCN0282.JPG
Panther:~/.Trash/badfile francine$ sudo rm -i *
Password:
remove 486innards.jpg? y
remove DSCN0282.JPG? y
I was in my home folder, moved to the trash, which has two folders, we'll presume the "badfile" folder contains files that can't otherwise be eliminated. So I moved into that folder, did a list to make sure I was in the right place, then issued the command to operate as super user and remove files interactively.
There is one set of circumstances where this will not work. Windows will let people give files names containing invalid characters. The Finder will let you copy these files to your drive. After that there is nothing you can do. UNIX doesn't recognize the files, because of the invalid characters, and neither does the Finder. You're stuck with them until you reformat your drive. The files that I have heard of where this is the case were named on a PC with various accented characters from various languages (one of them was an MP3 file with the title in Icelandic). So if you are copying Windows files do be careful. If the file has strange looking characters in its name, rename it BEFORE you copy it. If you can't rename it, and copy it anyway, don't say you weren't warned.
What I use sudo for most often is to run the cron jobs. Those are the UNIX maintenance tasks that run in the wee hours of the morning, if you leave your computer on 24/7, and not asleep. Some run daily, some weekly, and some monthly. You can download any number of utilities to allow you to do this using a nice Mac GUI. I downloaded one myself (MacJanitor), but it is actually just as easy to type the command every now and then when I happen to be running the Terminal anyway. You can run the commands individually, or you can do as I do, and whenever you happen to think of it run them all in one fell swoop:
Panther:~ francine$ sudo periodic daily weekly monthly
Password:
One of them (I think it's the weekly) takes a bit of time to do its thing, so go have dessert or something while it works. You'll know it's done when the command prompt reappears.
I've posted my personal list of favorite bash commands HERE.