Interacting with the System
It is common to have people working on UNIX
systems that have never
worked on a computer before or have only worked in pure windowing environments,
like on a Macintosh. When they get to the command line,
they are lost. On more
than one occasion, I have talked to customers and I have asked them to type in
cd /. There is a pause and I hear:
I think to myself, "that's too many characters." So I ask them what they typed,
and they respond, "cd-space-slash."
We need to adhere to some conventions throughout this site to make things
easier. One is that commands that I talk about will be in your path unless I say
otherwise. Therefore, to access them, all you need to do is input the name of
the command without the full path.
The second convention is the translation of the phrases "input the command,"
"enter the command," and "type in the command." These are translated to mean
"input/enter/type in the command and press Enter." I don't know how
many times I have talked with customers and have said "type in the command" and
then asked them for what happens and their response is, "Oh, you want me to
press Enter?" Yes! Unless I say otherwise, always press Enter after inputting,
entering, or typing in a command.
Simply having shell is probably not enough for most users. Although you
could probably come up with an interesting and possibly useful shell script,
more than likely you're going to need some commands to run. There are literally hundreds of
different commands that come with your system by default and there are many more different
variations of these commands, which you can download from the Internet.
Sometimes the commands you issue are not separate files on the hard disk, but rather are
built-in to your shell. For example, the cd command, which is used to change directories, is
part of the shell, whereas the ls command, which is used to display the contents of directories
is a separate program. In some cases one shell has a particular command built-in, but it is
not available in another shell.
In general a command is broke down into three parts:
programname option(s) argument(s)
Note that not all commands have options and you do not always need to have arguments to a command.
For example, the date does have any arguments and works just fine without any
options. Some command are built-in to the shell you are using, but may be an external command with
a different shell. For example, the echo is internal to the bash
shell, but you will probably also find the /bin/echo command on your system.
If you ever run into trouble and are confused about the behavior of your shell, one important
thing to know is what shell you have. If you weren't told what shell you had when your account
was created or you are installing Linux for the first time and really don't know, there are a
couple of ways of finding out. The first is to simply ask the shell. This is done by accessing
the $SHELL environment variable. (We discuss environment variables in detail in the
section on shell variables.) This is done using the echo
command like this:
As you might guess, the echo command simply displays on the screen exactly what you told it,
in this case we told it to display the $SHELL variable. (We know it is a variable because of
the leading $, which we also will discuss in section on shell variables .)
What should probably happen is you get something like this:
In this case, the shell is /bin/bash. We can also find out what shell we are using by seeing which programs we are currently running. With Linux, as with other Unix dialects, a running
program is called a " process", and you check your processes using the ps command (for
process status). You can start it with an argument simply by
inputting ps and pressing the enter key. This will probably
get you something like this:
PID TTY TIME CMD
21797 pts/1 00:00:00 bash
6060 pts/1 00:00:00 ps
In this case we see under the heading CMD (for command) only "bash" and not the full
pathname as in the previous example. However, there are options to the ps command which
will show us the path.
The shell you are using is just one piece of information the system maintains in regard to your
current session. Much of this information is stored in the form of variables, like your shell.
These variables are set for you when you login to the system. You can also set variables
yourself using the set command. This might look like this:
Where VAR is the variable name and "value" is the value which you assigned to that variable.
Note that it is not until you want to access the value of the variable that you preceded with
the $. To find out the contents of all variables, you would use the set command by itself with
no arguments. This gives you a long list of variables.
When you login to the system you start in your "home" directory, which can be stored in the
$HOME variable. As we discussed earlier, to change your current directory (also called your
working directory) you use the cd command. If you wanted to return to your home directory,
you can issue the command cd $HOME and your shell will pass the value of the $HOME variable to
the cd, which would then change directories for you. (Note that typically if you use the cd command
with no arguments at all, you change to your home directory by default.)
One part of your environment which is extremely useful to know is the directory you are
currently in. To do this you might want to tell the system to simply print your current
working directory. This is done with the pwd command, which simply displays the full path to
your current directory.
It is also useful to see what files and directories reside in your current directory.
This is done
with the ls command (short for "list"). Without the options the ls command provides you a
simple list of what is in your current directory, without any additional information. The
output might look like this:
letter.txt memo.txt picture.jpg
you can use the -l option to get a "long" listing of the files and directories. This might show
you something like this:
prompt# ls -l
-rw-r--r-- 1 jimmo users 2457 Feb 13 22:00 letter.txt
-rw-r--r-- 1 jimmo users 7426 Feb 15 21:33 memo.txt
-rw-r--r-- 1 jimmo users 34104 Feb 14 21:31 picture.jpg
This information includes the permissions on the file, who owns the file, the size, and so
forth. Details of this can be found in the
section on file permissions.
For a more detailed discussion on how various shells behave see the
section on shells.
There are many ways to do the things you want to do. Some use a
hammer approach and force the answer out of the system. In many cases, there are
other commands that do the exact same thing without all the gyrations. So, what
I am going to try to do here is step through some of the logic (and illogic)
that I went through when first learning Linux. That way, we can all laugh
together at how silly I was, and maybe you won't make the same mistakes I did.
Every dialect of UNIX
that I have seen has the ls command. This gives a
directory listing of either the current directory if no argument
is given, or a
listing of a particular file or directory if arguments are specified. The
default behavior under Linux for the ls command is to list the names of the
files in a single column. Try it and see.
It is a frequent (maybe not common) misconception for new users to think
that they have to be in a particular directory to get a listing of it. They will
spend a great deal of time moving up and down the directory tree looking for a
particular file. Fortunately, they don't have to do it that way. The issue with
this misunderstanding is that every command is capable of working with paths, as
is the operating system
that does the work. Remember our discussion of Linux
basics. Paths can be relative to our current directory, such as
For example, assume that you have a subdirectory
of your current working
directory called letters. In it are several subdirectories for types of letters,
such as business, school, family, friends, and taxes. To get a listing of each
of these directories, you could write
Because the ls command lets you have multiple commands on the same line, you
also could have issued the command like this:
Both will give you a listing of each of the five directories. Even for five
directories, typing all of that is a pain. You might think you could save some
typing if you simply entered
However, this gives you a listing of all the files and directories in ./letters, not the
subdirectories. Instead, if you entered
the shell would expand the wildcard
(*) and give you a listing of both the ./letters directory as well as the directories immediately below ./letters, like
the second example above. If each of the subdirectories is small, then this might fit onto one
screen. If, on the other hand, you have 50 letters in each subdirectory, they are not all going to
fit on the screen at once. Remember our discussion on shell
basics? You can use the pipe
(|) to send the command through something like more so that you could read it a page at a time.
It is common to run command one right after the other. If you simply press the enter key after the
first command, the shell executes it before returning to the prompt. Often you want to issue
two commands in sequence. This is done by separating the commands with a semi-colon, like this:
Note that these commands are not really connected in any way. The shell simply executes one
after the other. To actually "connect" the commands, you would need to use a
pipe. Details on pipes can be found in the
section on shells