|
Search |
|
|
|
Classic 2 Guys |
|
10 Random Stories:
|
|
|
|
Don't Be Afraid of Unix! It's Here To Help. |
|
Most Mac OS X users, at some level, know that their operating system is based on BSD Unix. What they don't realize is that there is an incredible amount of power hidden underneath the GUI just sitting there waiting for the right situation to strut its stuff.
At its very best, the command line, or terminal, can be very arcane and intimidating. It is also true that, to Apple's credit, there isn't much that you cannot due in the GUI that you can do at the command line. However, as all Unix/Linux users know, the command line can offer solutions that are more elegant and take less steps than using the GUI. In fact, you could say that the arcane text interface of the terminal is its greatest attribute, the GUI gets out of the way.
We'll start our journey into Unix by going over exactly what the terminal is and isn't. The Finder and the terminal are very similar in many ways. They are both used to manipulate files. You can view, manipulate (copy, paste, etc), open and launch files. The major difference between the two is this: the finder only allows you to do and to see what Apple wants you to. In many situations the finder will mislead you and show information that is not exactly accurate. The GUI becomes in the way.
Now that we know what the command line is and isn't, it is time to dive in and learn a few commands. We will start with a handful of commands that are not only useful, but essential in order to get the feel of what the terminal can do.
First you will want to start the terminal application. You will find it in >Applications>Utilities.
If you've never used the terminal before, the first line you will see will be a hello message from Darwin (the kernel program). The next time you login to the terminal you will see a message giving you the last time you logged in followed by the hello message.
The next line you will see will look something like this:
Frodo:~ rich$
This is called the command prompt. The terminal shell is telling you that it is ready to accept input. The shell is actually a program that is running in the terminal that interprets what you type on the command line and executes the programs that you specify there. Let's look at each piece.
The first word, Frodo, is the name of the machine that you are connected to. At first this might sound strange, but in fact, you could be connected remotely over a network to a distant machine (to be discussed in another article).
The name of the machine is followed by a colon and the name of the current directory. In Unix, the ~ (tilda) symbol represents the logged in users home directory. My home directory would be /Users/rich.
The current directory name is followed by a $ which is the actual prompt. This prompt can change depending on who is logged in (you might see a # if you are logged in as the Super User) or what shell you are running ($ usually represents the BASH shell and the % usually represents the C shell).
Press return a few times and you will see the prompt repeated over and over.
Frodo:~ rich$
Frodo:~ rich$
Frodo:~ rich$
Frodo:~ rich$
The shell doesn't do anything, because there is no command (program) for the shell program to interpret. When working from the command line, the first entry is always a program followed by any switches (think of switches as things you turn on in a program to make it behave the way you want it to) followed by any arguments (arguments are the target files that the command is going to perform action on).
Let's try our first command. At the prompt, type the following (from this point forward, I am going to use the convention that all user input, the things that you type, will be in bold print):
Frodo:~ rich$date
Press return. You should see the following output:
Mon Dec 27 12:01:30 EST 2004
Frodo:~rich$
Congratulations! You have run your first Unix command line program. That wasn't so hard, was it? The date command will give you the current time.
Now that we have the hang of this command stuff, let's go a little deeper. At the prompt type:
Frodo:~ rich$ls
Press return. You should see something Similar to the following output:
Documents Music notes.txt
First, let's look at the command itself, ls. Unix was developed by extremely brilliant hackers at Bell Labs (my use of the term hacker here is a positive one. The media has distorted the meaning of the word to have negative connotations – here it means someone who programs extremely well). One thing that hackers despise is wasting time. The command ls is actually short hand for list. Making more sense now?
The ls command returned the items that I have in my home directory. (Note that the ls command does take an argument, a path. If you leave the argument blank, ls uses the current directory.) The output is hard to interpret in this form. How do I know what is a file or a directory? Easy, use a command switch.
At the prompt type:
Frodo:~ rich$ls -F
Press return. The output should be similar to:
Documents/ Music/ notes.txt
The -F command switch tells ls to put a / after all of the items that are directories. I have two directories, Documents and Music, and one file, notes.txt, in my home directory.
There are many more switches that can be used with the ls command. One of my favorites is -G. This tells ls to color code its output. Directories are one color, files are another, and links are a third color.
Where do you find out everything that ls can do? Again, hackers hate wasting time, so they built the help manuals right into the system. The next command we are going to learn is the help system command man (short for manual – see this Unix stuff is cake). To search for a specific command, type man followed by the command. The man program will search its pages looking for the correct command. It will then display the help pages in a pager program so that you can view it. The space bar will page down and typing a q will exit man and return you to the prompt. Try it with the ls command now and see what you can do with the ls command. Look in particular at the -L and -A switches (the -A switch is extremely useful when the finder won't display those pesky hidden files).
Now we can get help and list files in a directory. How do we move around the file system. The Unix file system is a tree structure that branches out from a central point called the root ( or /). All paths are relative to this starting point. How do we move around in the terminal? Let's say, that we want to go into the Documents directory to have a look around. We could use the command ls Documents to print the contents, but then if we wanted to manipulate any of the files in there, we would have to use the path (hackers hate wasting time and typing takes up time). At the prompt, type the following command:
Frodo:~ rich$cd Documents
Press return. Your prompt should now look like:
Frodo:~/Documents rich$
We are now in the documents directory. Use the ls command to prove that you moved. It will display any files and directories that you have stored in your Documents directory. Use the cd (short for change directory) program to move around the file system. You can use the cd command to jump around the file system. At the prompt type:
Frodo:~/Documents rich$cd /
Press return. The prompt should now look something like:
Frodo:/ rich$
Type ls -F. The output should look something like:
Applications/ Library/ System/ Users/
You are now at the root of the file system. You moved from your home directory to the very top.
Type cd ~. The prompt should now look like:
Frodo:~ rich$
Now I am back in my home directory (remember that in Unix systems the ~ is short for the path of your home directory, in this case /Users/rich). Move back into the documents directory (type cd Documents). An easy way to move one directoy up without typing the whole path is to use .. (that is dot dot). In Unix, this means move to the previous directory.
Before we do this, we should be sure of where we are at. At the prompt type pwd. The output should look similar to:
Frodo:~/Documents rich$pwd
/Users/rich/Documents
Frodo:~/Documents rich$
The command pwd is short for print working directory. Working directory is just another way of saying current directory or the directory that you are currently looking at.
Now type cd .. (cd space dot dot). The output should look like:
Frodo:~/Documents rich$cd ..
Frodo:~ rich$
As you can see from the prompt we moved to the proceeding directory in the tree.
Two very useful Unix commands are the cp (short for copy) and mv (move) commands. I will leave it as an exercise for the users to explore these commands. Look them up in the man pages to see what they can and can't do. Then make a directory in your home folder and fill it with some dummy files. Practice using the cp and mv commands to get the hang of it.
We have barely scratched the surface as to what is available at the command line. There are many online resources that list all of the commands that are available to users. Don't be fooled, however. Commands are just the beginning. In the next article we will be exploring pipes and redirection. In Unix, it is possible to pipe the output of one command to the input of a second command and that to a third command. You can also direct the output of a command to a file.
Right now, pipes and redirection are not important. Take what we have learned here and play with it. The best way to learn is by doing. Use the commands to explore your computer to see what is there. Knowing what is there can only make you a better user.
|
|
December 27 2004, 1:15 PM EDT, by |
Comments:
|
Jonahan |
12/28/04, 4:00 PM EDT |
Back in the day they tried to teach me the scary UNIX stuff in college, but it didn't take too well. I had a PowerBook 140 with a GUI beyond anything the SPARC workstations could do and wondered why I had to type "cp" or "mv" just to move files around.
But OS X is great to ease the average user into the full power of the Terminal. It's there if you need it, and you can peek at it from time to time when you're feeling adventurous!
Mac OS X Hints is also a great site to give you some really neat ideas of stuff you can do with the Terminal. (http://www.macosxhints.com/index.php)
|
nhmacusr |
12/29/04, 6:14 PM EDT |
The stuff in this articel isn't very exciting, but when we get to pipes and redorection (with standard input and output) things get more exciting! Especially when you are trying to find stuff. I hardly ever use the finder (the search engine sucks). Finding stuff from the terminal is much more precise.
|
FaramirTook |
1/2/05, 5:55 PM EDT |
You can search in the terminal? I know a little bit more about the terminal than the next guy from my forays into Linux, but I could learn a lot more. I'm going to explore and learn a bit more about FreeBSD stuff. Like /volumes over /mnt.
|
nhmacusr |
1/11/05, 8:47 AM EDT |
If you can get your head around the find command and use it in conjunction with grep, you have a content search much like spotlight is going to be. Regular expressions can be a bit of a bear, but when mastered, they are truely powerful. I can search through thousands of source files (using grep) and look for key words in the content of the files that I am looking for. This works for any file that isn't a binary.
|
This article is archived, so you may not comment on it.
(The good news is there's always the shoutbox, the forums or the contact form if you're socially-inclined at the moment!)
|
|
|
|
Site Links |
|
|
|
Deep Thoughts |
|
When somebody shouts out "Holy Toledo!" because something crazy was happening, I think they should be shouting "Holy Cincinnati!" or "Holy Cleveland!", because those two Ohio cities are much more chaotic than Toledo. I mean, c'mon.
|
|
Around Da Web |
|
iProng: |
iPhone steals show at CTIA Wireless 2007
|
DLO offers dual cover fashion case for iPod
|
AT&T received 1M inquiries on iPhone
|
MacDailyNews: |
Ars Technica in-depth review: Apple TV ?impressed all those who touched it?
|
Inside Apple?s Mac OS X 10.5 Leopard Server OS
|
The chips inside Apple TV
|
Think Secret: |
Adobe Creative Suite 3 pricing revealed
|
|
|
We Like: |
|
|
|
Side Projects |
|
Jonahan
- JediPoker.net
- Jonahan.com
- iProng
- MacProng
iKen
Jedbeck
J.P.
|
|