Basic Commands
From Linux 101, The beginner's guide to all things Linux.
Contents |
[edit] Running programs in Linux
Running programs is a very common thing to do on any computer, so we are going to go over some details of running them from a shell on a Linux system.
To run a program, you simply type the program name at the shell as the first word. To enter any command-line arguments, you hit space and put them after the program name. It is that simple.
[edit] Environmental Variables and The PATH
Environmental variables are very useful. When a program runs, it can read from them to gather some information about the system. They will be explained later on, because they are not really overwhelmingly important to beginners except to know that they exist, and you do use them all the time without realizing it.
At a shell prompt, type in
-
echo $PATH
and hit enter.
|
Note: $PATH not $path |
So how does your shell know where your program is located? It looks in the PATH environmental variable. PATH has a format of: "directory1:directory2:directory3:directory4" and so forth.
|
Note: You may have seen something similar to this in Windows. Windows separates directories with a semi-colon ; Linux uses a colon : |
When you hit enter, the shell looks at the program name you entered. Then it goes to directory1 and tries to see if the program is there. If it is, it will run it. If it is not, it will continue down the directory list until it finds it, or if it does not it will give you an error saying command not found.
[edit] Executing outside of the PATH
Perhaps you have just made a program in your home directory, how do you run it? /home/charlie is not in the PATH environmental variable. You simply have to tell the shell which directory it is in, and you can do this one of two ways. Using the absolute path location, or relative path location. Absolute path location means that when you type in the program name, you also type in what directory it is in.
|
Note: do not forget that leading forward slash! Example: /etc/X11/xorg.conf is not the same as etc/X11/xorg.conf unless you are in the / directory! |
Relative paths involve using your present working directory (at the shell, the pwd command gives it to you). Basically, this means that you are telling the shell where the program is relative to where you are currently working.
At a shell, you could type any of these to run the same program:
Example of absolute paths:
-
/home/charlie/program1
Examples of relative paths:
-
charlie/program1- (if you are in /home)
-
./program1- (if you are in /home/charlie)
-
../program1- (if you are in any subdirectory of /home/charlie, such as /home/charlie/cpsc111)
[edit] Tab Completion
This section is a guideline. Depending on your settings, you may or may not have to hit the <tab> key twice. Tab completion is a handy trick, especially if you are lazy. It is also helpful for finding program names when you forget them, but remember what they start with. To use it, type the first few letters of the program name, then hit the <tab> key. If it does not complete the program name, that usually means that there is another program name that is similar to the one you are trying to run. Hit <tab> again, and it displays a listing of the programs that are a possible match to what you are trying to run. If you type another character or two and hit <tab> again, usually it will match up for you.
As a seconary note, program command line arguments are often file names. If you have typed the program name, and now working on command-line arguments, the <tab> key now will complete file names of files in your present working directory.
Tab completion is an excellent tool for saving time and keystrokes.
[edit] Command-line Arguments
Command-line arguments can vary from program to program -- it usually is up to the programmer on the method they see fit. Most programmers see fit to use two different means: the long form and the short form.
The long form usually follows this pattern: --option-name or --option-name=value. A good example of the long form is --help which on 99% of programs will tell you how to run it, along with Man pages.)
The short form was made to save users keystrokes. When you look through Man pages and you see 3 options you wish to run a program with (let's say -a, -C, and -g) you could run the program like:
-
program -a -C -g
or
-
program -aCg
The second method usually just saves you the time of having to type out more.
It is important to note that command line arguments are usually case sensitive, so do not mistake -a for -A or vice versa. Follow the author's instructions closely. It is also perfectly fine to use the long form on some options and short form on others.
As a final note, when using the long form it is very easy to see what option has what value; for example, in program --debug-level=2 --daemon --hostname=clug.parl.clemson.edu it is easy to see that you're running program with a debug level of 2 and with hostname set to clug.parl.clemson.edu.
It may not be as easy to read the same off a short form method. You may expect to see something like program -Ddh 2 clug.parl.clemson.edu where -D is the shorthand of --debug-level, -d for --daemon, and -h for --hostname.
With the shortform, you pay attention to the order that you supplied the arguments. For example, these should do the same thing:
-
program -Ddh 2 clug.parl.clemson.edu -
program -dDh 2 clug.parl.clemson.edu -
program -dhD clug.parl.clemson.edu 2
But, these are bad:
-
program -Ddh clug.parl.clemson.edu 2 -
program -dhD 2 clug.parl.clemson.edu
You can expect the program to complain that the variables and values do not match up.
[edit] Running Programs as root
root is the almighty user on every Linux system. A user, by default, cannot modify anything system-wide or perform any actions on the computer. For example, a user by default cannot restart the network connection. Obviously this and many other actions would make using the computer for a normal user very hard. There are at least three ways to run a program as root root
- Login to the computer as
rootdirectly (bad idea) - Execute
su -as your normal user to switch to therootuser (better) - Execute
sudo <program>to runprogramwithrootaccess (best)
The 3rd option is the only one available on Ubuntu by default. It has the greatest flexibility while keeping users from always running as root.

