Useful UNIX/LINUX commands

There are quite literally hundreds of commands that UNIX can execute. Many of them will be of no interest; it's rather like conversation not requiring all the words in the dictionary. This section mentions some of the more common commands, with a brief explanation of what they do. In order to make sense of the explanations, however, it is necessary to describe some assumptions and terminology that most UNIX documentation will assume without explicitly stating.

All information processing systems require some form of information storage. In UNIX (and most other computing environments) related pieces of information are stored in an addressable unit called a file. Each file has a name which in UNIX can be up to 255 characters long. These characters may be virtually anything that can be generated on the keyboard, but you are well-advised to stick with printable characters, and to remember that UNIX is case sensitive. The following example shows one of the many ways to create a file:

$ % date > test

$ % cat test

Thu Sep 16 11:06:54 CDT 1999

$ %

As illustrated in an earlier example, the date command outputs the current date as a string of characters. This time, however, they did not appear on your display because the addition of the ``>'' symbol instructs the shell to store all output from the current command in a file, named in this case test. If there had already been a file named test, its contents would have been replaced by the output of the date command. The second command given to the shell to execute was named cat (which is short for concatenate), followed by the name of the file test. The cat command simply outputs the contents of the named file(s); in this case the file named test contains the output of the previous date command, so that is what appears on the line following the cat command.

The average UNIX system has thousands of different files. Most of these you will never see or access, so it is not reasonable to expect you to know their names. What you need is a reserved name space in which you can name files whatever you like without reference to what others have done. This is accomplished by the directory mechanism. A directory is named in the same way and with the same conventions as a file, but provides a new name space for files. In other words, a directory is a special kind of file that contains other file names. All files, including directories, must exist in the name space of at least one directory. A file name has no meaning outside the context of the directory in which it is stored. Some examples will give an intuitive grasp of these formalizations.

Having built a file named test in the example above, it is instructive to examine it more closely:

$ % ls 

test

$ %

The command ls (short for list) shows the contents of a directory; by default ls shows the contents of the current working directory. In the case above, the only file in the current working directory is the one named test. A new directory with an empty name space is created with the mkdir command:

$ % mkdir wrk

$ % ls -l

-rw---- 1 smith 2324 29 Sep 16 11:17 test

drwx--- 2 smith 2324 512 Sep 16 11:56 wrk

$ %

A command name may be followed by ``arguments'' - options which are recognized by that command. In the case above, the argument -l requests that the ls command show a long format listing with added detail for each file. The command mkdir added a file to the current working directory with the name ``wrk''. Notice that the lines describing the file test and the file wrk are different in some respects. The first character of each line in a long format listing indicates a normal file with the ``-`` character or a special file with some other character. In the case of the file name ``wrk'', the character is a ``d'' which identifies wrk as a directory. Because the ls command lists the contents of a directory, the following command can verify that the directory named wrk is indeed empty6:

$ % ls wrk

$ %

To move the file named test to the directory named wrk, the following command would suffice:

$ % mv test wrk

$ % ls -l

drwx--- 2 smith 2324 512 Sep 16 11:56 wrk

$ % ls -l wrk

-rw---- 1 smith 2324 29 Sep 16 11:17 test

$ %

The mv command moves the name of a file (or group of files) from one directory to another. In the case above, notice that the file named test is no longer listed in the current working directory but is listed in the directory named wrk. Adding the name of a directory as an argument to the ls command causes the contents of that directory to be listed rather than the default.

Having made a new directory, it would be convenient if that directory could be made the current working directory (the default name space). The following command does precisely that:

$ % cd wrk

$/wrk % ls -l

-rw---- 1 smith 2324 29 Sep 16 11:17 test

$/wrk %

The cd command (short for change directory) sets the current working directory to the argument provided with the command. Notice that the command prompt provided by the shell has changed - the ``~'' has become ``~/wrk''. With this particular shell, the current working directory is printed as part of the command prompt. Now the ls command will show the file named test. Since only one directory at a time can be the current working directory, it is an error to enter a cd command with more than one argument:

$/wrk % mkdir foo bar

$/wrk % ls -l

drwx--- 2 smith 2324 512 Sep 16 11:56 bar

drwx--- 2 smith 2324 512 Sep 16 11:56 foo

-rw---- 1 smith 2324 29 Sep 16 11:17 test

$/wrk % cd foo bar

cd: Too many arguments.

$/wrk %

If you enter a cd command with no argument, your login directory (often referred to as your home directory) will be assumed:

$/wrk % cd

$ %

If your shell does not include the current working directory in the prompt, the following command will output it:

$ % pwd

/home/user/sas/

$ %

The string of alpha-numeric characters output by the command pwd (short for print working directory) is called a path. In UNIX, a path (or sometimes pathname) is the sequential list of directories which must be examined in order to identify a unique file. Each directory is separated by the ``/'' character. The final file named in the string may be either a directory or a normal file, but all the previous names must be directories. The path printed in the example above shows if you search the directory ``/home'' you will find a directory named ``user''; in that directory will be one named ``sas''. There may be many more files named sas on this system, but pwd command this particular one as the current working directory. Paths like the one above that begin with the ``/'' character are called absolute paths. If the string does not begin with the ``/'' character, it is termed a relative path, and assumes that the first element in the string can be found by searching the current working directory. The "cd wrk" command above is an example of a relative path. It is very important that you understand paths because that is the method used in UNIX to disambiguate file names.

The cd command is used to navigate the UNIX file system. All you need to know is the name of the top-level directory where absolute paths begin. That directory is generally referred to as the root directory, but has the name ``/'' (not /root). You can test this statement by trying commands you have learned above that use directory names as arguments (for instance ls /).

Eventually you will want to view the contents of a file. A previous example showed the use of the cat command to accomplish that, and indeed any file can be output in that manner. If the file happens to contain more lines of text than your display device can show at one time, however, the first lines will be scrolled off the top of the display before the bottom lines are available. A useful refinement to the cat command would be the ability to view the file "screen-full" at a time. Programs that do this are called pagers in UNIX due to their ability to display a file "page" at a time. Most UNIX systems have several pagers available with different features. The most popular one on LINUX is named less:

$ % ps -ef > output

$ % less output

Unlike the previous example commands, the less command does not automatically exit; instead it will show you the first screen of data, then wait for you to type the space bar before showing the next screen. When you reach the end of the file, pressing the space bar will do nothing (or maybe beep on some terminals). In order to exit the less command, type a "q" key. You do not need to be at the end of the file to quit the less command. To page backwards (in other words, to see the previous screen), use the "b" key. Other pagers commonly available on UNIX systems are named more and page. They operate in much the same way, but may use different characters to page forward and backward through the text file.

Before you attempt to view files with a pager or cat, be aware that many files are not text files. If you try to view a file that is not a text file, the results are very unpredictable, and most often unpleasant. Displaying a binary file (directories and most executable programs) on your screen will probably "scramble" your display device making it very difficult or impossible to use. Some pagers will try to warn you of this potential problem before outputting the file:

$ % less wrk

wrk is a directory

$ % less /bin/date

"/bin/date" may be a binary file. See it anyway?

The first command attempts to use less on a directory; the command interprets this as a mistake since the file type is obviously not a text file. The second example attempts to page an executable file which is a binary. The less command cannot tell for sure, since it is not a special file, but warns you of the potential problem and waits for a "y" or "n" response before proceeding. The correct response is, of course, n. All pagers may not be so helpful (certainly cat is not), so it is best to know the content type of the file before trying to view it. Directories are easy since an ls -l command will identify them, but here is a more general solution:

$ % file /bin/date

/bin/date ELF 32-bit MSB executable ...

$ %

The file command will output the content type of any file name passed to it as an argument. There are many types of files, but if the file type is not some form of text file, you should not try to view it on your display. In order to both view and change a file, you will need to use one of the family of programs called editors.

UNIX was one of the first computing environments to provide online documentation. Most native UNIX commands will have a manual page which may be referenced by entering the man command:

$ % man date

This command will present the reference documentation for the command provided as an argument (date in this case) using a pager. If there is more than one reference page for a command, you will be asked which one you want to view. In general, the first command offered is the most common, and you may respond with the number one. The online reference manual for UNIX is not tutorial in nature. It provides a formal definition for the command which you may find a bit technical at first. There is a man page for the man command which will describe the format, but usually you can find the available options by scanning quickly. You may not be able to find a man page for commercial applications that are not part of the native operating system:

$ % man matlab

No manual entry for matlab.

$ %

In these cases the manufacturer chose to provide the documentation in another form - perhaps HTML or printed form. The group providing support for this specific application should be able to help.

There are several editors on most UNIX systems, each with unique command sets and features. The topic of editors is too lengthy for this discussion (check the References section for a suggestion for the emacs editor).