Join Kevin Skoglund for an in-depth discussion in this video Terminal and Unix setup, part of Ruby on Rails 3 Essential Training.
As developers we are going to need to work with UNIX, which is the backbone of Mac OS X. We can't do that from the graphical user interface that you usually use on your Mac. Instead we will need to use a command-line application called Terminal. The Terminal application comes pre- installed on your Mac and it lives inside the Applications/Utilities folder. Where can you find that is you can either to File > New Finder Window, and select Applications or you may also have a quick link for Applications down here in your dock. Once you do that, scroll down to the bottom to find the folder called Utilities, open that up, and scroll down until you find the application Terminal.
I am also going to drag a copy of that to my dock just because we are going to be using the Terminal program very often, so that way I will have easy access to it. We can close this window and now I will launch Terminal. Now yours probably looks different from mine. For example, you probably have a white screen. That's simply because I've gone to Terminal > Preferences and I've changed the color settings here for mine to use a special set of configurations that I developed. I think that yellow on black is just a little easier on the eyes and it's clearer for you to read in these movies. You can feel free to play with the colors and set them however you like.
Now let's explore a little bit of UNIX. Don't worry that my command prompt here at the beginning looks different than yours. That's also something we can configure and you'll see that just in a moment. To begin with, let's start in the cd command. This is for Change Directory. This is what allows us to move between directories, the same way that we have open and close folders in the normal Mac OS. So in edition to telling it to change directory, we have to tell it where we want to go. First let's put in the tilde. That's in the upper left of your keyboard. That tilde is the shortcut for saying go back to my home directory.
That's the directory that I should be in by default but it's never a bad idea just to make sure that we are where we think we are. So this will change my directory to that directory. It doesn't give me any kind of prompt but that's where I am right now. If we type ls, that is a listing of what's in the directory. So now we can see what's in my directory and those are the same things that we would see if we went into the Mac and we, for example, open up a new window. Here's Kevin and here's those same folders. Desktop, Documents, Downloads, etcetera. So that's where we are. We are in my root user directory.
We can also change directories to another directory. So, for example, I can go into my Library folder by typing cd Library. Now I'll move into that Library folder. We can see that with another ls. I can then go backwards a directory by using cd.. and that will back me up one directory and now I'm back in my home directory. We can move forward more than one directory at a time. For example, I can type Library and then a slash and then whatever folder I want to go in. Let's say it's Application Support.
Another trick is that we can start typing what we want to hit the Tab key and it will auto complete for us, which is especially nice if a folder name has spaces in it, because then it puts the backslash in front of those spaces for us. So that will move forward into that Application Support folder. To go back now, cd../.. would go back to directories instead of going back just one. You get the idea. Now as I say we can ls to see what's in that directory. We can also pass in some options to ls. ls -la, we can do either one of those or both of those together.
The dash lets it know that it some options that we are going to be passing in and that will give us a list of all the things that are in the directory. It will also include the file permissions, the file owner, the file size, the date, and then you'll notice that the list of files that's there includes not only Desktop, Documents, Downloads etcetera, it also shows files that have dots in front of them. Those dot files are usually configuration files. They are hidden from our normal view on Mac OS but we can see them in UNIX when we pass in that -a option.
So ls -la will show us this list view and include those dot files. Okay let's try couple of other things here. I am just going to clear my screen with the Command+K, just to get that out of the way, and let's try echo 'hello', all right. So that's just simply going to echo back hello. Echo is just a real simple command that will return a value to us, just like an echo would. We can echo $SHELL, all capitals, and this is going to give us the value of the special constant that set up called SHELL.
When we are in UNIX, we are not just in UNIX. We are also in a working environment. By default on the Mac that's going to be the born again shell, which is just called bash. So most of the time you're going to be working in bash. It's kind of become the industry standard. Most people use bash. The people are using other ones usually use them because they were familiar with these other environments or because they're trying out some new ones that may have some fancy new features, but bash has really become very standard. So just keep in mind, you are going to be in UNIX working in the bash shell. Now we also have a command called which, and which will show us where a program is located.
Echo itself, it's just a tiny little program. It's not a big fancy program like Photoshop, and it exists inside the bin folder. There is a file called echo and that's the echo that's being called when I run echo. The reason why the location of that file is important is because it is possible to have more than one copy of an application on your computer, just like you can install more than one copy of a web browser. But since we are in the command line and we're just simply typing something like echo, it needs to know which one it should go and find. It's not like we are double-clicking on it, like we do from the graphical user interface.
So how does it know which one if we have more than one? Well the way it decides is by using another variable called PATH. This is my current path. What this is, is the places that will look for command and the order that it will look for them. So first it will look in usr/bin: to see if I have echo in there. If I don't, then next, it will they will look inside bin to see if it's there. Then it will look in usr/ sbin:/sbin:/usr/local/bin: and finally in this path here. Now notice that it got it on this second one here, /bin.
That's where it found echo. So it didn't find it in usr/bin:. It found it in just bin. This is very important. If UNIX isn't finding programs that it's looking for, it may be because your path does not include the path where that's located. It's not going to look anywhere else. It's going to look in these places that are in this list and then it's going to give up and say, "Sorry, I can't find it." So what we want to do now is actually make some changes to that path variable. Let's start by doing ls -la again. We will see the list of the files that are there. You will see that I have a file called bash_profile. I created that file.
You probably don't have it. If you do happen to have something called bash_profile, that's fine, or if you have something called .profile, those are basically the same things except bash_profile is just for the bash shell, whereas profile is for all shells. I typically tend use bash_profile just to make sure that it's clear that these of the instructions I want to use with bash. You don't have both unless you are really are certain about what you're doing because they can conflict with each other. So in order to open up bash_profile and edit it, I am going to use another command called nano. This is a built-in, very simple text editor from the command line, not like Microsoft Word or anything that's graphical like that,. This is from the command line, nano .bash_profile.
Now if you don't have this file, go ahead and type that anyway. That's fine. What we are going to do is say we are going to be creating and saving this file with this name. It's not like a lot of the other programs where you are used to, where you provide the name only after that fact. We are going to go ahead and give it the name at the beginning. So nano .bash_profile. Mine opens up and has some content in it already. The first one is telling it to customize my prompt. that's why my prompt looks different than yours. You can customize this yourself if you like. What I've said is, use the username followed by $ sign and the space.
You can Google for PS1 and find out more about how you can customize that if you want. Then I've also put in an alias here. So alias ll is going to be equal to this command. It's the same thing as ls -la that I have been typing, with H and G just to make it look a little prettier. So by defining this alias, now I can just type ll and get the same thing as if I typed all the rest of that out. ll is a very common one a lot of UNIX programmers use. I encourage you to use it as well. What I want to do though is skip down to the new line and this is the important part that you need to add, which is export PATH.
The path is now going to be equal to "/usr/local/bin:. That's the directory we first want to look in to see if we have anything installed. Then we are going to add /usr/local/sbin: and then after that, usr/local/mysql/bin and last of all if you don't find it in any of these places, look in that long list of places that you already had saved. Whatever value path had to begin with, stick those here.
So what I am basically doing is saying, "Hey, before you look in your default places, check a few of my custom folders first." This usr/local is where we are going to be storing things and that's a very common way to do it so that all of your programs that you install, will all be installed in your usr/local folder. They will be kept there and separate from everything else. So let's do that. We will hit Return one last time there just to make sure that we have one extra line return and then you will notice that down here, it tells me to Exit and I am going to hold down the Control key. That's what that up arrows letting me know, so Control+X.
Save the modified buffer? We are going to tuck a Y because yes, we want to save it. File name we want write to, we have already indicated its bash_profile so it has that in there. We just hit Return and now we've saved the changes to that file. Another UNIX command is cat, which will output the contents of a file. cat .bash and we can also use auto- complete here, bash_profile, hit Return and now we should see the contents and they should look something like this. Again PS1 and alias are optional, you don't have to have those, but export PATH is the important part.
You definitely want to make sure you have it and you want to make sure that it looks just like what I have there. Now if we try again the echo $PATH, you will notice that we don't get our new changes. That's because the profile gets run whenever a new Terminal window opens. So let's close this window and let's just open up a new window again. Here we are. Now if we say echo $PATH, you will see that we do get our changes. They are included there, and also because I use the ll for the alias, you will see that I can just type ll now and get that full list.
Okay, so that does it or configuring UNIX and learning a few of the basics about how to move around. Next we need to move onto installing some other software.
- Understanding MVC (Model View Controller ) architecture
- Routing browser requests through the framework
- Responding to requests with dynamic content
- Defining associations and database relationships
- Creating, reading, updating and deleting records
- Working with forms
- Validating form data
- Reviewing built-in security features
- Authenticating users and managing user access
- Debugging and error handling