Directories & Files
This course will get you up to speed with the fundamentals of Linux and prepare you for further study.
In today's lesson, we're going to talk about how to navigate man pages, what the PATH environment variable is used for, what the which command does, how to ask commands for help, and how to search for man pages. When using the man command there are a few key combinations that you should be aware of.
Enter moves down one line. So every time I hit enter, you see the page just moving down by one line. If you wanna move down an entire screen or a page worth at a time, hit the space bar. If you wanna go to the top of the page type lowercase g, and to move to the very bottom capital G shift G, and to quit you simply type Q.
Environment variables store information. They are storage locations that are a name value pair, and they're typically uppercase. All the letters in the variable are uppercase. And you can see the contents of the variable by using the echo dollar variable name command. We'll go into greater detail about environment variables in a later lesson. However, the environment variable that we're most interested in today is PATH.
PATH is an environment variable. It controls the command search path. It contains a list of directories that are separated by a colon. What I mean by that it controls the command search path is that when you type in a command at the prompt and press enter, that command will be searched for in the directories that are listed in your PATH environment variable.
In this example, user local bin will be searched first. If the command is found there, it will be executed. If it's not found, then /bin will be searched. And this process goes on and on until either the command is found in your path, or if it doesn't match your request then you'll be politely told that it can't be found. If you want to know the location or the full path to the command that you're executing, use the which command. For instance, you could type which cat.
Now, if there were multiple programs named cat, then the one that gets executed depends on your search path. If cat is in /bin and also in /user local bin, whichever directory occurs first in your path will be the cat command that gets executed. Here's an example of using which with tac command. Tac is cat spelled backwards. So can you guess what tac does? That's right. It displays a file's contents in reverse order. Putting this all together, you can start looking at what is in each directory in your path and use the man command to discover what each and every one of those programs does.
In theory, you could teach yourself how to use Linux this way, however, it would be a long tedious process and not very efficient. Knowing about the PATH environment variable gives you an idea of where commands are located on the Linux system. If you have an idea of something that you wanna do, but you're not exactly sure how to go about it or which commands to use, you could start by looking in your path looking in those directories and see if any one of those commands may help you out. And then you can get more details on exactly how to use that command with the man command.
Many commands will provide hints for how to use them at the command line. Some commands will accept the --h flag and others will accept -h, and some will refuse to give you any help at all. In that case, you would use the man command. Ls will provide some information at the command line when you specify --help. You can get help from gzip by using -h or --help.
If you're not sure which command to use, you can search through the man pages with man space -k space your search term. From there, you can read the man page for the command or ask for help with -h or --help. In this example, if we search for calendar we see a couple of programs, and we can just type man and those program names to determine which one does what we want to do.
In summary, man is the command you can use to display the documentation that is built into the Linux system. The PATH environment variable controls your search path. It can determine which command gets executed. You can learn the full path to a command with the which command by typing which and the command you're interested in, for example, which Ls. You can ask commands for help by using --help or -h. And if you want to search man pages use man -K.
Here are some suggested next steps. Log onto your Linux system, take a look at your PATH environment variable, go through each one of those directories and perform an Ls on it. For example, your path may contain /bin:/usr/bin. So you could type Ls space /bin or LS space /usr/bin. You could also change directory to those directories and then perform an Ls there.
For example, you could type cd/bin and hit enter, and then you're in the bin directory and you can simply type Ls. From there, pick some commands that look interesting to you and run man against them. Additionally, pick a few commands and try using --help and -h on them.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.