From the course: Unix Essential Training

Command basics - Unix Tutorial

From the course: Unix Essential Training

Start my 1-month free trial

Command basics

- [Instructor] In this chapter, we will discuss Unix commands and processes. Unix commands are just small programs, and those programs are really just files in the file system that have code in them, and of course, we need to be able to run them. So we want to have permission to execute that code, and that's it. It's a file that has code that we execute. Many of those files are stored in either the bin directory or in the usr bin directory. If you go to the command line and you type which followed by the name of a command like cat, it'll tell you the location of the cat command that you're using /bin/cat in this case. That means that the file name is cat and it's in the bin directory. We can do which ls for the ls command and see it's in the same place. If we do which banner, we learned the banner command earlier, we can see that it's stored in a different place. It's in the usr bin directory. Each one of these is a small, little program that we execute. For almost every Unix command, there's also an associated manual page or information about how to use that command. We call them man pages for short because the command to get to them is man space, and then the name of the command. So I'm asking for the manual page for cat, and it will open up documentation on how to use the cat command. This is using the more pagination tool that we saw earlier. So we can hit the space bar to go forward or F or B to go forward and back where the page is. We can use the up and down arrows, and the man pages tell you how to use the command as well as give you information about all the options that you can use with it. So for example, we see here that the cat command can use the n option in order to number the output lines. The manual pages are super useful, and a really good way for you to learn more about how the different commands work and what the different options are available for them. So how does Unix know how to find these commands after all the ls command was stored in this directory while the banner command was stored in a different directory, how does it keep those straight? It does that using a variable called PATH, and we can see that value if we use echo space $, and then in all capitals, P-A-T-H. This is my current path value. Yours may be different than mine. As we'll see in a moment, it's something that we can customize. This string is a list of the directories where Unix will go looking for commands and in the order that it will go look for them. So the very first place it'll go look for the command is in that usr, local bin directory. Then it will try usr bin then just bin, then usr sbin, and then sbin. So if the command happens to exist in more than one place, it gives them an order of preference as well. We can demonstrate how this works by changing the PATH variable. You'll have to use capital P-A-T-H=, and then let's use the same part of the name, but let's leave out the bin portion here, everything from bin forward because that's where the ls command is. So you can just copy and paste all of this, or re-type it and then hit Return. Now if we hit the up arrow and echo the path value again, you'll see that it's been changed. So now let's type the ls command to list the contents of the current directory. It comes up and says, sorry, the command's not found. It looked in those two places that are in my PATH, and it didn't find it in either one of those, so it gave up. We can go ahead and change this value back just by adding in the rest of this. I'll just copy it, or you can re-type it and then hit Return, and now when I type the ls command, you can see it works again because now it looked in this directory and found the command there. The PATH's an important concept. A lot of the built-in commands are going to be found because they exist in these regular directories, but if you had your own custom commands, or if you installed other software like a database program it might have commands that sit in a different directory, and you'll have to amend your PATH, so that it also looks in those directories to find those commands. One final thing I want to note is that when you set the path variable in this way inside the shell, it's only keeping that PATH for this current session inside the shell. If we opened up a new window, or if we close this window, and open a new one, it would revert back to the default path again. In a later movie, we'll learn about configuring your environment, and we'll learn to make that change permanent.

Contents